1
0
mirror of https://github.com/actix/examples synced 2025-01-22 22:05:57 +01:00

Merge pull request #110 from Dowwie/master

added unit test example to async_ex2
This commit is contained in:
Darin 2019-04-10 15:20:52 -04:00 committed by GitHub
commit 173d65f285
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 1 deletions

View File

@ -18,3 +18,4 @@ serde_json = "1.0.39"
time = "0.1.42"
validator = "0.8.0"
validator_derive = "0.8.0"
actix-service = "0.3.6"

View File

@ -23,4 +23,35 @@ pub fn get_product_detail(id: web::Path<String>)
pub fn remove_product(id: web::Path<String>)
-> impl Future<Item = HttpResponse, Error = Error> {
fut_ok(HttpResponse::Ok().finish())
}
}
#[cfg(test)]
mod tests {
use super::*;
use actix_service::Service;
use actix_web::{test, HttpResponse, HttpRequest, App,
http::{header, StatusCode}, web};
use crate::appconfig::config_app;
#[test]
fn test_add_product() {
let mut app = test::init_service(
App::new()
.configure(config_app)
);
let payload = r#"{"id":12345,"product_type":"fancy","name":"test"}"#.as_bytes();
let req = test::TestRequest::post()
.uri("/products")
.header(header::CONTENT_TYPE, "application/json")
.set_payload(payload)
.to_request();
let resp = test::block_on(app.call(req)).unwrap();
assert_eq!(resp.status(), StatusCode::OK);
}
}