mirror of https://github.com/fafhrd91/actix-web
add route::service tests
This commit is contained in:
parent
b4da6585be
commit
1a982042c8
|
@ -113,7 +113,6 @@ flate2 = "1.0.13"
|
||||||
zstd = "0.7"
|
zstd = "0.7"
|
||||||
rand = "0.8"
|
rand = "0.8"
|
||||||
rcgen = "0.8"
|
rcgen = "0.8"
|
||||||
serde_derive = "1.0"
|
|
||||||
tls-openssl = { package = "openssl", version = "0.10.9" }
|
tls-openssl = { package = "openssl", version = "0.10.9" }
|
||||||
tls-rustls = { package = "rustls", version = "0.19.0" }
|
tls-rustls = { package = "rustls", version = "0.19.0" }
|
||||||
|
|
||||||
|
|
|
@ -376,7 +376,7 @@ mod m {
|
||||||
mod tests {
|
mod tests {
|
||||||
use actix_http::http::header;
|
use actix_http::http::header;
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use serde_derive::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::test::TestRequest;
|
use crate::test::TestRequest;
|
||||||
|
|
68
src/route.rs
68
src/route.rs
|
@ -240,9 +240,12 @@ mod tests {
|
||||||
|
|
||||||
use actix_rt::time::sleep;
|
use actix_rt::time::sleep;
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use serde_derive::Serialize;
|
use futures_core::future::LocalBoxFuture;
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
use crate::http::{Method, StatusCode};
|
use crate::dev::{always_ready, fn_factory, fn_service, Service};
|
||||||
|
use crate::http::{header, Method, StatusCode};
|
||||||
|
use crate::service::{ServiceRequest, ServiceResponse};
|
||||||
use crate::test::{call_service, init_service, read_body, TestRequest};
|
use crate::test::{call_service, init_service, read_body, TestRequest};
|
||||||
use crate::{error, web, App, HttpResponse};
|
use crate::{error, web, App, HttpResponse};
|
||||||
|
|
||||||
|
@ -316,4 +319,65 @@ mod tests {
|
||||||
let body = read_body(resp).await;
|
let body = read_body(resp).await;
|
||||||
assert_eq!(body, Bytes::from_static(b"{\"name\":\"test\"}"));
|
assert_eq!(body, Bytes::from_static(b"{\"name\":\"test\"}"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_service_handler() {
|
||||||
|
struct HelloWorld;
|
||||||
|
|
||||||
|
impl Service<ServiceRequest> for HelloWorld {
|
||||||
|
type Response = ServiceResponse;
|
||||||
|
type Error = crate::Error;
|
||||||
|
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
|
||||||
|
|
||||||
|
always_ready!();
|
||||||
|
|
||||||
|
fn call(&self, req: ServiceRequest) -> Self::Future {
|
||||||
|
let (req, _) = req.into_parts();
|
||||||
|
|
||||||
|
let res = HttpResponse::Ok()
|
||||||
|
.insert_header(header::ContentType::plaintext())
|
||||||
|
.body("Hello world!");
|
||||||
|
|
||||||
|
Box::pin(async move { Ok(ServiceResponse::new(req, res)) })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let srv = init_service(
|
||||||
|
App::new()
|
||||||
|
.route(
|
||||||
|
"/hello",
|
||||||
|
web::get().service(fn_factory(|| async { Ok(HelloWorld) })),
|
||||||
|
)
|
||||||
|
.route(
|
||||||
|
"/bye",
|
||||||
|
web::get().service(fn_factory(|| async {
|
||||||
|
Ok::<_, ()>(fn_service(|req: ServiceRequest| async {
|
||||||
|
let (req, _) = req.into_parts();
|
||||||
|
|
||||||
|
let res = HttpResponse::Ok()
|
||||||
|
.insert_header(header::ContentType::plaintext())
|
||||||
|
.body("Goodbye, and thanks for all the fish!");
|
||||||
|
|
||||||
|
Ok::<_, Infallible>(ServiceResponse::new(req, res))
|
||||||
|
}))
|
||||||
|
})),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let req = TestRequest::get().uri("/hello").to_request();
|
||||||
|
let resp = call_service(&srv, req).await;
|
||||||
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
let body = read_body(resp).await;
|
||||||
|
assert_eq!(body, Bytes::from_static(b"Hello world!"));
|
||||||
|
|
||||||
|
let req = TestRequest::get().uri("/bye").to_request();
|
||||||
|
let resp = call_service(&srv, req).await;
|
||||||
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
let body = read_body(resp).await;
|
||||||
|
assert_eq!(
|
||||||
|
body,
|
||||||
|
Bytes::from_static(b"Goodbye, and thanks for all the fish!")
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue