mirror of
https://github.com/actix/examples
synced 2025-02-02 09:39:03 +01:00
Add tests in examples (#185)
* Add test in json example * Add test in hello world example * Remove extern crate
This commit is contained in:
parent
9e28d2ec8b
commit
4e8f8d0b93
@ -19,3 +19,32 @@ fn main() -> std::io::Result<()> {
|
|||||||
.bind("127.0.0.1:8080")?
|
.bind("127.0.0.1:8080")?
|
||||||
.run()
|
.run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use actix_web::dev::Service;
|
||||||
|
use actix_web::{test, web, App, http, Error};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_index() -> Result<(), Error> {
|
||||||
|
let app = App::new().route("/", web::get().to(index));
|
||||||
|
let mut app = test::init_service(app);
|
||||||
|
|
||||||
|
let req = test::TestRequest::get()
|
||||||
|
.uri("/")
|
||||||
|
.to_request();
|
||||||
|
let resp = test::block_on(app.call(req)).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(resp.status(), http::StatusCode::OK);
|
||||||
|
|
||||||
|
let response_body = match resp.response().body().as_ref() {
|
||||||
|
Some(actix_web::body::Body::Bytes(bytes)) => bytes,
|
||||||
|
_ => panic!("Response error"),
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(response_body, r##"Hello world!"##);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,6 +1,3 @@
|
|||||||
#[macro_use]
|
|
||||||
extern crate json;
|
|
||||||
|
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
error, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer,
|
error, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer,
|
||||||
};
|
};
|
||||||
@ -99,3 +96,33 @@ fn main() -> std::io::Result<()> {
|
|||||||
.bind("127.0.0.1:8080")?
|
.bind("127.0.0.1:8080")?
|
||||||
.run()
|
.run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use actix_web::dev::Service;
|
||||||
|
use actix_web::{test, web, App, http};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_index() -> Result<(), Error> {
|
||||||
|
let app = App::new().route("/", web::post().to(index));
|
||||||
|
let mut app = test::init_service(app);
|
||||||
|
|
||||||
|
let req = test::TestRequest::post()
|
||||||
|
.uri("/")
|
||||||
|
.set_json(&MyObj { name: "my-name".to_owned(), number: 43 })
|
||||||
|
.to_request();
|
||||||
|
let resp = test::block_on(app.call(req)).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(resp.status(), http::StatusCode::OK);
|
||||||
|
|
||||||
|
let response_body = match resp.response().body().as_ref() {
|
||||||
|
Some(actix_web::body::Body::Bytes(bytes)) => bytes,
|
||||||
|
_ => panic!("Response error"),
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(response_body, r##"{"name":"my-name","number":43}"##);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user