mirror of
https://github.com/actix/examples
synced 2024-11-30 17:14:35 +01:00
Updated forms/form to v4. (#498)
This commit is contained in:
parent
0e03496d36
commit
4d82158b0f
@ -2,11 +2,8 @@
|
|||||||
name = "form-example"
|
name = "form-example"
|
||||||
version = "1.0.0"
|
version = "1.0.0"
|
||||||
authors = ["Gorm Casper <gcasper@gmail.com>"]
|
authors = ["Gorm Casper <gcasper@gmail.com>"]
|
||||||
edition = "2018"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "3"
|
actix-web = "4.0.0-beta.21"
|
||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
|
||||||
actix-rt = "1"
|
|
||||||
|
@ -23,9 +23,9 @@ async fn main() -> std::io::Result<()> {
|
|||||||
fn app_config(config: &mut web::ServiceConfig) {
|
fn app_config(config: &mut web::ServiceConfig) {
|
||||||
config.service(
|
config.service(
|
||||||
web::scope("")
|
web::scope("")
|
||||||
.data(AppState {
|
.app_data(web::Data::new(AppState {
|
||||||
foo: "bar".to_string(),
|
foo: "bar".to_string(),
|
||||||
})
|
}))
|
||||||
.service(web::resource("/").route(web::get().to(index)))
|
.service(web::resource("/").route(web::get().to(index)))
|
||||||
.service(web::resource("/post1").route(web::post().to(handle_post_1)))
|
.service(web::resource("/post1").route(web::post().to(handle_post_1)))
|
||||||
.service(web::resource("/post2").route(web::post().to(handle_post_2)))
|
.service(web::resource("/post2").route(web::post().to(handle_post_2)))
|
||||||
@ -55,11 +55,11 @@ async fn handle_post_1(params: web::Form<MyParams>) -> Result<HttpResponse> {
|
|||||||
async fn handle_post_2(
|
async fn handle_post_2(
|
||||||
state: web::Data<AppState>,
|
state: web::Data<AppState>,
|
||||||
params: web::Form<MyParams>,
|
params: web::Form<MyParams>,
|
||||||
) -> HttpResponse {
|
) -> Result<HttpResponse> {
|
||||||
HttpResponse::Ok().content_type("text/plain").body(format!(
|
Ok(HttpResponse::Ok().content_type("text/plain").body(format!(
|
||||||
"Your name is {}, and in AppState I have foo: {}",
|
"Your name is {}, and in AppState I have foo: {}",
|
||||||
params.name, state.foo
|
params.name, state.foo
|
||||||
))
|
)))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Request and POST Params
|
/// Request and POST Params
|
||||||
@ -74,33 +74,23 @@ async fn handle_post_3(req: HttpRequest, params: web::Form<MyParams>) -> impl Re
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use actix_web::body::to_bytes;
|
||||||
use actix_web::body::{Body, ResponseBody};
|
use actix_web::dev::{Service, ServiceResponse};
|
||||||
use actix_web::dev::{HttpResponseBuilder, Service, ServiceResponse};
|
use actix_web::http::{header::HeaderValue, header::CONTENT_TYPE, StatusCode};
|
||||||
use actix_web::http::{header::CONTENT_TYPE, HeaderValue, StatusCode};
|
|
||||||
use actix_web::test::{self, TestRequest};
|
use actix_web::test::{self, TestRequest};
|
||||||
use actix_web::web::Form;
|
use actix_web::web::{Bytes, Form};
|
||||||
|
|
||||||
trait BodyTest {
|
trait BodyTest {
|
||||||
fn as_str(&self) -> &str;
|
fn as_str(&self) -> &str;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BodyTest for ResponseBody<Body> {
|
impl BodyTest for Bytes {
|
||||||
fn as_str(&self) -> &str {
|
fn as_str(&self) -> &str {
|
||||||
match self {
|
std::str::from_utf8(self).unwrap()
|
||||||
ResponseBody::Body(ref b) => match b {
|
|
||||||
Body::Bytes(ref by) => std::str::from_utf8(by).unwrap(),
|
|
||||||
_ => panic!(),
|
|
||||||
},
|
|
||||||
ResponseBody::Other(ref b) => match b {
|
|
||||||
Body::Bytes(ref by) => std::str::from_utf8(by).unwrap(),
|
|
||||||
_ => panic!(),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_web::test]
|
||||||
async fn handle_post_1_unit_test() {
|
async fn handle_post_1_unit_test() {
|
||||||
let params = Form(MyParams {
|
let params = Form(MyParams {
|
||||||
name: "John".to_string(),
|
name: "John".to_string(),
|
||||||
@ -112,12 +102,14 @@ mod tests {
|
|||||||
resp.headers().get(CONTENT_TYPE).unwrap(),
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
||||||
HeaderValue::from_static("text/plain")
|
HeaderValue::from_static("text/plain")
|
||||||
);
|
);
|
||||||
assert_eq!(resp.body().as_str(), "Your name is John");
|
|
||||||
|
let body = to_bytes(resp.into_body()).await.unwrap();
|
||||||
|
assert_eq!(body.as_str(), "Your name is John");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_web::test]
|
||||||
async fn handle_post_1_integration_test() {
|
async fn handle_post_1_integration_test() {
|
||||||
let mut app = test::init_service(App::new().configure(app_config)).await;
|
let app = test::init_service(App::new().configure(app_config)).await;
|
||||||
let req = test::TestRequest::post()
|
let req = test::TestRequest::post()
|
||||||
.uri("/post1")
|
.uri("/post1")
|
||||||
.set_form(&MyParams {
|
.set_form(&MyParams {
|
||||||
@ -131,10 +123,11 @@ mod tests {
|
|||||||
resp.headers().get(CONTENT_TYPE).unwrap(),
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
||||||
HeaderValue::from_static("text/plain")
|
HeaderValue::from_static("text/plain")
|
||||||
);
|
);
|
||||||
assert_eq!(resp.response().body().as_str(), "Your name is John");
|
let body = to_bytes(resp.into_body()).await.unwrap();
|
||||||
|
assert_eq!(body.as_str(), "Your name is John");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_web::test]
|
||||||
async fn handle_post_2_unit_test() {
|
async fn handle_post_2_unit_test() {
|
||||||
let state = TestRequest::default()
|
let state = TestRequest::default()
|
||||||
.data(AppState {
|
.data(AppState {
|
||||||
@ -145,22 +138,23 @@ mod tests {
|
|||||||
let params = Form(MyParams {
|
let params = Form(MyParams {
|
||||||
name: "John".to_string(),
|
name: "John".to_string(),
|
||||||
});
|
});
|
||||||
let resp = handle_post_2(data.clone(), params).await;
|
let resp = handle_post_2(data.clone(), params).await.unwrap();
|
||||||
|
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get(CONTENT_TYPE).unwrap(),
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
||||||
HeaderValue::from_static("text/plain")
|
HeaderValue::from_static("text/plain")
|
||||||
);
|
);
|
||||||
|
let body = to_bytes(resp.into_body()).await.unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.body().as_str(),
|
body.as_str(),
|
||||||
"Your name is John, and in AppState I have foo: bar"
|
"Your name is John, and in AppState I have foo: bar"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_web::test]
|
||||||
async fn handle_post_2_integration_test() {
|
async fn handle_post_2_integration_test() {
|
||||||
let mut app = test::init_service(App::new().configure(app_config)).await;
|
let app = test::init_service(App::new().configure(app_config)).await;
|
||||||
let req = test::TestRequest::post()
|
let req = test::TestRequest::post()
|
||||||
.uri("/post2")
|
.uri("/post2")
|
||||||
.set_form(&MyParams {
|
.set_form(&MyParams {
|
||||||
@ -174,37 +168,37 @@ mod tests {
|
|||||||
resp.headers().get(CONTENT_TYPE).unwrap(),
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
||||||
HeaderValue::from_static("text/plain")
|
HeaderValue::from_static("text/plain")
|
||||||
);
|
);
|
||||||
|
let resp = resp.into_parts().1;
|
||||||
|
let body = to_bytes(resp.into_body()).await.unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.response().body().as_str(),
|
body.as_str(),
|
||||||
"Your name is John, and in AppState I have foo: bar"
|
"Your name is John, and in AppState I have foo: bar"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_web::test]
|
||||||
async fn handle_post_3_unit_test() {
|
async fn handle_post_3_unit_test() {
|
||||||
let req = TestRequest::default().to_http_request();
|
let req = TestRequest::default().to_http_request();
|
||||||
let params = Form(MyParams {
|
let params = Form(MyParams {
|
||||||
name: "John".to_string(),
|
name: "John".to_string(),
|
||||||
});
|
});
|
||||||
let result = handle_post_3(req.clone(), params).await;
|
let result = handle_post_3(req.clone(), params).await;
|
||||||
let resp = match result.respond_to(&req).await {
|
let resp = result.respond_to(&req);
|
||||||
Ok(t) => t,
|
|
||||||
Err(_) => {
|
|
||||||
HttpResponseBuilder::new(StatusCode::INTERNAL_SERVER_ERROR).finish()
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get(CONTENT_TYPE).unwrap(),
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
||||||
HeaderValue::from_static("text/plain")
|
HeaderValue::from_static("text/plain")
|
||||||
);
|
);
|
||||||
assert_eq!(resp.body().as_str(), "Your name is John");
|
let body = match to_bytes(resp.into_body()).await {
|
||||||
|
Ok(x) => x,
|
||||||
|
_ => panic!(),
|
||||||
|
};
|
||||||
|
assert_eq!(body.as_str(), "Your name is John");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_web::test]
|
||||||
async fn handle_post_3_integration_test() {
|
async fn handle_post_3_integration_test() {
|
||||||
let mut app = test::init_service(App::new().configure(app_config)).await;
|
let app = test::init_service(App::new().configure(app_config)).await;
|
||||||
let req = test::TestRequest::post()
|
let req = test::TestRequest::post()
|
||||||
.uri("/post3")
|
.uri("/post3")
|
||||||
.set_form(&MyParams {
|
.set_form(&MyParams {
|
||||||
@ -218,6 +212,8 @@ mod tests {
|
|||||||
resp.headers().get(CONTENT_TYPE).unwrap(),
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
||||||
HeaderValue::from_static("text/plain")
|
HeaderValue::from_static("text/plain")
|
||||||
);
|
);
|
||||||
assert_eq!(resp.response().body().as_str(), "Your name is John");
|
|
||||||
|
let body = to_bytes(resp.into_body()).await.unwrap();
|
||||||
|
assert_eq!(body.as_str(), "Your name is John");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user