mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-24 00:21:08 +01:00
Use immutable reference of service state. Update awc dns resolver. (#1905)
This commit is contained in:
parent
20cf0094e5
commit
41bc04b1c4
@ -11,8 +11,12 @@
|
|||||||
* `ServiceRequest::into_parts` and `ServiceRequest::from_parts` would not fail.
|
* `ServiceRequest::into_parts` and `ServiceRequest::from_parts` would not fail.
|
||||||
`ServiceRequest::from_request` would not fail and no payload would be generated [#1893]
|
`ServiceRequest::from_request` would not fail and no payload would be generated [#1893]
|
||||||
* Our `Either` type now uses `Left`/`Right` variants (instead of `A`/`B`) [#1894]
|
* Our `Either` type now uses `Left`/`Right` variants (instead of `A`/`B`) [#1894]
|
||||||
|
* `test::{call_service, read_response, read_response_json, send_request}` take `&Service`
|
||||||
|
in argument [#1905]
|
||||||
|
* `App::wrap_fn`, `Resource::wrap_fn` and `Scope::wrap_fn` would give `&Service` in closure
|
||||||
|
argument [#1905]
|
||||||
* `web::block` accept any closure that has an output bound to `Send` and `'static`. [#1957]
|
* `web::block` accept any closure that has an output bound to `Send` and `'static`. [#1957]
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
* Multiple calls `App::data` with the same type now keeps the latest call's data. [#1906]
|
* Multiple calls `App::data` with the same type now keeps the latest call's data. [#1906]
|
||||||
|
|
||||||
@ -28,6 +32,7 @@
|
|||||||
[#1893]: https://github.com/actix/actix-web/pull/1893
|
[#1893]: https://github.com/actix/actix-web/pull/1893
|
||||||
[#1894]: https://github.com/actix/actix-web/pull/1894
|
[#1894]: https://github.com/actix/actix-web/pull/1894
|
||||||
[#1869]: https://github.com/actix/actix-web/pull/1869
|
[#1869]: https://github.com/actix/actix-web/pull/1869
|
||||||
|
[#1905]: https://github.com/actix/actix-web/pull/1905
|
||||||
[#1906]: https://github.com/actix/actix-web/pull/1906
|
[#1906]: https://github.com/actix/actix-web/pull/1906
|
||||||
[#1957]: https://github.com/actix/actix-web/pull/1957
|
[#1957]: https://github.com/actix/actix-web/pull/1957
|
||||||
|
|
||||||
|
16
Cargo.toml
16
Cargo.toml
@ -74,13 +74,13 @@ required-features = ["rustls"]
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-codec = "0.4.0-beta.1"
|
actix-codec = "0.4.0-beta.1"
|
||||||
actix-macros = "=0.2.0-beta.1"
|
actix-macros = "0.2.0"
|
||||||
actix-router = "0.2.4"
|
actix-router = "0.2.7"
|
||||||
actix-rt = "=2.0.0-beta.2"
|
actix-rt = "2"
|
||||||
actix-server = "2.0.0-beta.2"
|
actix-server = "2.0.0-beta.3"
|
||||||
actix-service = "=2.0.0-beta.3"
|
actix-service = "2.0.0-beta.4"
|
||||||
actix-utils = "3.0.0-beta.1"
|
actix-utils = "3.0.0-beta.2"
|
||||||
actix-tls = { version = "3.0.0-beta.2", default-features = false, optional = true }
|
actix-tls = { version = "3.0.0-beta.3", default-features = false, optional = true }
|
||||||
|
|
||||||
actix-web-codegen = "0.4.0"
|
actix-web-codegen = "0.4.0"
|
||||||
actix-http = "3.0.0-beta.1"
|
actix-http = "3.0.0-beta.1"
|
||||||
@ -108,7 +108,7 @@ rust-tls = { package = "rustls", version = "0.19.0", optional = true }
|
|||||||
smallvec = "1.6"
|
smallvec = "1.6"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix = "0.11.0-beta.1"
|
actix = { version = "0.11.0-beta.2", default-features = false }
|
||||||
actix-http = { version = "3.0.0-beta.1", features = ["actors"] }
|
actix-http = { version = "3.0.0-beta.1", features = ["actors"] }
|
||||||
rand = "0.8"
|
rand = "0.8"
|
||||||
env_logger = "0.8"
|
env_logger = "0.8"
|
||||||
|
@ -18,7 +18,7 @@ path = "src/lib.rs"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = { version = "4.0.0-beta.1", default-features = false }
|
actix-web = { version = "4.0.0-beta.1", default-features = false }
|
||||||
actix-service = "2.0.0-beta.3"
|
actix-service = "2.0.0-beta.4"
|
||||||
bitflags = "1"
|
bitflags = "1"
|
||||||
bytes = "1"
|
bytes = "1"
|
||||||
futures-core = { version = "0.3.7", default-features = false }
|
futures-core = { version = "0.3.7", default-features = false }
|
||||||
@ -31,5 +31,5 @@ percent-encoding = "2.1"
|
|||||||
v_htmlescape = "0.12"
|
v_htmlescape = "0.12"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-rt = "=2.0.0-beta.2"
|
actix-rt = "2"
|
||||||
actix-web = "4.0.0-beta.1"
|
actix-web = "4.0.0-beta.1"
|
||||||
|
@ -73,7 +73,8 @@ mod tests {
|
|||||||
},
|
},
|
||||||
middleware::Compress,
|
middleware::Compress,
|
||||||
test::{self, TestRequest},
|
test::{self, TestRequest},
|
||||||
web, App, HttpResponse, Responder,
|
web::{self, Bytes},
|
||||||
|
App, HttpResponse, Responder,
|
||||||
};
|
};
|
||||||
use futures_util::future::ok;
|
use futures_util::future::ok;
|
||||||
|
|
||||||
@ -365,7 +366,7 @@ mod tests {
|
|||||||
DispositionType::Attachment
|
DispositionType::Attachment
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut srv = test::init_service(
|
let srv = test::init_service(
|
||||||
App::new().service(
|
App::new().service(
|
||||||
Files::new("/", ".")
|
Files::new("/", ".")
|
||||||
.mime_override(all_attachment)
|
.mime_override(all_attachment)
|
||||||
@ -375,7 +376,7 @@ mod tests {
|
|||||||
.await;
|
.await;
|
||||||
|
|
||||||
let request = TestRequest::get().uri("/").to_request();
|
let request = TestRequest::get().uri("/").to_request();
|
||||||
let response = test::call_service(&mut srv, request).await;
|
let response = test::call_service(&srv, request).await;
|
||||||
assert_eq!(response.status(), StatusCode::OK);
|
assert_eq!(response.status(), StatusCode::OK);
|
||||||
|
|
||||||
let content_disposition = response
|
let content_disposition = response
|
||||||
@ -390,7 +391,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_named_file_ranges_status_code() {
|
async fn test_named_file_ranges_status_code() {
|
||||||
let mut srv = test::init_service(
|
let srv = test::init_service(
|
||||||
App::new().service(Files::new("/test", ".").index_file("Cargo.toml")),
|
App::new().service(Files::new("/test", ".").index_file("Cargo.toml")),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
@ -400,7 +401,7 @@ mod tests {
|
|||||||
.uri("/t%65st/Cargo.toml")
|
.uri("/t%65st/Cargo.toml")
|
||||||
.insert_header((header::RANGE, "bytes=10-20"))
|
.insert_header((header::RANGE, "bytes=10-20"))
|
||||||
.to_request();
|
.to_request();
|
||||||
let response = test::call_service(&mut srv, request).await;
|
let response = test::call_service(&srv, request).await;
|
||||||
assert_eq!(response.status(), StatusCode::PARTIAL_CONTENT);
|
assert_eq!(response.status(), StatusCode::PARTIAL_CONTENT);
|
||||||
|
|
||||||
// Invalid range header
|
// Invalid range header
|
||||||
@ -408,7 +409,7 @@ mod tests {
|
|||||||
.uri("/t%65st/Cargo.toml")
|
.uri("/t%65st/Cargo.toml")
|
||||||
.insert_header((header::RANGE, "bytes=1-0"))
|
.insert_header((header::RANGE, "bytes=1-0"))
|
||||||
.to_request();
|
.to_request();
|
||||||
let response = test::call_service(&mut srv, request).await;
|
let response = test::call_service(&srv, request).await;
|
||||||
|
|
||||||
assert_eq!(response.status(), StatusCode::RANGE_NOT_SATISFIABLE);
|
assert_eq!(response.status(), StatusCode::RANGE_NOT_SATISFIABLE);
|
||||||
}
|
}
|
||||||
@ -495,14 +496,14 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_static_files_with_spaces() {
|
async fn test_static_files_with_spaces() {
|
||||||
let mut srv = test::init_service(
|
let srv = test::init_service(
|
||||||
App::new().service(Files::new("/", ".").index_file("Cargo.toml")),
|
App::new().service(Files::new("/", ".").index_file("Cargo.toml")),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
let request = TestRequest::get()
|
let request = TestRequest::get()
|
||||||
.uri("/tests/test%20space.binary")
|
.uri("/tests/test%20space.binary")
|
||||||
.to_request();
|
.to_request();
|
||||||
let response = test::call_service(&mut srv, request).await;
|
let response = test::call_service(&srv, request).await;
|
||||||
assert_eq!(response.status(), StatusCode::OK);
|
assert_eq!(response.status(), StatusCode::OK);
|
||||||
|
|
||||||
let bytes = test::read_body(response).await;
|
let bytes = test::read_body(response).await;
|
||||||
@ -512,28 +513,28 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_files_not_allowed() {
|
async fn test_files_not_allowed() {
|
||||||
let mut srv = test::init_service(App::new().service(Files::new("/", "."))).await;
|
let srv = test::init_service(App::new().service(Files::new("/", "."))).await;
|
||||||
|
|
||||||
let req = TestRequest::default()
|
let req = TestRequest::default()
|
||||||
.uri("/Cargo.toml")
|
.uri("/Cargo.toml")
|
||||||
.method(Method::POST)
|
.method(Method::POST)
|
||||||
.to_request();
|
.to_request();
|
||||||
|
|
||||||
let resp = test::call_service(&mut srv, req).await;
|
let resp = test::call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);
|
assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);
|
||||||
|
|
||||||
let mut srv = test::init_service(App::new().service(Files::new("/", "."))).await;
|
let srv = test::init_service(App::new().service(Files::new("/", "."))).await;
|
||||||
let req = TestRequest::default()
|
let req = TestRequest::default()
|
||||||
.method(Method::PUT)
|
.method(Method::PUT)
|
||||||
.uri("/Cargo.toml")
|
.uri("/Cargo.toml")
|
||||||
.to_request();
|
.to_request();
|
||||||
let resp = test::call_service(&mut srv, req).await;
|
let resp = test::call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);
|
assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_files_guards() {
|
async fn test_files_guards() {
|
||||||
let mut srv = test::init_service(
|
let srv = test::init_service(
|
||||||
App::new().service(Files::new("/", ".").use_guards(guard::Post())),
|
App::new().service(Files::new("/", ".").use_guards(guard::Post())),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
@ -543,13 +544,13 @@ mod tests {
|
|||||||
.method(Method::POST)
|
.method(Method::POST)
|
||||||
.to_request();
|
.to_request();
|
||||||
|
|
||||||
let resp = test::call_service(&mut srv, req).await;
|
let resp = test::call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_named_file_content_encoding() {
|
async fn test_named_file_content_encoding() {
|
||||||
let mut srv = test::init_service(App::new().wrap(Compress::default()).service(
|
let srv = test::init_service(App::new().wrap(Compress::default()).service(
|
||||||
web::resource("/").to(|| async {
|
web::resource("/").to(|| async {
|
||||||
NamedFile::open("Cargo.toml")
|
NamedFile::open("Cargo.toml")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
@ -562,14 +563,14 @@ mod tests {
|
|||||||
.uri("/")
|
.uri("/")
|
||||||
.insert_header((header::ACCEPT_ENCODING, "gzip"))
|
.insert_header((header::ACCEPT_ENCODING, "gzip"))
|
||||||
.to_request();
|
.to_request();
|
||||||
let res = test::call_service(&mut srv, request).await;
|
let res = test::call_service(&srv, request).await;
|
||||||
assert_eq!(res.status(), StatusCode::OK);
|
assert_eq!(res.status(), StatusCode::OK);
|
||||||
assert!(!res.headers().contains_key(header::CONTENT_ENCODING));
|
assert!(!res.headers().contains_key(header::CONTENT_ENCODING));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_named_file_content_encoding_gzip() {
|
async fn test_named_file_content_encoding_gzip() {
|
||||||
let mut srv = test::init_service(App::new().wrap(Compress::default()).service(
|
let srv = test::init_service(App::new().wrap(Compress::default()).service(
|
||||||
web::resource("/").to(|| async {
|
web::resource("/").to(|| async {
|
||||||
NamedFile::open("Cargo.toml")
|
NamedFile::open("Cargo.toml")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
@ -582,7 +583,7 @@ mod tests {
|
|||||||
.uri("/")
|
.uri("/")
|
||||||
.insert_header((header::ACCEPT_ENCODING, "gzip"))
|
.insert_header((header::ACCEPT_ENCODING, "gzip"))
|
||||||
.to_request();
|
.to_request();
|
||||||
let res = test::call_service(&mut srv, request).await;
|
let res = test::call_service(&srv, request).await;
|
||||||
assert_eq!(res.status(), StatusCode::OK);
|
assert_eq!(res.status(), StatusCode::OK);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
res.headers()
|
res.headers()
|
||||||
@ -604,27 +605,27 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_static_files() {
|
async fn test_static_files() {
|
||||||
let mut srv = test::init_service(
|
let srv = test::init_service(
|
||||||
App::new().service(Files::new("/", ".").show_files_listing()),
|
App::new().service(Files::new("/", ".").show_files_listing()),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
let req = TestRequest::with_uri("/missing").to_request();
|
let req = TestRequest::with_uri("/missing").to_request();
|
||||||
|
|
||||||
let resp = test::call_service(&mut srv, req).await;
|
let resp = test::call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
||||||
|
|
||||||
let mut srv = test::init_service(App::new().service(Files::new("/", "."))).await;
|
let srv = test::init_service(App::new().service(Files::new("/", "."))).await;
|
||||||
|
|
||||||
let req = TestRequest::default().to_request();
|
let req = TestRequest::default().to_request();
|
||||||
let resp = test::call_service(&mut srv, req).await;
|
let resp = test::call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
||||||
|
|
||||||
let mut srv = test::init_service(
|
let srv = test::init_service(
|
||||||
App::new().service(Files::new("/", ".").show_files_listing()),
|
App::new().service(Files::new("/", ".").show_files_listing()),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
let req = TestRequest::with_uri("/tests").to_request();
|
let req = TestRequest::with_uri("/tests").to_request();
|
||||||
let resp = test::call_service(&mut srv, req).await;
|
let resp = test::call_service(&srv, req).await;
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
||||||
"text/html; charset=utf-8"
|
"text/html; charset=utf-8"
|
||||||
@ -637,16 +638,16 @@ mod tests {
|
|||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_redirect_to_slash_directory() {
|
async fn test_redirect_to_slash_directory() {
|
||||||
// should not redirect if no index
|
// should not redirect if no index
|
||||||
let mut srv = test::init_service(
|
let srv = test::init_service(
|
||||||
App::new().service(Files::new("/", ".").redirect_to_slash_directory()),
|
App::new().service(Files::new("/", ".").redirect_to_slash_directory()),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
let req = TestRequest::with_uri("/tests").to_request();
|
let req = TestRequest::with_uri("/tests").to_request();
|
||||||
let resp = test::call_service(&mut srv, req).await;
|
let resp = test::call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
||||||
|
|
||||||
// should redirect if index present
|
// should redirect if index present
|
||||||
let mut srv = test::init_service(
|
let srv = test::init_service(
|
||||||
App::new().service(
|
App::new().service(
|
||||||
Files::new("/", ".")
|
Files::new("/", ".")
|
||||||
.index_file("test.png")
|
.index_file("test.png")
|
||||||
@ -655,12 +656,12 @@ mod tests {
|
|||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
let req = TestRequest::with_uri("/tests").to_request();
|
let req = TestRequest::with_uri("/tests").to_request();
|
||||||
let resp = test::call_service(&mut srv, req).await;
|
let resp = test::call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::FOUND);
|
assert_eq!(resp.status(), StatusCode::FOUND);
|
||||||
|
|
||||||
// should not redirect if the path is wrong
|
// should not redirect if the path is wrong
|
||||||
let req = TestRequest::with_uri("/not_existing").to_request();
|
let req = TestRequest::with_uri("/not_existing").to_request();
|
||||||
let resp = test::call_service(&mut srv, req).await;
|
let resp = test::call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -672,7 +673,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_default_handler_file_missing() {
|
async fn test_default_handler_file_missing() {
|
||||||
let mut st = Files::new("/", ".")
|
let st = Files::new("/", ".")
|
||||||
.default_handler(|req: ServiceRequest| {
|
.default_handler(|req: ServiceRequest| {
|
||||||
ok(req.into_response(HttpResponse::Ok().body("default content")))
|
ok(req.into_response(HttpResponse::Ok().body("default content")))
|
||||||
})
|
})
|
||||||
@ -681,7 +682,7 @@ mod tests {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
let req = TestRequest::with_uri("/missing").to_srv_request();
|
let req = TestRequest::with_uri("/missing").to_srv_request();
|
||||||
|
|
||||||
let resp = test::call_service(&mut st, req).await;
|
let resp = test::call_service(&st, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
let bytes = test::read_body(resp).await;
|
let bytes = test::read_body(resp).await;
|
||||||
assert_eq!(bytes, web::Bytes::from_static(b"default content"));
|
assert_eq!(bytes, web::Bytes::from_static(b"default content"));
|
||||||
@ -750,54 +751,49 @@ mod tests {
|
|||||||
// );
|
// );
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// #[actix_rt::test]
|
#[actix_rt::test]
|
||||||
// fn integration_serve_index() {
|
async fn integration_serve_index() {
|
||||||
// let mut srv = test::TestServer::with_factory(|| {
|
let srv = test::init_service(
|
||||||
// App::new().handler(
|
App::new().service(Files::new("test", ".").index_file("Cargo.toml")),
|
||||||
// "test",
|
)
|
||||||
// Files::new(".").index_file("Cargo.toml"),
|
.await;
|
||||||
// )
|
|
||||||
// });
|
|
||||||
|
|
||||||
// let request = srv.get().uri(srv.url("/test")).finish().unwrap();
|
let req = TestRequest::get().uri("/test").to_request();
|
||||||
// let response = srv.execute(request.send()).unwrap();
|
let res = test::call_service(&srv, req).await;
|
||||||
// assert_eq!(response.status(), StatusCode::OK);
|
assert_eq!(res.status(), StatusCode::OK);
|
||||||
// let bytes = srv.execute(response.body()).unwrap();
|
|
||||||
// let data = Bytes::from(fs::read("Cargo.toml").unwrap());
|
|
||||||
// assert_eq!(bytes, data);
|
|
||||||
|
|
||||||
// let request = srv.get().uri(srv.url("/test/")).finish().unwrap();
|
let bytes = test::read_body(res).await;
|
||||||
// let response = srv.execute(request.send()).unwrap();
|
|
||||||
// assert_eq!(response.status(), StatusCode::OK);
|
|
||||||
// let bytes = srv.execute(response.body()).unwrap();
|
|
||||||
// let data = Bytes::from(fs::read("Cargo.toml").unwrap());
|
|
||||||
// assert_eq!(bytes, data);
|
|
||||||
|
|
||||||
// // nonexistent index file
|
let data = Bytes::from(fs::read("Cargo.toml").unwrap());
|
||||||
// let request = srv.get().uri(srv.url("/test/unknown")).finish().unwrap();
|
assert_eq!(bytes, data);
|
||||||
// let response = srv.execute(request.send()).unwrap();
|
|
||||||
// assert_eq!(response.status(), StatusCode::NOT_FOUND);
|
|
||||||
|
|
||||||
// let request = srv.get().uri(srv.url("/test/unknown/")).finish().unwrap();
|
let req = TestRequest::get().uri("/test/").to_request();
|
||||||
// let response = srv.execute(request.send()).unwrap();
|
let res = test::call_service(&srv, req).await;
|
||||||
// assert_eq!(response.status(), StatusCode::NOT_FOUND);
|
assert_eq!(res.status(), StatusCode::OK);
|
||||||
// }
|
|
||||||
|
|
||||||
// #[actix_rt::test]
|
let bytes = test::read_body(res).await;
|
||||||
// fn integration_percent_encoded() {
|
let data = Bytes::from(fs::read("Cargo.toml").unwrap());
|
||||||
// let mut srv = test::TestServer::with_factory(|| {
|
assert_eq!(bytes, data);
|
||||||
// App::new().handler(
|
|
||||||
// "test",
|
|
||||||
// Files::new(".").index_file("Cargo.toml"),
|
|
||||||
// )
|
|
||||||
// });
|
|
||||||
|
|
||||||
// let request = srv
|
// nonexistent index file
|
||||||
// .get()
|
let req = TestRequest::get().uri("/test/unknown").to_request();
|
||||||
// .uri(srv.url("/test/%43argo.toml"))
|
let res = test::call_service(&srv, req).await;
|
||||||
// .finish()
|
assert_eq!(res.status(), StatusCode::NOT_FOUND);
|
||||||
// .unwrap();
|
|
||||||
// let response = srv.execute(request.send()).unwrap();
|
let req = TestRequest::get().uri("/test/unknown/").to_request();
|
||||||
// assert_eq!(response.status(), StatusCode::OK);
|
let res = test::call_service(&srv, req).await;
|
||||||
// }
|
assert_eq!(res.status(), StatusCode::NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn integration_percent_encoded() {
|
||||||
|
let srv = test::init_service(
|
||||||
|
App::new().service(Files::new("test", ".").index_file("Cargo.toml")),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let req = TestRequest::get().uri("/test/%43argo.toml").to_request();
|
||||||
|
let res = test::call_service(&srv, req).await;
|
||||||
|
assert_eq!(res.status(), StatusCode::OK);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,4 @@
|
|||||||
use std::{
|
use std::{fmt, io, path::PathBuf, rc::Rc, task::Poll};
|
||||||
fmt, io,
|
|
||||||
path::PathBuf,
|
|
||||||
rc::Rc,
|
|
||||||
task::{Context, Poll},
|
|
||||||
};
|
|
||||||
|
|
||||||
use actix_service::Service;
|
use actix_service::Service;
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
@ -40,10 +35,10 @@ type FilesServiceFuture = Either<
|
|||||||
>;
|
>;
|
||||||
|
|
||||||
impl FilesService {
|
impl FilesService {
|
||||||
fn handle_err(&mut self, e: io::Error, req: ServiceRequest) -> FilesServiceFuture {
|
fn handle_err(&self, e: io::Error, req: ServiceRequest) -> FilesServiceFuture {
|
||||||
log::debug!("Failed to handle {}: {}", req.path(), e);
|
log::debug!("Failed to handle {}: {}", req.path(), e);
|
||||||
|
|
||||||
if let Some(ref mut default) = self.default {
|
if let Some(ref default) = self.default {
|
||||||
Either::Right(default.call(req))
|
Either::Right(default.call(req))
|
||||||
} else {
|
} else {
|
||||||
Either::Left(ok(req.error_response(e)))
|
Either::Left(ok(req.error_response(e)))
|
||||||
@ -62,11 +57,9 @@ impl Service<ServiceRequest> for FilesService {
|
|||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = FilesServiceFuture;
|
type Future = FilesServiceFuture;
|
||||||
|
|
||||||
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
actix_service::always_ready!();
|
||||||
Poll::Ready(Ok(()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call(&mut self, req: ServiceRequest) -> Self::Future {
|
fn call(&self, req: ServiceRequest) -> Self::Future {
|
||||||
let is_method_valid = if let Some(guard) = &self.guards {
|
let is_method_valid = if let Some(guard) = &self.guards {
|
||||||
// execute user defined guards
|
// execute user defined guards
|
||||||
(**guard).check(req.head())
|
(**guard).check(req.head())
|
||||||
|
@ -11,11 +11,10 @@ use actix_web::{
|
|||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_utf8_file_contents() {
|
async fn test_utf8_file_contents() {
|
||||||
// use default ISO-8859-1 encoding
|
// use default ISO-8859-1 encoding
|
||||||
let mut srv =
|
let srv = test::init_service(App::new().service(Files::new("/", "./tests"))).await;
|
||||||
test::init_service(App::new().service(Files::new("/", "./tests"))).await;
|
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/utf8.txt").to_request();
|
let req = TestRequest::with_uri("/utf8.txt").to_request();
|
||||||
let res = test::call_service(&mut srv, req).await;
|
let res = test::call_service(&srv, req).await;
|
||||||
|
|
||||||
assert_eq!(res.status(), StatusCode::OK);
|
assert_eq!(res.status(), StatusCode::OK);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
@ -24,13 +23,13 @@ async fn test_utf8_file_contents() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// prefer UTF-8 encoding
|
// prefer UTF-8 encoding
|
||||||
let mut srv = test::init_service(
|
let srv = test::init_service(
|
||||||
App::new().service(Files::new("/", "./tests").prefer_utf8(true)),
|
App::new().service(Files::new("/", "./tests").prefer_utf8(true)),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/utf8.txt").to_request();
|
let req = TestRequest::with_uri("/utf8.txt").to_request();
|
||||||
let res = test::call_service(&mut srv, req).await;
|
let res = test::call_service(&srv, req).await;
|
||||||
|
|
||||||
assert_eq!(res.status(), StatusCode::OK);
|
assert_eq!(res.status(), StatusCode::OK);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
@ -29,12 +29,12 @@ default = []
|
|||||||
openssl = ["open-ssl", "awc/openssl"]
|
openssl = ["open-ssl", "awc/openssl"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-service = "2.0.0-beta.3"
|
actix-service = "2.0.0-beta.4"
|
||||||
actix-codec = "0.4.0-beta.1"
|
actix-codec = "0.4.0-beta.1"
|
||||||
actix-tls = "3.0.0-beta.2"
|
actix-tls = "3.0.0-beta.3"
|
||||||
actix-utils = "3.0.0-beta.1"
|
actix-utils = "3.0.0-beta.2"
|
||||||
actix-rt = "=2.0.0-beta.2"
|
actix-rt = "2"
|
||||||
actix-server = "2.0.0-beta.2"
|
actix-server = "2.0.0-beta.3"
|
||||||
awc = "3.0.0-beta.1"
|
awc = "3.0.0-beta.1"
|
||||||
|
|
||||||
base64 = "0.13"
|
base64 = "0.13"
|
||||||
|
@ -60,7 +60,7 @@ pub async fn test_server_with_addr<F: ServiceFactory<TcpStream>>(
|
|||||||
|
|
||||||
// run server in separate thread
|
// run server in separate thread
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
let sys = System::new("actix-test-server");
|
let sys = System::new();
|
||||||
let local_addr = tcp.local_addr().unwrap();
|
let local_addr = tcp.local_addr().unwrap();
|
||||||
|
|
||||||
let srv = Server::build()
|
let srv = Server::build()
|
||||||
@ -69,7 +69,7 @@ pub async fn test_server_with_addr<F: ServiceFactory<TcpStream>>(
|
|||||||
.disable_signals();
|
.disable_signals();
|
||||||
|
|
||||||
sys.block_on(async {
|
sys.block_on(async {
|
||||||
srv.start();
|
srv.run();
|
||||||
tx.send((System::current(), local_addr)).unwrap();
|
tx.send((System::current(), local_addr)).unwrap();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -106,7 +106,6 @@ pub async fn test_server_with_addr<F: ServiceFactory<TcpStream>>(
|
|||||||
|
|
||||||
Client::builder().connector(connector).finish()
|
Client::builder().connector(connector).finish()
|
||||||
};
|
};
|
||||||
actix_tls::connect::start_default_resolver().await.unwrap();
|
|
||||||
|
|
||||||
TestServer {
|
TestServer {
|
||||||
addr,
|
addr,
|
||||||
|
@ -16,7 +16,9 @@
|
|||||||
* `Extensions::insert` returns Option of replaced item. [#1904]
|
* `Extensions::insert` returns Option of replaced item. [#1904]
|
||||||
* Remove `HttpResponseBuilder::json2()` and make `HttpResponseBuilder::json()` take a value by
|
* Remove `HttpResponseBuilder::json2()` and make `HttpResponseBuilder::json()` take a value by
|
||||||
reference. [#1903]
|
reference. [#1903]
|
||||||
* Simplify `BlockingError` type to a struct. It's only triggered with blocking thread pool is dead. [#1957]
|
* `client::error::ConnectError` Resolver variant contains `Box<dyn std::error::Error>` type [#1905]
|
||||||
|
* `client::ConnectorConfig` default timeout changed to 5 seconds. [#1905]
|
||||||
|
* Simplify `BlockingError` type to a struct. It's only triggered with blocking thread pool is dead. [#1957]
|
||||||
|
|
||||||
### Removed
|
### Removed
|
||||||
* `ResponseBuilder::set`; use `ResponseBuilder::insert_header`. [#1869]
|
* `ResponseBuilder::set`; use `ResponseBuilder::insert_header`. [#1869]
|
||||||
@ -29,6 +31,7 @@
|
|||||||
[#1894]: https://github.com/actix/actix-web/pull/1894
|
[#1894]: https://github.com/actix/actix-web/pull/1894
|
||||||
[#1903]: https://github.com/actix/actix-web/pull/1903
|
[#1903]: https://github.com/actix/actix-web/pull/1903
|
||||||
[#1904]: https://github.com/actix/actix-web/pull/1904
|
[#1904]: https://github.com/actix/actix-web/pull/1904
|
||||||
|
[#1905]: https://github.com/actix/actix-web/pull/1905
|
||||||
[#1912]: https://github.com/actix/actix-web/pull/1912
|
[#1912]: https://github.com/actix/actix-web/pull/1912
|
||||||
[#1957]: https://github.com/actix/actix-web/pull/1957
|
[#1957]: https://github.com/actix/actix-web/pull/1957
|
||||||
|
|
||||||
|
@ -40,12 +40,12 @@ secure-cookies = ["cookie/secure"]
|
|||||||
actors = ["actix"]
|
actors = ["actix"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-service = "2.0.0-beta.3"
|
actix-service = "2.0.0-beta.4"
|
||||||
actix-codec = "0.4.0-beta.1"
|
actix-codec = "0.4.0-beta.1"
|
||||||
actix-utils = "3.0.0-beta.1"
|
actix-utils = "3.0.0-beta.2"
|
||||||
actix-rt = "=2.0.0-beta.2"
|
actix-rt = "2"
|
||||||
actix-tls = "3.0.0-beta.2"
|
actix-tls = "3.0.0-beta.2"
|
||||||
actix = { version = "0.11.0-beta.1", optional = true }
|
actix = { version = "0.11.0-beta.2", default-features = false, optional = true }
|
||||||
|
|
||||||
base64 = "0.13"
|
base64 = "0.13"
|
||||||
bitflags = "1.2"
|
bitflags = "1.2"
|
||||||
@ -55,9 +55,9 @@ cookie = { version = "0.14.1", features = ["percent-encode"] }
|
|||||||
derive_more = "0.99.5"
|
derive_more = "0.99.5"
|
||||||
either = "1.5.3"
|
either = "1.5.3"
|
||||||
encoding_rs = "0.8"
|
encoding_rs = "0.8"
|
||||||
futures-channel = { version = "0.3.7", default-features = false }
|
futures-channel = { version = "0.3.7", default-features = false, features = ["alloc"] }
|
||||||
futures-core = { version = "0.3.7", default-features = false }
|
futures-core = { version = "0.3.7", default-features = false, features = ["alloc"] }
|
||||||
futures-util = { version = "0.3.7", default-features = false, features = ["sink"] }
|
futures-util = { version = "0.3.7", default-features = false, features = ["alloc", "sink"] }
|
||||||
ahash = "0.6"
|
ahash = "0.6"
|
||||||
h2 = "0.3.0"
|
h2 = "0.3.0"
|
||||||
http = "0.2.2"
|
http = "0.2.2"
|
||||||
@ -85,7 +85,7 @@ brotli2 = { version="0.3.2", optional = true }
|
|||||||
flate2 = { version = "1.0.13", optional = true }
|
flate2 = { version = "1.0.13", optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-server = "2.0.0-beta.2"
|
actix-server = "2.0.0-beta.3"
|
||||||
actix-http-test = { version = "3.0.0-beta.1", features = ["openssl"] }
|
actix-http-test = { version = "3.0.0-beta.1", features = ["openssl"] }
|
||||||
actix-tls = { version = "3.0.0-beta.2", features = ["openssl"] }
|
actix-tls = { version = "3.0.0-beta.2", features = ["openssl"] }
|
||||||
criterion = "0.3"
|
criterion = "0.3"
|
||||||
|
@ -18,7 +18,7 @@ pub(crate) struct ConnectorConfig {
|
|||||||
impl Default for ConnectorConfig {
|
impl Default for ConnectorConfig {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
timeout: Duration::from_secs(1),
|
timeout: Duration::from_secs(5),
|
||||||
conn_lifetime: Duration::from_secs(75),
|
conn_lifetime: Duration::from_secs(75),
|
||||||
conn_keep_alive: Duration::from_secs(15),
|
conn_keep_alive: Duration::from_secs(15),
|
||||||
disconnect_timeout: Some(Duration::from_millis(3000)),
|
disconnect_timeout: Some(Duration::from_millis(3000)),
|
||||||
|
@ -5,7 +5,8 @@ use std::{fmt, io, time};
|
|||||||
|
|
||||||
use actix_codec::{AsyncRead, AsyncWrite, Framed, ReadBuf};
|
use actix_codec::{AsyncRead, AsyncWrite, Framed, ReadBuf};
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use futures_util::future::{err, Either, FutureExt, LocalBoxFuture, Ready};
|
use futures_core::future::LocalBoxFuture;
|
||||||
|
use futures_util::future::{err, Either, FutureExt, Ready};
|
||||||
use h2::client::SendRequest;
|
use h2::client::SendRequest;
|
||||||
use pin_project::pin_project;
|
use pin_project::pin_project;
|
||||||
|
|
||||||
|
@ -100,9 +100,9 @@ impl Connector<(), ()> {
|
|||||||
fn build_ssl(protocols: Vec<Vec<u8>>) -> SslConnector {
|
fn build_ssl(protocols: Vec<Vec<u8>>) -> SslConnector {
|
||||||
let mut config = ClientConfig::new();
|
let mut config = ClientConfig::new();
|
||||||
config.set_protocols(&protocols);
|
config.set_protocols(&protocols);
|
||||||
config
|
config.root_store.add_server_trust_anchors(
|
||||||
.root_store
|
&actix_tls::connect::ssl::rustls::TLS_SERVER_ROOTS,
|
||||||
.add_server_trust_anchors(&actix_tls::accept::rustls::TLS_SERVER_ROOTS);
|
);
|
||||||
SslConnector::Rustls(Arc::new(config))
|
SslConnector::Rustls(Arc::new(config))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -392,11 +392,11 @@ mod connect_impl {
|
|||||||
Ready<Result<IoConnection<Io>, ConnectError>>,
|
Ready<Result<IoConnection<Io>, ConnectError>>,
|
||||||
>;
|
>;
|
||||||
|
|
||||||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
self.tcp_pool.poll_ready(cx)
|
self.tcp_pool.poll_ready(cx)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: Connect) -> Self::Future {
|
fn call(&self, req: Connect) -> Self::Future {
|
||||||
match req.uri.scheme_str() {
|
match req.uri.scheme_str() {
|
||||||
Some("https") | Some("wss") => {
|
Some("https") | Some("wss") => {
|
||||||
Either::Right(err(ConnectError::SslIsNotSupported))
|
Either::Right(err(ConnectError::SslIsNotSupported))
|
||||||
@ -460,11 +460,11 @@ mod connect_impl {
|
|||||||
InnerConnectorResponseB<T2, Io1, Io2>,
|
InnerConnectorResponseB<T2, Io1, Io2>,
|
||||||
>;
|
>;
|
||||||
|
|
||||||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
self.tcp_pool.poll_ready(cx)
|
self.tcp_pool.poll_ready(cx)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: Connect) -> Self::Future {
|
fn call(&self, req: Connect) -> Self::Future {
|
||||||
match req.uri.scheme_str() {
|
match req.uri.scheme_str() {
|
||||||
Some("https") | Some("wss") => Either::Right(InnerConnectorResponseB {
|
Some("https") | Some("wss") => Either::Right(InnerConnectorResponseB {
|
||||||
fut: self.ssl_pool.call(req),
|
fut: self.ssl_pool.call(req),
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
use actix_tls::connect::resolver::ResolveError;
|
|
||||||
use derive_more::{Display, From};
|
use derive_more::{Display, From};
|
||||||
|
|
||||||
#[cfg(feature = "openssl")]
|
#[cfg(feature = "openssl")]
|
||||||
@ -23,7 +22,7 @@ pub enum ConnectError {
|
|||||||
|
|
||||||
/// Failed to resolve the hostname
|
/// Failed to resolve the hostname
|
||||||
#[display(fmt = "Failed resolving hostname: {}", _0)]
|
#[display(fmt = "Failed resolving hostname: {}", _0)]
|
||||||
Resolver(ResolveError),
|
Resolver(Box<dyn std::error::Error>),
|
||||||
|
|
||||||
/// No dns records
|
/// No dns records
|
||||||
#[display(fmt = "No dns records found for the input")]
|
#[display(fmt = "No dns records found for the input")]
|
||||||
|
@ -13,7 +13,8 @@ use actix_utils::task::LocalWaker;
|
|||||||
use ahash::AHashMap;
|
use ahash::AHashMap;
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use futures_channel::oneshot;
|
use futures_channel::oneshot;
|
||||||
use futures_util::future::{poll_fn, FutureExt, LocalBoxFuture};
|
use futures_core::future::LocalBoxFuture;
|
||||||
|
use futures_util::future::{poll_fn, FutureExt};
|
||||||
use h2::client::{Connection, SendRequest};
|
use h2::client::{Connection, SendRequest};
|
||||||
use http::uri::Authority;
|
use http::uri::Authority;
|
||||||
use indexmap::IndexSet;
|
use indexmap::IndexSet;
|
||||||
@ -45,7 +46,7 @@ impl From<Authority> for Key {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Connections pool
|
/// Connections pool
|
||||||
pub(crate) struct ConnectionPool<T, Io: 'static>(Rc<RefCell<T>>, Rc<RefCell<Inner<Io>>>);
|
pub(crate) struct ConnectionPool<T, Io: 'static>(Rc<T>, Rc<RefCell<Inner<Io>>>);
|
||||||
|
|
||||||
impl<T, Io> ConnectionPool<T, Io>
|
impl<T, Io> ConnectionPool<T, Io>
|
||||||
where
|
where
|
||||||
@ -53,7 +54,7 @@ where
|
|||||||
T: Service<Connect, Response = (Io, Protocol), Error = ConnectError> + 'static,
|
T: Service<Connect, Response = (Io, Protocol), Error = ConnectError> + 'static,
|
||||||
{
|
{
|
||||||
pub(crate) fn new(connector: T, config: ConnectorConfig) -> Self {
|
pub(crate) fn new(connector: T, config: ConnectorConfig) -> Self {
|
||||||
let connector_rc = Rc::new(RefCell::new(connector));
|
let connector_rc = Rc::new(connector);
|
||||||
let inner_rc = Rc::new(RefCell::new(Inner {
|
let inner_rc = Rc::new(RefCell::new(Inner {
|
||||||
config,
|
config,
|
||||||
acquired: 0,
|
acquired: 0,
|
||||||
@ -98,12 +99,12 @@ where
|
|||||||
type Error = ConnectError;
|
type Error = ConnectError;
|
||||||
type Future = LocalBoxFuture<'static, Result<IoConnection<Io>, ConnectError>>;
|
type Future = LocalBoxFuture<'static, Result<IoConnection<Io>, ConnectError>>;
|
||||||
|
|
||||||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
self.0.poll_ready(cx)
|
self.0.poll_ready(cx)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: Connect) -> Self::Future {
|
fn call(&self, req: Connect) -> Self::Future {
|
||||||
let mut connector = self.0.clone();
|
let connector = self.0.clone();
|
||||||
let inner = self.1.clone();
|
let inner = self.1.clone();
|
||||||
|
|
||||||
let fut = async move {
|
let fut = async move {
|
||||||
@ -325,7 +326,7 @@ where
|
|||||||
{
|
{
|
||||||
if let Some(timeout) = self.config.disconnect_timeout {
|
if let Some(timeout) = self.config.disconnect_timeout {
|
||||||
if let ConnectionType::H1(io) = conn.io {
|
if let ConnectionType::H1(io) = conn.io {
|
||||||
actix_rt::spawn(CloseConnection::new(io, timeout))
|
actix_rt::spawn(CloseConnection::new(io, timeout));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -340,7 +341,7 @@ where
|
|||||||
if let ConnectionType::H1(io) = io {
|
if let ConnectionType::H1(io) = io {
|
||||||
actix_rt::spawn(CloseConnection::new(
|
actix_rt::spawn(CloseConnection::new(
|
||||||
io, timeout,
|
io, timeout,
|
||||||
))
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
@ -372,7 +373,7 @@ where
|
|||||||
self.acquired -= 1;
|
self.acquired -= 1;
|
||||||
if let Some(timeout) = self.config.disconnect_timeout {
|
if let Some(timeout) = self.config.disconnect_timeout {
|
||||||
if let ConnectionType::H1(io) = io {
|
if let ConnectionType::H1(io) = io {
|
||||||
actix_rt::spawn(CloseConnection::new(io, timeout))
|
actix_rt::spawn(CloseConnection::new(io, timeout));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.check_availability();
|
self.check_availability();
|
||||||
@ -428,7 +429,7 @@ struct ConnectorPoolSupport<T, Io>
|
|||||||
where
|
where
|
||||||
Io: AsyncRead + AsyncWrite + Unpin + 'static,
|
Io: AsyncRead + AsyncWrite + Unpin + 'static,
|
||||||
{
|
{
|
||||||
connector: T,
|
connector: Rc<T>,
|
||||||
inner: Rc<RefCell<Inner<Io>>>,
|
inner: Rc<RefCell<Inner<Io>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -535,7 +536,7 @@ where
|
|||||||
rx: Some(rx),
|
rx: Some(rx),
|
||||||
inner: Some(inner),
|
inner: Some(inner),
|
||||||
config,
|
config,
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
use std::{
|
use std::{
|
||||||
cell::RefCell,
|
|
||||||
collections::VecDeque,
|
collections::VecDeque,
|
||||||
fmt,
|
fmt,
|
||||||
future::Future,
|
future::Future,
|
||||||
@ -91,7 +90,7 @@ where
|
|||||||
U: Service<(Request, Framed<T, Codec>), Response = ()>,
|
U: Service<(Request, Framed<T, Codec>), Response = ()>,
|
||||||
U::Error: fmt::Display,
|
U::Error: fmt::Display,
|
||||||
{
|
{
|
||||||
flow: Rc<RefCell<HttpFlow<S, X, U>>>,
|
flow: Rc<HttpFlow<S, X, U>>,
|
||||||
on_connect_data: OnConnectData,
|
on_connect_data: OnConnectData,
|
||||||
flags: Flags,
|
flags: Flags,
|
||||||
peer_addr: Option<net::SocketAddr>,
|
peer_addr: Option<net::SocketAddr>,
|
||||||
@ -177,7 +176,7 @@ where
|
|||||||
pub(crate) fn new(
|
pub(crate) fn new(
|
||||||
stream: T,
|
stream: T,
|
||||||
config: ServiceConfig,
|
config: ServiceConfig,
|
||||||
services: Rc<RefCell<HttpFlow<S, X, U>>>,
|
services: Rc<HttpFlow<S, X, U>>,
|
||||||
on_connect_data: OnConnectData,
|
on_connect_data: OnConnectData,
|
||||||
peer_addr: Option<net::SocketAddr>,
|
peer_addr: Option<net::SocketAddr>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
@ -200,7 +199,7 @@ where
|
|||||||
config: ServiceConfig,
|
config: ServiceConfig,
|
||||||
read_buf: BytesMut,
|
read_buf: BytesMut,
|
||||||
timeout: Option<Sleep>,
|
timeout: Option<Sleep>,
|
||||||
services: Rc<RefCell<HttpFlow<S, X, U>>>,
|
services: Rc<HttpFlow<S, X, U>>,
|
||||||
on_connect_data: OnConnectData,
|
on_connect_data: OnConnectData,
|
||||||
peer_addr: Option<net::SocketAddr>,
|
peer_addr: Option<net::SocketAddr>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
@ -377,7 +376,7 @@ where
|
|||||||
Poll::Ready(Ok(req)) => {
|
Poll::Ready(Ok(req)) => {
|
||||||
self.as_mut().send_continue();
|
self.as_mut().send_continue();
|
||||||
this = self.as_mut().project();
|
this = self.as_mut().project();
|
||||||
let fut = this.flow.borrow_mut().service.call(req);
|
let fut = this.flow.service.call(req);
|
||||||
this.state.set(State::ServiceCall(fut));
|
this.state.set(State::ServiceCall(fut));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -467,12 +466,12 @@ where
|
|||||||
if req.head().expect() {
|
if req.head().expect() {
|
||||||
// set dispatcher state so the future is pinned.
|
// set dispatcher state so the future is pinned.
|
||||||
let mut this = self.as_mut().project();
|
let mut this = self.as_mut().project();
|
||||||
let task = this.flow.borrow_mut().expect.call(req);
|
let task = this.flow.expect.call(req);
|
||||||
this.state.set(State::ExpectCall(task));
|
this.state.set(State::ExpectCall(task));
|
||||||
} else {
|
} else {
|
||||||
// the same as above.
|
// the same as above.
|
||||||
let mut this = self.as_mut().project();
|
let mut this = self.as_mut().project();
|
||||||
let task = this.flow.borrow_mut().service.call(req);
|
let task = this.flow.service.call(req);
|
||||||
this.state.set(State::ServiceCall(task));
|
this.state.set(State::ServiceCall(task));
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -485,7 +484,7 @@ where
|
|||||||
Poll::Ready(Ok(req)) => {
|
Poll::Ready(Ok(req)) => {
|
||||||
self.as_mut().send_continue();
|
self.as_mut().send_continue();
|
||||||
let mut this = self.as_mut().project();
|
let mut this = self.as_mut().project();
|
||||||
let task = this.flow.borrow_mut().service.call(req);
|
let task = this.flow.service.call(req);
|
||||||
this.state.set(State::ServiceCall(task));
|
this.state.set(State::ServiceCall(task));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -556,9 +555,7 @@ where
|
|||||||
// merge on_connect_ext data into request extensions
|
// merge on_connect_ext data into request extensions
|
||||||
this.on_connect_data.merge_into(&mut req);
|
this.on_connect_data.merge_into(&mut req);
|
||||||
|
|
||||||
if pl == MessageType::Stream
|
if pl == MessageType::Stream && this.flow.upgrade.is_some() {
|
||||||
&& this.flow.borrow().upgrade.is_some()
|
|
||||||
{
|
|
||||||
this.messages.push_back(DispatcherMessage::Upgrade(req));
|
this.messages.push_back(DispatcherMessage::Upgrade(req));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -811,9 +808,8 @@ where
|
|||||||
let framed = Framed::from_parts(parts);
|
let framed = Framed::from_parts(parts);
|
||||||
let upgrade = inner_p
|
let upgrade = inner_p
|
||||||
.flow
|
.flow
|
||||||
.borrow_mut()
|
|
||||||
.upgrade
|
.upgrade
|
||||||
.take()
|
.as_ref()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.call((req, framed));
|
.call((req, framed));
|
||||||
self.as_mut()
|
self.as_mut()
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use std::task::{Context, Poll};
|
use std::task::Poll;
|
||||||
|
|
||||||
use actix_service::{Service, ServiceFactory};
|
use actix_service::{Service, ServiceFactory};
|
||||||
use futures_util::future::{ready, Ready};
|
use futures_util::future::{ready, Ready};
|
||||||
@ -26,11 +26,9 @@ impl Service<Request> for ExpectHandler {
|
|||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = Ready<Result<Self::Response, Self::Error>>;
|
type Future = Ready<Result<Self::Response, Self::Error>>;
|
||||||
|
|
||||||
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
actix_service::always_ready!();
|
||||||
Poll::Ready(Ok(()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call(&mut self, req: Request) -> Self::Future {
|
fn call(&self, req: Request) -> Self::Future {
|
||||||
ready(Ok(req))
|
ready(Ok(req))
|
||||||
// TODO: add some way to trigger error
|
// TODO: add some way to trigger error
|
||||||
// Err(error::ErrorExpectationFailed("test"))
|
// Err(error::ErrorExpectationFailed("test"))
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
use std::cell::RefCell;
|
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
@ -367,7 +366,7 @@ where
|
|||||||
X: Service<Request>,
|
X: Service<Request>,
|
||||||
U: Service<(Request, Framed<T, Codec>)>,
|
U: Service<(Request, Framed<T, Codec>)>,
|
||||||
{
|
{
|
||||||
flow: Rc<RefCell<HttpFlow<S, X, U>>>,
|
flow: Rc<HttpFlow<S, X, U>>,
|
||||||
on_connect_ext: Option<Rc<ConnectCallback<T>>>,
|
on_connect_ext: Option<Rc<ConnectCallback<T>>>,
|
||||||
cfg: ServiceConfig,
|
cfg: ServiceConfig,
|
||||||
_phantom: PhantomData<B>,
|
_phantom: PhantomData<B>,
|
||||||
@ -417,9 +416,9 @@ where
|
|||||||
type Error = DispatchError;
|
type Error = DispatchError;
|
||||||
type Future = Dispatcher<T, S, B, X, U>;
|
type Future = Dispatcher<T, S, B, X, U>;
|
||||||
|
|
||||||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
let mut flow = self.flow.borrow_mut();
|
let ready = self
|
||||||
let ready = flow
|
.flow
|
||||||
.expect
|
.expect
|
||||||
.poll_ready(cx)
|
.poll_ready(cx)
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
@ -429,7 +428,8 @@ where
|
|||||||
})?
|
})?
|
||||||
.is_ready();
|
.is_ready();
|
||||||
|
|
||||||
let ready = flow
|
let ready = self
|
||||||
|
.flow
|
||||||
.service
|
.service
|
||||||
.poll_ready(cx)
|
.poll_ready(cx)
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
@ -440,7 +440,7 @@ where
|
|||||||
.is_ready()
|
.is_ready()
|
||||||
&& ready;
|
&& ready;
|
||||||
|
|
||||||
let ready = if let Some(ref mut upg) = flow.upgrade {
|
let ready = if let Some(ref upg) = self.flow.upgrade {
|
||||||
upg.poll_ready(cx)
|
upg.poll_ready(cx)
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
let e = e.into();
|
let e = e.into();
|
||||||
@ -460,7 +460,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, (io, addr): (T, Option<net::SocketAddr>)) -> Self::Future {
|
fn call(&self, (io, addr): (T, Option<net::SocketAddr>)) -> Self::Future {
|
||||||
let on_connect_data =
|
let on_connect_data =
|
||||||
OnConnectData::from_io(&io, self.on_connect_ext.as_deref());
|
OnConnectData::from_io(&io, self.on_connect_ext.as_deref());
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use std::task::{Context, Poll};
|
use std::task::Poll;
|
||||||
|
|
||||||
use actix_codec::Framed;
|
use actix_codec::Framed;
|
||||||
use actix_service::{Service, ServiceFactory};
|
use actix_service::{Service, ServiceFactory};
|
||||||
@ -28,11 +28,9 @@ impl<T> Service<(Request, Framed<T, Codec>)> for UpgradeHandler {
|
|||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = Ready<Result<Self::Response, Self::Error>>;
|
type Future = Ready<Result<Self::Response, Self::Error>>;
|
||||||
|
|
||||||
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
actix_service::always_ready!();
|
||||||
Poll::Ready(Ok(()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call(&mut self, _: (Request, Framed<T, Codec>)) -> Self::Future {
|
fn call(&self, _: (Request, Framed<T, Codec>)) -> Self::Future {
|
||||||
ready(Ok(()))
|
ready(Ok(()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
use std::cell::RefCell;
|
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::net;
|
use std::net;
|
||||||
@ -37,7 +36,7 @@ where
|
|||||||
S: Service<Request>,
|
S: Service<Request>,
|
||||||
B: MessageBody,
|
B: MessageBody,
|
||||||
{
|
{
|
||||||
flow: Rc<RefCell<HttpFlow<S, X, U>>>,
|
flow: Rc<HttpFlow<S, X, U>>,
|
||||||
connection: Connection<T, Bytes>,
|
connection: Connection<T, Bytes>,
|
||||||
on_connect_data: OnConnectData,
|
on_connect_data: OnConnectData,
|
||||||
config: ServiceConfig,
|
config: ServiceConfig,
|
||||||
@ -56,7 +55,7 @@ where
|
|||||||
B: MessageBody,
|
B: MessageBody,
|
||||||
{
|
{
|
||||||
pub(crate) fn new(
|
pub(crate) fn new(
|
||||||
services: Rc<RefCell<HttpFlow<S, X, U>>>,
|
flow: Rc<HttpFlow<S, X, U>>,
|
||||||
connection: Connection<T, Bytes>,
|
connection: Connection<T, Bytes>,
|
||||||
on_connect_data: OnConnectData,
|
on_connect_data: OnConnectData,
|
||||||
config: ServiceConfig,
|
config: ServiceConfig,
|
||||||
@ -80,7 +79,7 @@ where
|
|||||||
};
|
};
|
||||||
|
|
||||||
Dispatcher {
|
Dispatcher {
|
||||||
flow: services,
|
flow,
|
||||||
config,
|
config,
|
||||||
peer_addr,
|
peer_addr,
|
||||||
connection,
|
connection,
|
||||||
@ -138,7 +137,7 @@ where
|
|||||||
|
|
||||||
let svc = ServiceResponse::<S::Future, S::Response, S::Error, B> {
|
let svc = ServiceResponse::<S::Future, S::Response, S::Error, B> {
|
||||||
state: ServiceResponseState::ServiceCall(
|
state: ServiceResponseState::ServiceCall(
|
||||||
this.flow.borrow_mut().service.call(req),
|
this.flow.service.call(req),
|
||||||
Some(res),
|
Some(res),
|
||||||
),
|
),
|
||||||
config: this.config.clone(),
|
config: this.config.clone(),
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
use std::cell::RefCell;
|
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
@ -249,7 +248,7 @@ pub struct H2ServiceHandler<T, S, B>
|
|||||||
where
|
where
|
||||||
S: Service<Request>,
|
S: Service<Request>,
|
||||||
{
|
{
|
||||||
flow: Rc<RefCell<HttpFlow<S, (), ()>>>,
|
flow: Rc<HttpFlow<S, (), ()>>,
|
||||||
cfg: ServiceConfig,
|
cfg: ServiceConfig,
|
||||||
on_connect_ext: Option<Rc<ConnectCallback<T>>>,
|
on_connect_ext: Option<Rc<ConnectCallback<T>>>,
|
||||||
_phantom: PhantomData<B>,
|
_phantom: PhantomData<B>,
|
||||||
@ -290,15 +289,15 @@ where
|
|||||||
type Error = DispatchError;
|
type Error = DispatchError;
|
||||||
type Future = H2ServiceHandlerResponse<T, S, B>;
|
type Future = H2ServiceHandlerResponse<T, S, B>;
|
||||||
|
|
||||||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
self.flow.borrow_mut().service.poll_ready(cx).map_err(|e| {
|
self.flow.service.poll_ready(cx).map_err(|e| {
|
||||||
let e = e.into();
|
let e = e.into();
|
||||||
error!("Service readiness error: {:?}", e);
|
error!("Service readiness error: {:?}", e);
|
||||||
DispatchError::Service(e)
|
DispatchError::Service(e)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, (io, addr): (T, Option<net::SocketAddr>)) -> Self::Future {
|
fn call(&self, (io, addr): (T, Option<net::SocketAddr>)) -> Self::Future {
|
||||||
let on_connect_data =
|
let on_connect_data =
|
||||||
OnConnectData::from_io(&io, self.on_connect_ext.as_deref());
|
OnConnectData::from_io(&io, self.on_connect_ext.as_deref());
|
||||||
|
|
||||||
@ -321,7 +320,7 @@ where
|
|||||||
{
|
{
|
||||||
Incoming(Dispatcher<T, S, B, (), ()>),
|
Incoming(Dispatcher<T, S, B, (), ()>),
|
||||||
Handshake(
|
Handshake(
|
||||||
Option<Rc<RefCell<HttpFlow<S, (), ()>>>>,
|
Option<Rc<HttpFlow<S, (), ()>>>,
|
||||||
Option<ServiceConfig>,
|
Option<ServiceConfig>,
|
||||||
Option<net::SocketAddr>,
|
Option<net::SocketAddr>,
|
||||||
OnConnectData,
|
OnConnectData,
|
||||||
|
@ -126,6 +126,6 @@ impl IntoHeaderValue for Mime {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn try_into_value(self) -> Result<HeaderValue, Self::Error> {
|
fn try_into_value(self) -> Result<HeaderValue, Self::Error> {
|
||||||
HeaderValue::try_from(format!("{}", self))
|
HeaderValue::from_str(self.as_ref())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
use std::cell::RefCell;
|
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
@ -440,7 +439,7 @@ where
|
|||||||
X: Service<Request>,
|
X: Service<Request>,
|
||||||
U: Service<(Request, Framed<T, h1::Codec>)>,
|
U: Service<(Request, Framed<T, h1::Codec>)>,
|
||||||
{
|
{
|
||||||
flow: Rc<RefCell<HttpFlow<S, X, U>>>,
|
flow: Rc<HttpFlow<S, X, U>>,
|
||||||
cfg: ServiceConfig,
|
cfg: ServiceConfig,
|
||||||
on_connect_ext: Option<Rc<ConnectCallback<T>>>,
|
on_connect_ext: Option<Rc<ConnectCallback<T>>>,
|
||||||
_phantom: PhantomData<B>,
|
_phantom: PhantomData<B>,
|
||||||
@ -454,12 +453,12 @@ pub(super) struct HttpFlow<S, X, U> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<S, X, U> HttpFlow<S, X, U> {
|
impl<S, X, U> HttpFlow<S, X, U> {
|
||||||
pub(super) fn new(service: S, expect: X, upgrade: Option<U>) -> Rc<RefCell<Self>> {
|
pub(super) fn new(service: S, expect: X, upgrade: Option<U>) -> Rc<Self> {
|
||||||
Rc::new(RefCell::new(Self {
|
Rc::new(Self {
|
||||||
service,
|
service,
|
||||||
expect,
|
expect,
|
||||||
upgrade,
|
upgrade,
|
||||||
}))
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -509,9 +508,9 @@ where
|
|||||||
type Error = DispatchError;
|
type Error = DispatchError;
|
||||||
type Future = HttpServiceHandlerResponse<T, S, B, X, U>;
|
type Future = HttpServiceHandlerResponse<T, S, B, X, U>;
|
||||||
|
|
||||||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
let mut flow = self.flow.borrow_mut();
|
let ready = self
|
||||||
let ready = flow
|
.flow
|
||||||
.expect
|
.expect
|
||||||
.poll_ready(cx)
|
.poll_ready(cx)
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
@ -521,7 +520,8 @@ where
|
|||||||
})?
|
})?
|
||||||
.is_ready();
|
.is_ready();
|
||||||
|
|
||||||
let ready = flow
|
let ready = self
|
||||||
|
.flow
|
||||||
.service
|
.service
|
||||||
.poll_ready(cx)
|
.poll_ready(cx)
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
@ -532,7 +532,7 @@ where
|
|||||||
.is_ready()
|
.is_ready()
|
||||||
&& ready;
|
&& ready;
|
||||||
|
|
||||||
let ready = if let Some(ref mut upg) = flow.upgrade {
|
let ready = if let Some(ref upg) = self.flow.upgrade {
|
||||||
upg.poll_ready(cx)
|
upg.poll_ready(cx)
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
let e = e.into();
|
let e = e.into();
|
||||||
@ -553,7 +553,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn call(
|
fn call(
|
||||||
&mut self,
|
&self,
|
||||||
(io, proto, peer_addr): (T, Protocol, Option<net::SocketAddr>),
|
(io, proto, peer_addr): (T, Protocol, Option<net::SocketAddr>),
|
||||||
) -> Self::Future {
|
) -> Self::Future {
|
||||||
let on_connect_data =
|
let on_connect_data =
|
||||||
@ -604,7 +604,7 @@ where
|
|||||||
Option<(
|
Option<(
|
||||||
Handshake<T, Bytes>,
|
Handshake<T, Bytes>,
|
||||||
ServiceConfig,
|
ServiceConfig,
|
||||||
Rc<RefCell<HttpFlow<S, X, U>>>,
|
Rc<HttpFlow<S, X, U>>,
|
||||||
OnConnectData,
|
OnConnectData,
|
||||||
Option<net::SocketAddr>,
|
Option<net::SocketAddr>,
|
||||||
)>,
|
)>,
|
||||||
|
@ -21,7 +21,7 @@ impl<T> WsService<T> {
|
|||||||
WsService(Arc::new(Mutex::new((PhantomData, Cell::new(false)))))
|
WsService(Arc::new(Mutex::new((PhantomData, Cell::new(false)))))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_polled(&mut self) {
|
fn set_polled(&self) {
|
||||||
*self.0.lock().unwrap().1.get_mut() = true;
|
*self.0.lock().unwrap().1.get_mut() = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,15 +44,12 @@ where
|
|||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = Pin<Box<dyn Future<Output = Result<(), Error>>>>;
|
type Future = Pin<Box<dyn Future<Output = Result<(), Error>>>>;
|
||||||
|
|
||||||
fn poll_ready(&mut self, _ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
fn poll_ready(&self, _ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
self.set_polled();
|
self.set_polled();
|
||||||
Poll::Ready(Ok(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(
|
fn call(&self, (req, mut framed): (Request, Framed<T, h1::Codec>)) -> Self::Future {
|
||||||
&mut self,
|
|
||||||
(req, mut framed): (Request, Framed<T, h1::Codec>),
|
|
||||||
) -> Self::Future {
|
|
||||||
let fut = async move {
|
let fut = async move {
|
||||||
let res = ws::handshake(req.head()).unwrap().message_body(());
|
let res = ws::handshake(req.head()).unwrap().message_body(());
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@ path = "src/lib.rs"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = { version = "4.0.0-beta.1", default-features = false }
|
actix-web = { version = "4.0.0-beta.1", default-features = false }
|
||||||
actix-utils = "3.0.0-beta.1"
|
actix-utils = "3.0.0-beta.2"
|
||||||
|
|
||||||
bytes = "1"
|
bytes = "1"
|
||||||
derive_more = "0.99.5"
|
derive_more = "0.99.5"
|
||||||
@ -28,5 +28,5 @@ mime = "0.3"
|
|||||||
twoway = "0.2"
|
twoway = "0.2"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-rt = "=2.0.0-beta.2"
|
actix-rt = "2"
|
||||||
actix-http = "3.0.0-beta.1"
|
actix-http = "3.0.0-beta.1"
|
||||||
|
@ -16,7 +16,7 @@ name = "actix_web_actors"
|
|||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix = "0.11.0-beta.1"
|
actix = { version = "0.11.0-beta.2", default-features = false }
|
||||||
actix-codec = "0.4.0-beta.1"
|
actix-codec = "0.4.0-beta.1"
|
||||||
actix-http = "3.0.0-beta.1"
|
actix-http = "3.0.0-beta.1"
|
||||||
actix-web = { version = "4.0.0-beta.1", default-features = false }
|
actix-web = { version = "4.0.0-beta.1", default-features = false }
|
||||||
@ -28,6 +28,6 @@ pin-project = "1.0.0"
|
|||||||
tokio = { version = "1", features = ["sync"] }
|
tokio = { version = "1", features = ["sync"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-rt = "=2.0.0-beta.2"
|
actix-rt = "2"
|
||||||
env_logger = "0.7"
|
env_logger = "0.7"
|
||||||
futures-util = { version = "0.3.7", default-features = false }
|
futures-util = { version = "0.3.7", default-features = false }
|
||||||
|
@ -233,14 +233,13 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_default_resource() {
|
async fn test_default_resource() {
|
||||||
let mut srv =
|
let srv = init_service(App::new().service(web::resource("/test").to(|| {
|
||||||
init_service(App::new().service(web::resource("/test").to(|| {
|
HttpResponse::Ok().streaming(HttpContext::create(MyActor { count: 0 }))
|
||||||
HttpResponse::Ok().streaming(HttpContext::create(MyActor { count: 0 }))
|
})))
|
||||||
})))
|
.await;
|
||||||
.await;
|
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/test").to_request();
|
let req = TestRequest::with_uri("/test").to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
let body = read_body(resp).await;
|
let body = read_body(resp).await;
|
||||||
|
@ -19,7 +19,7 @@ syn = { version = "1", features = ["full", "parsing"] }
|
|||||||
proc-macro2 = "1"
|
proc-macro2 = "1"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-rt = "=2.0.0-beta.2"
|
actix-rt = "2"
|
||||||
actix-web = "4.0.0-beta.1"
|
actix-web = "4.0.0-beta.1"
|
||||||
futures-util = { version = "0.3.7", default-features = false }
|
futures-util = { version = "0.3.7", default-features = false }
|
||||||
trybuild = "1"
|
trybuild = "1"
|
||||||
|
@ -175,7 +175,6 @@ pub fn main(_: TokenStream, item: TokenStream) -> TokenStream {
|
|||||||
let vis = &input.vis;
|
let vis = &input.vis;
|
||||||
let sig = &mut input.sig;
|
let sig = &mut input.sig;
|
||||||
let body = &input.block;
|
let body = &input.block;
|
||||||
let name = &sig.ident;
|
|
||||||
|
|
||||||
if sig.asyncness.is_none() {
|
if sig.asyncness.is_none() {
|
||||||
return syn::Error::new_spanned(sig.fn_token, "only async fn is supported")
|
return syn::Error::new_spanned(sig.fn_token, "only async fn is supported")
|
||||||
@ -188,7 +187,7 @@ pub fn main(_: TokenStream, item: TokenStream) -> TokenStream {
|
|||||||
(quote! {
|
(quote! {
|
||||||
#(#attrs)*
|
#(#attrs)*
|
||||||
#vis #sig {
|
#vis #sig {
|
||||||
actix_web::rt::System::new(stringify!(#name))
|
actix_web::rt::System::new()
|
||||||
.block_on(async move { #body })
|
.block_on(async move { #body })
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
use std::pin::Pin;
|
|
||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform};
|
use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform};
|
||||||
@ -8,7 +7,7 @@ use actix_web::{http, test, web::Path, App, Error, HttpResponse, Responder};
|
|||||||
use actix_web_codegen::{
|
use actix_web_codegen::{
|
||||||
connect, delete, get, head, options, patch, post, put, route, trace,
|
connect, delete, get, head, options, patch, post, put, route, trace,
|
||||||
};
|
};
|
||||||
use futures_util::future;
|
use futures_util::future::{self, LocalBoxFuture};
|
||||||
|
|
||||||
// Make sure that we can name function as 'config'
|
// Make sure that we can name function as 'config'
|
||||||
#[get("/config")]
|
#[get("/config")]
|
||||||
@ -117,14 +116,13 @@ where
|
|||||||
{
|
{
|
||||||
type Response = ServiceResponse<B>;
|
type Response = ServiceResponse<B>;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
#[allow(clippy::type_complexity)]
|
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
|
||||||
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;
|
|
||||||
|
|
||||||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
self.service.poll_ready(cx)
|
self.service.poll_ready(cx)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: ServiceRequest) -> Self::Future {
|
fn call(&self, req: ServiceRequest) -> Self::Future {
|
||||||
let fut = self.service.call(req);
|
let fut = self.service.call(req);
|
||||||
|
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
|
@ -5,6 +5,9 @@
|
|||||||
* `ClientRequest::insert_header` method which allows using typed headers. [#1869]
|
* `ClientRequest::insert_header` method which allows using typed headers. [#1869]
|
||||||
* `ClientRequest::append_header` method which allows using typed headers. [#1869]
|
* `ClientRequest::append_header` method which allows using typed headers. [#1869]
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
* Relax default timeout for `Connector` to 5 seconds(original 1 second). [#1905]
|
||||||
|
|
||||||
### Removed
|
### Removed
|
||||||
* `ClientRequest::set`; use `ClientRequest::insert_header`. [#1869]
|
* `ClientRequest::set`; use `ClientRequest::insert_header`. [#1869]
|
||||||
* `ClientRequest::set_header`; use `ClientRequest::insert_header`. [#1869]
|
* `ClientRequest::set_header`; use `ClientRequest::insert_header`. [#1869]
|
||||||
@ -12,6 +15,7 @@
|
|||||||
* `ClientRequest::header`; use `ClientRequest::append_header`. [#1869]
|
* `ClientRequest::header`; use `ClientRequest::append_header`. [#1869]
|
||||||
|
|
||||||
[#1869]: https://github.com/actix/actix-web/pull/1869
|
[#1869]: https://github.com/actix/actix-web/pull/1869
|
||||||
|
[#1905]: https://github.com/actix/actix-web/pull/1905
|
||||||
|
|
||||||
|
|
||||||
## 3.0.0-beta.1 - 2021-01-07
|
## 3.0.0-beta.1 - 2021-01-07
|
||||||
|
@ -38,9 +38,9 @@ compress = ["actix-http/compress"]
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-codec = "0.4.0-beta.1"
|
actix-codec = "0.4.0-beta.1"
|
||||||
actix-service = "2.0.0-beta.3"
|
actix-service = "2.0.0-beta.4"
|
||||||
actix-http = "3.0.0-beta.1"
|
actix-http = "3.0.0-beta.1"
|
||||||
actix-rt = "=2.0.0-beta.2"
|
actix-rt = "2"
|
||||||
|
|
||||||
base64 = "0.13"
|
base64 = "0.13"
|
||||||
bytes = "1"
|
bytes = "1"
|
||||||
@ -58,15 +58,12 @@ open-ssl = { version = "0.10", package = "openssl", optional = true }
|
|||||||
rust-tls = { version = "0.19.0", package = "rustls", optional = true, features = ["dangerous_configuration"] }
|
rust-tls = { version = "0.19.0", package = "rustls", optional = true, features = ["dangerous_configuration"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
# TODO: actix is temporary added as dev dep for actix-macro reason.
|
|
||||||
# Can be removed when it does not impact tests.
|
|
||||||
actix = "0.11.0-beta.1"
|
|
||||||
actix-web = { version = "4.0.0-beta.1", features = ["openssl"] }
|
actix-web = { version = "4.0.0-beta.1", features = ["openssl"] }
|
||||||
actix-http = { version = "3.0.0-beta.1", features = ["openssl"] }
|
actix-http = { version = "3.0.0-beta.1", features = ["openssl"] }
|
||||||
actix-http-test = { version = "3.0.0-beta.1", features = ["openssl"] }
|
actix-http-test = { version = "3.0.0-beta.1", features = ["openssl"] }
|
||||||
actix-utils = "3.0.0-beta.1"
|
actix-utils = "3.0.0-beta.1"
|
||||||
actix-server = "2.0.0-beta.2"
|
actix-server = "2.0.0-beta.3"
|
||||||
actix-tls = { version = "3.0.0-beta.2", features = ["openssl", "rustls"] }
|
actix-tls = { version = "3.0.0-beta.3", features = ["openssl", "rustls"] }
|
||||||
|
|
||||||
brotli2 = "0.3.2"
|
brotli2 = "0.3.2"
|
||||||
flate2 = "1.0.13"
|
flate2 = "1.0.13"
|
||||||
|
@ -21,7 +21,7 @@ use actix_rt::System;
|
|||||||
use awc::Client;
|
use awc::Client;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
System::new("test").block_on(async {
|
System::new().block_on(async {
|
||||||
let client = Client::default();
|
let client = Client::default();
|
||||||
|
|
||||||
let res = client
|
let res = client
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
use std::cell::RefCell;
|
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
@ -24,7 +23,7 @@ pub struct ClientBuilder {
|
|||||||
conn_window_size: Option<u32>,
|
conn_window_size: Option<u32>,
|
||||||
headers: HeaderMap,
|
headers: HeaderMap,
|
||||||
timeout: Option<Duration>,
|
timeout: Option<Duration>,
|
||||||
connector: Option<RefCell<Box<dyn Connect>>>,
|
connector: Option<Box<dyn Connect>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for ClientBuilder {
|
impl Default for ClientBuilder {
|
||||||
@ -56,7 +55,7 @@ impl ClientBuilder {
|
|||||||
<T::Response as Connection>::Future: 'static,
|
<T::Response as Connection>::Future: 'static,
|
||||||
T::Future: 'static,
|
T::Future: 'static,
|
||||||
{
|
{
|
||||||
self.connector = Some(RefCell::new(Box::new(ConnectorWrapper(connector))));
|
self.connector = Some(Box::new(ConnectorWrapper(connector)));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -182,9 +181,7 @@ impl ClientBuilder {
|
|||||||
if let Some(val) = self.stream_window_size {
|
if let Some(val) = self.stream_window_size {
|
||||||
connector = connector.initial_window_size(val)
|
connector = connector.initial_window_size(val)
|
||||||
};
|
};
|
||||||
RefCell::new(
|
Box::new(ConnectorWrapper(connector.finish())) as _
|
||||||
Box::new(ConnectorWrapper(connector.finish())) as Box<dyn Connect>
|
|
||||||
)
|
|
||||||
};
|
};
|
||||||
let config = ClientConfig {
|
let config = ClientConfig {
|
||||||
headers: self.headers,
|
headers: self.headers,
|
||||||
|
@ -20,14 +20,14 @@ pub(crate) struct ConnectorWrapper<T>(pub T);
|
|||||||
|
|
||||||
pub(crate) trait Connect {
|
pub(crate) trait Connect {
|
||||||
fn send_request(
|
fn send_request(
|
||||||
&mut self,
|
&self,
|
||||||
head: RequestHead,
|
head: RequestHead,
|
||||||
body: Body,
|
body: Body,
|
||||||
addr: Option<net::SocketAddr>,
|
addr: Option<net::SocketAddr>,
|
||||||
) -> Pin<Box<dyn Future<Output = Result<ClientResponse, SendRequestError>>>>;
|
) -> Pin<Box<dyn Future<Output = Result<ClientResponse, SendRequestError>>>>;
|
||||||
|
|
||||||
fn send_request_extra(
|
fn send_request_extra(
|
||||||
&mut self,
|
&self,
|
||||||
head: Rc<RequestHead>,
|
head: Rc<RequestHead>,
|
||||||
extra_headers: Option<HeaderMap>,
|
extra_headers: Option<HeaderMap>,
|
||||||
body: Body,
|
body: Body,
|
||||||
@ -36,7 +36,7 @@ pub(crate) trait Connect {
|
|||||||
|
|
||||||
/// Send request, returns Response and Framed
|
/// Send request, returns Response and Framed
|
||||||
fn open_tunnel(
|
fn open_tunnel(
|
||||||
&mut self,
|
&self,
|
||||||
head: RequestHead,
|
head: RequestHead,
|
||||||
addr: Option<net::SocketAddr>,
|
addr: Option<net::SocketAddr>,
|
||||||
) -> Pin<
|
) -> Pin<
|
||||||
@ -52,7 +52,7 @@ pub(crate) trait Connect {
|
|||||||
|
|
||||||
/// Send request and extra headers, returns Response and Framed
|
/// Send request and extra headers, returns Response and Framed
|
||||||
fn open_tunnel_extra(
|
fn open_tunnel_extra(
|
||||||
&mut self,
|
&self,
|
||||||
head: Rc<RequestHead>,
|
head: Rc<RequestHead>,
|
||||||
extra_headers: Option<HeaderMap>,
|
extra_headers: Option<HeaderMap>,
|
||||||
addr: Option<net::SocketAddr>,
|
addr: Option<net::SocketAddr>,
|
||||||
@ -78,7 +78,7 @@ where
|
|||||||
T::Future: 'static,
|
T::Future: 'static,
|
||||||
{
|
{
|
||||||
fn send_request(
|
fn send_request(
|
||||||
&mut self,
|
&self,
|
||||||
head: RequestHead,
|
head: RequestHead,
|
||||||
body: Body,
|
body: Body,
|
||||||
addr: Option<net::SocketAddr>,
|
addr: Option<net::SocketAddr>,
|
||||||
@ -101,7 +101,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn send_request_extra(
|
fn send_request_extra(
|
||||||
&mut self,
|
&self,
|
||||||
head: Rc<RequestHead>,
|
head: Rc<RequestHead>,
|
||||||
extra_headers: Option<HeaderMap>,
|
extra_headers: Option<HeaderMap>,
|
||||||
body: Body,
|
body: Body,
|
||||||
@ -126,7 +126,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn open_tunnel(
|
fn open_tunnel(
|
||||||
&mut self,
|
&self,
|
||||||
head: RequestHead,
|
head: RequestHead,
|
||||||
addr: Option<net::SocketAddr>,
|
addr: Option<net::SocketAddr>,
|
||||||
) -> Pin<
|
) -> Pin<
|
||||||
@ -158,7 +158,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn open_tunnel_extra(
|
fn open_tunnel_extra(
|
||||||
&mut self,
|
&self,
|
||||||
head: Rc<RequestHead>,
|
head: Rc<RequestHead>,
|
||||||
extra_headers: Option<HeaderMap>,
|
extra_headers: Option<HeaderMap>,
|
||||||
addr: Option<net::SocketAddr>,
|
addr: Option<net::SocketAddr>,
|
||||||
|
@ -93,7 +93,6 @@
|
|||||||
#![doc(html_logo_url = "https://actix.rs/img/logo.png")]
|
#![doc(html_logo_url = "https://actix.rs/img/logo.png")]
|
||||||
#![doc(html_favicon_url = "https://actix.rs/favicon.ico")]
|
#![doc(html_favicon_url = "https://actix.rs/favicon.ico")]
|
||||||
|
|
||||||
use std::cell::RefCell;
|
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
@ -145,7 +144,7 @@ use self::connect::{Connect, ConnectorWrapper};
|
|||||||
pub struct Client(Rc<ClientConfig>);
|
pub struct Client(Rc<ClientConfig>);
|
||||||
|
|
||||||
pub(crate) struct ClientConfig {
|
pub(crate) struct ClientConfig {
|
||||||
pub(crate) connector: RefCell<Box<dyn Connect>>,
|
pub(crate) connector: Box<dyn Connect>,
|
||||||
pub(crate) headers: HeaderMap,
|
pub(crate) headers: HeaderMap,
|
||||||
pub(crate) timeout: Option<Duration>,
|
pub(crate) timeout: Option<Duration>,
|
||||||
}
|
}
|
||||||
@ -153,9 +152,7 @@ pub(crate) struct ClientConfig {
|
|||||||
impl Default for Client {
|
impl Default for Client {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Client(Rc::new(ClientConfig {
|
Client(Rc::new(ClientConfig {
|
||||||
connector: RefCell::new(Box::new(ConnectorWrapper(
|
connector: Box::new(ConnectorWrapper(Connector::new().finish())),
|
||||||
Connector::new().finish(),
|
|
||||||
))),
|
|
||||||
headers: HeaderMap::new(),
|
headers: HeaderMap::new(),
|
||||||
timeout: Some(Duration::from_secs(5)),
|
timeout: Some(Duration::from_secs(5)),
|
||||||
}))
|
}))
|
||||||
|
@ -183,15 +183,13 @@ impl RequestSender {
|
|||||||
where
|
where
|
||||||
B: Into<Body>,
|
B: Into<Body>,
|
||||||
{
|
{
|
||||||
let mut connector = config.connector.borrow_mut();
|
|
||||||
|
|
||||||
let fut = match self {
|
let fut = match self {
|
||||||
RequestSender::Owned(head) => {
|
RequestSender::Owned(head) => {
|
||||||
connector.send_request(head, body.into(), addr)
|
config.connector.send_request(head, body.into(), addr)
|
||||||
}
|
|
||||||
RequestSender::Rc(head, extra_headers) => {
|
|
||||||
connector.send_request_extra(head, extra_headers, body.into(), addr)
|
|
||||||
}
|
}
|
||||||
|
RequestSender::Rc(head, extra_headers) => config
|
||||||
|
.connector
|
||||||
|
.send_request_extra(head, extra_headers, body.into(), addr),
|
||||||
};
|
};
|
||||||
|
|
||||||
SendClientRequest::new(fut, response_decompress, timeout.or(config.timeout))
|
SendClientRequest::new(fut, response_decompress, timeout.or(config.timeout))
|
||||||
|
@ -325,11 +325,7 @@ impl WebsocketsRequest {
|
|||||||
let max_size = self.max_size;
|
let max_size = self.max_size;
|
||||||
let server_mode = self.server_mode;
|
let server_mode = self.server_mode;
|
||||||
|
|
||||||
let fut = self
|
let fut = self.config.connector.open_tunnel(head, self.addr);
|
||||||
.config
|
|
||||||
.connector
|
|
||||||
.borrow_mut()
|
|
||||||
.open_tunnel(head, self.addr);
|
|
||||||
|
|
||||||
// set request timeout
|
// set request timeout
|
||||||
let (head, framed) = if let Some(to) = self.config.timeout {
|
let (head, framed) = if let Some(to) = self.config.timeout {
|
||||||
|
@ -120,9 +120,7 @@ async fn test_timeout() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let connector = awc::Connector::new()
|
let connector = awc::Connector::new()
|
||||||
.connector(actix_tls::connect::new_connector(
|
.connector(actix_tls::connect::default_connector())
|
||||||
actix_tls::connect::start_default_resolver().await.unwrap(),
|
|
||||||
))
|
|
||||||
.timeout(Duration::from_secs(15))
|
.timeout(Duration::from_secs(15))
|
||||||
.finish();
|
.finish();
|
||||||
|
|
||||||
|
@ -68,7 +68,7 @@ impl<T: Responder> Responder for OptionResponder<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn future_responder(c: &mut Criterion) {
|
fn future_responder(c: &mut Criterion) {
|
||||||
let rt = actix_rt::System::new("test");
|
let rt = actix_rt::System::new();
|
||||||
let req = TestRequest::default().to_http_request();
|
let req = TestRequest::default().to_http_request();
|
||||||
|
|
||||||
c.bench_function("future_responder", move |b| {
|
c.bench_function("future_responder", move |b| {
|
||||||
@ -91,7 +91,7 @@ fn future_responder(c: &mut Criterion) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn responder(c: &mut Criterion) {
|
fn responder(c: &mut Criterion) {
|
||||||
let rt = actix_rt::System::new("test");
|
let rt = actix_rt::System::new();
|
||||||
let req = TestRequest::default().to_http_request();
|
let req = TestRequest::default().to_http_request();
|
||||||
c.bench_function("responder", move |b| {
|
c.bench_function("responder", move |b| {
|
||||||
b.iter_custom(|_| {
|
b.iter_custom(|_| {
|
||||||
|
@ -29,7 +29,7 @@ const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
|
|||||||
fn bench_async_burst(c: &mut Criterion) {
|
fn bench_async_burst(c: &mut Criterion) {
|
||||||
// We are using System here, since Runtime requires preinitialized tokio
|
// We are using System here, since Runtime requires preinitialized tokio
|
||||||
// Maybe add to actix_rt docs
|
// Maybe add to actix_rt docs
|
||||||
let rt = actix_rt::System::new("test");
|
let rt = actix_rt::System::new();
|
||||||
|
|
||||||
let srv = rt.block_on(async {
|
let srv = rt.block_on(async {
|
||||||
test::start(|| {
|
test::start(|| {
|
||||||
|
@ -25,7 +25,7 @@ pub fn bench_async_service<S>(c: &mut Criterion, srv: S, name: &str)
|
|||||||
where
|
where
|
||||||
S: Service<ServiceRequest, Response = ServiceResponse, Error = Error> + 'static,
|
S: Service<ServiceRequest, Response = ServiceResponse, Error = Error> + 'static,
|
||||||
{
|
{
|
||||||
let rt = actix_rt::System::new("test");
|
let rt = actix_rt::System::new();
|
||||||
let srv = Rc::new(RefCell::new(srv));
|
let srv = Rc::new(RefCell::new(srv));
|
||||||
|
|
||||||
let req = TestRequest::default().to_srv_request();
|
let req = TestRequest::default().to_srv_request();
|
||||||
@ -67,7 +67,7 @@ async fn index(req: ServiceRequest) -> Result<ServiceResponse, Error> {
|
|||||||
// Sample results on MacBook Pro '14
|
// Sample results on MacBook Pro '14
|
||||||
// time: [2.0724 us 2.1345 us 2.2074 us]
|
// time: [2.0724 us 2.1345 us 2.2074 us]
|
||||||
fn async_web_service(c: &mut Criterion) {
|
fn async_web_service(c: &mut Criterion) {
|
||||||
let rt = actix_rt::System::new("test");
|
let rt = actix_rt::System::new();
|
||||||
let srv = Rc::new(RefCell::new(rt.block_on(init_service(
|
let srv = Rc::new(RefCell::new(rt.block_on(init_service(
|
||||||
App::new().service(web::service("/").finish(index)),
|
App::new().service(web::service("/").finish(index)),
|
||||||
))));
|
))));
|
||||||
|
32
src/app.rs
32
src/app.rs
@ -422,7 +422,7 @@ where
|
|||||||
>
|
>
|
||||||
where
|
where
|
||||||
B1: MessageBody,
|
B1: MessageBody,
|
||||||
F: FnMut(ServiceRequest, &mut T::Service) -> R + Clone,
|
F: Fn(ServiceRequest, &T::Service) -> R + Clone,
|
||||||
R: Future<Output = Result<ServiceResponse<B1>, Error>>,
|
R: Future<Output = Result<ServiceResponse<B1>, Error>>,
|
||||||
{
|
{
|
||||||
App {
|
App {
|
||||||
@ -480,7 +480,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_default_resource() {
|
async fn test_default_resource() {
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new().service(web::resource("/test").to(HttpResponse::Ok)),
|
App::new().service(web::resource("/test").to(HttpResponse::Ok)),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
@ -492,7 +492,7 @@ mod tests {
|
|||||||
let resp = srv.call(req).await.unwrap();
|
let resp = srv.call(req).await.unwrap();
|
||||||
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
||||||
|
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.service(web::resource("/test").to(HttpResponse::Ok))
|
.service(web::resource("/test").to(HttpResponse::Ok))
|
||||||
.service(
|
.service(
|
||||||
@ -525,7 +525,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_data_factory() {
|
async fn test_data_factory() {
|
||||||
let mut srv =
|
let srv =
|
||||||
init_service(App::new().data_factory(|| ok::<_, ()>(10usize)).service(
|
init_service(App::new().data_factory(|| ok::<_, ()>(10usize)).service(
|
||||||
web::resource("/").to(|_: web::Data<usize>| HttpResponse::Ok()),
|
web::resource("/").to(|_: web::Data<usize>| HttpResponse::Ok()),
|
||||||
))
|
))
|
||||||
@ -534,7 +534,7 @@ mod tests {
|
|||||||
let resp = srv.call(req).await.unwrap();
|
let resp = srv.call(req).await.unwrap();
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
let mut srv =
|
let srv =
|
||||||
init_service(App::new().data_factory(|| ok::<_, ()>(10u32)).service(
|
init_service(App::new().data_factory(|| ok::<_, ()>(10u32)).service(
|
||||||
web::resource("/").to(|_: web::Data<usize>| HttpResponse::Ok()),
|
web::resource("/").to(|_: web::Data<usize>| HttpResponse::Ok()),
|
||||||
))
|
))
|
||||||
@ -557,7 +557,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_extension() {
|
async fn test_extension() {
|
||||||
let mut srv = init_service(App::new().app_data(10usize).service(
|
let srv = init_service(App::new().app_data(10usize).service(
|
||||||
web::resource("/").to(|req: HttpRequest| {
|
web::resource("/").to(|req: HttpRequest| {
|
||||||
assert_eq!(*req.app_data::<usize>().unwrap(), 10);
|
assert_eq!(*req.app_data::<usize>().unwrap(), 10);
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
@ -571,7 +571,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_wrap() {
|
async fn test_wrap() {
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.wrap(
|
.wrap(
|
||||||
DefaultHeaders::new()
|
DefaultHeaders::new()
|
||||||
@ -581,7 +581,7 @@ mod tests {
|
|||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
let req = TestRequest::with_uri("/test").to_request();
|
let req = TestRequest::with_uri("/test").to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
||||||
@ -591,7 +591,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_router_wrap() {
|
async fn test_router_wrap() {
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.route("/test", web::get().to(HttpResponse::Ok))
|
.route("/test", web::get().to(HttpResponse::Ok))
|
||||||
.wrap(
|
.wrap(
|
||||||
@ -601,7 +601,7 @@ mod tests {
|
|||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
let req = TestRequest::with_uri("/test").to_request();
|
let req = TestRequest::with_uri("/test").to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
||||||
@ -611,7 +611,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_wrap_fn() {
|
async fn test_wrap_fn() {
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.wrap_fn(|req, srv| {
|
.wrap_fn(|req, srv| {
|
||||||
let fut = srv.call(req);
|
let fut = srv.call(req);
|
||||||
@ -628,7 +628,7 @@ mod tests {
|
|||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
let req = TestRequest::with_uri("/test").to_request();
|
let req = TestRequest::with_uri("/test").to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
||||||
@ -638,7 +638,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_router_wrap_fn() {
|
async fn test_router_wrap_fn() {
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.route("/test", web::get().to(HttpResponse::Ok))
|
.route("/test", web::get().to(HttpResponse::Ok))
|
||||||
.wrap_fn(|req, srv| {
|
.wrap_fn(|req, srv| {
|
||||||
@ -655,7 +655,7 @@ mod tests {
|
|||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
let req = TestRequest::with_uri("/test").to_request();
|
let req = TestRequest::with_uri("/test").to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
||||||
@ -665,7 +665,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_external_resource() {
|
async fn test_external_resource() {
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.external_resource("youtube", "https://youtube.com/watch/{video_id}")
|
.external_resource("youtube", "https://youtube.com/watch/{video_id}")
|
||||||
.route(
|
.route(
|
||||||
@ -679,7 +679,7 @@ mod tests {
|
|||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
let req = TestRequest::with_uri("/test").to_request();
|
let req = TestRequest::with_uri("/test").to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
let body = read_body(resp).await;
|
let body = read_body(resp).await;
|
||||||
assert_eq!(body, Bytes::from_static(b"https://youtube.com/watch/12345"));
|
assert_eq!(body, Bytes::from_static(b"https://youtube.com/watch/12345"));
|
||||||
|
@ -196,7 +196,7 @@ where
|
|||||||
|
|
||||||
actix_service::forward_ready!(service);
|
actix_service::forward_ready!(service);
|
||||||
|
|
||||||
fn call(&mut self, req: Request) -> Self::Future {
|
fn call(&self, req: Request) -> Self::Future {
|
||||||
let (head, payload) = req.into_parts();
|
let (head, payload) = req.into_parts();
|
||||||
|
|
||||||
let req = if let Some(mut req) = self.app_state.pool().pop() {
|
let req = if let Some(mut req) = self.app_state.pool().pop() {
|
||||||
@ -287,8 +287,8 @@ impl Service<ServiceRequest> for AppRouting {
|
|||||||
|
|
||||||
actix_service::always_ready!();
|
actix_service::always_ready!();
|
||||||
|
|
||||||
fn call(&mut self, mut req: ServiceRequest) -> Self::Future {
|
fn call(&self, mut req: ServiceRequest) -> Self::Future {
|
||||||
let res = self.router.recognize_mut_checked(&mut req, |req, guards| {
|
let res = self.router.recognize_checked(&mut req, |req, guards| {
|
||||||
if let Some(ref guards) = guards {
|
if let Some(ref guards) = guards {
|
||||||
for f in guards {
|
for f in guards {
|
||||||
if !f.check(req.head()) {
|
if !f.check(req.head()) {
|
||||||
@ -354,7 +354,7 @@ mod tests {
|
|||||||
let data = Arc::new(AtomicBool::new(false));
|
let data = Arc::new(AtomicBool::new(false));
|
||||||
|
|
||||||
{
|
{
|
||||||
let mut app = init_service(
|
let app = init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.data(DropData(data.clone()))
|
.data(DropData(data.clone()))
|
||||||
.service(web::resource("/test").to(HttpResponse::Ok)),
|
.service(web::resource("/test").to(HttpResponse::Ok)),
|
||||||
|
@ -263,7 +263,7 @@ mod tests {
|
|||||||
cfg.app_data(15u8);
|
cfg.app_data(15u8);
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut srv = init_service(App::new().configure(cfg).service(
|
let srv = init_service(App::new().configure(cfg).service(
|
||||||
web::resource("/").to(|_: web::Data<usize>, req: HttpRequest| {
|
web::resource("/").to(|_: web::Data<usize>, req: HttpRequest| {
|
||||||
assert_eq!(*req.app_data::<u8>().unwrap(), 15u8);
|
assert_eq!(*req.app_data::<u8>().unwrap(), 15u8);
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
@ -286,7 +286,7 @@ mod tests {
|
|||||||
// });
|
// });
|
||||||
// };
|
// };
|
||||||
|
|
||||||
// let mut srv =
|
// let srv =
|
||||||
// init_service(App::new().configure(cfg).service(
|
// init_service(App::new().configure(cfg).service(
|
||||||
// web::resource("/").to(|_: web::Data<usize>| HttpResponse::Ok()),
|
// web::resource("/").to(|_: web::Data<usize>| HttpResponse::Ok()),
|
||||||
// ));
|
// ));
|
||||||
@ -297,7 +297,7 @@ mod tests {
|
|||||||
// let cfg2 = |cfg: &mut ServiceConfig| {
|
// let cfg2 = |cfg: &mut ServiceConfig| {
|
||||||
// cfg.data_factory(|| Ok::<_, ()>(10u32));
|
// cfg.data_factory(|| Ok::<_, ()>(10u32));
|
||||||
// };
|
// };
|
||||||
// let mut srv = init_service(
|
// let srv = init_service(
|
||||||
// App::new()
|
// App::new()
|
||||||
// .service(web::resource("/").to(|_: web::Data<usize>| HttpResponse::Ok()))
|
// .service(web::resource("/").to(|_: web::Data<usize>| HttpResponse::Ok()))
|
||||||
// .configure(cfg2),
|
// .configure(cfg2),
|
||||||
@ -309,7 +309,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_external_resource() {
|
async fn test_external_resource() {
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.configure(|cfg| {
|
.configure(|cfg| {
|
||||||
cfg.external_resource(
|
cfg.external_resource(
|
||||||
@ -328,7 +328,7 @@ mod tests {
|
|||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
let req = TestRequest::with_uri("/test").to_request();
|
let req = TestRequest::with_uri("/test").to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
let body = read_body(resp).await;
|
let body = read_body(resp).await;
|
||||||
assert_eq!(body, Bytes::from_static(b"https://youtube.com/watch/12345"));
|
assert_eq!(body, Bytes::from_static(b"https://youtube.com/watch/12345"));
|
||||||
@ -336,7 +336,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_service() {
|
async fn test_service() {
|
||||||
let mut srv = init_service(App::new().configure(|cfg| {
|
let srv = init_service(App::new().configure(|cfg| {
|
||||||
cfg.service(
|
cfg.service(
|
||||||
web::resource("/test").route(web::get().to(HttpResponse::Created)),
|
web::resource("/test").route(web::get().to(HttpResponse::Created)),
|
||||||
)
|
)
|
||||||
@ -347,13 +347,13 @@ mod tests {
|
|||||||
let req = TestRequest::with_uri("/test")
|
let req = TestRequest::with_uri("/test")
|
||||||
.method(Method::GET)
|
.method(Method::GET)
|
||||||
.to_request();
|
.to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::CREATED);
|
assert_eq!(resp.status(), StatusCode::CREATED);
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/index.html")
|
let req = TestRequest::with_uri("/index.html")
|
||||||
.method(Method::GET)
|
.method(Method::GET)
|
||||||
.to_request();
|
.to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
16
src/data.rs
16
src/data.rs
@ -144,7 +144,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_data_extractor() {
|
async fn test_data_extractor() {
|
||||||
let mut srv = init_service(App::new().data("TEST".to_string()).service(
|
let srv = init_service(App::new().data("TEST".to_string()).service(
|
||||||
web::resource("/").to(|data: web::Data<String>| {
|
web::resource("/").to(|data: web::Data<String>| {
|
||||||
assert_eq!(data.to_lowercase(), "test");
|
assert_eq!(data.to_lowercase(), "test");
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
@ -156,7 +156,7 @@ mod tests {
|
|||||||
let resp = srv.call(req).await.unwrap();
|
let resp = srv.call(req).await.unwrap();
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
let mut srv =
|
let srv =
|
||||||
init_service(App::new().data(10u32).service(
|
init_service(App::new().data(10u32).service(
|
||||||
web::resource("/").to(|_: web::Data<usize>| HttpResponse::Ok()),
|
web::resource("/").to(|_: web::Data<usize>| HttpResponse::Ok()),
|
||||||
))
|
))
|
||||||
@ -165,7 +165,7 @@ mod tests {
|
|||||||
let resp = srv.call(req).await.unwrap();
|
let resp = srv.call(req).await.unwrap();
|
||||||
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
||||||
|
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.data(10u32)
|
.data(10u32)
|
||||||
.data(13u32)
|
.data(13u32)
|
||||||
@ -186,7 +186,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_app_data_extractor() {
|
async fn test_app_data_extractor() {
|
||||||
let mut srv =
|
let srv =
|
||||||
init_service(App::new().app_data(Data::new(10usize)).service(
|
init_service(App::new().app_data(Data::new(10usize)).service(
|
||||||
web::resource("/").to(|_: web::Data<usize>| HttpResponse::Ok()),
|
web::resource("/").to(|_: web::Data<usize>| HttpResponse::Ok()),
|
||||||
))
|
))
|
||||||
@ -196,7 +196,7 @@ mod tests {
|
|||||||
let resp = srv.call(req).await.unwrap();
|
let resp = srv.call(req).await.unwrap();
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
let mut srv =
|
let srv =
|
||||||
init_service(App::new().app_data(Data::new(10u32)).service(
|
init_service(App::new().app_data(Data::new(10u32)).service(
|
||||||
web::resource("/").to(|_: web::Data<usize>| HttpResponse::Ok()),
|
web::resource("/").to(|_: web::Data<usize>| HttpResponse::Ok()),
|
||||||
))
|
))
|
||||||
@ -208,7 +208,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_route_data_extractor() {
|
async fn test_route_data_extractor() {
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new().service(
|
App::new().service(
|
||||||
web::resource("/")
|
web::resource("/")
|
||||||
.data(10usize)
|
.data(10usize)
|
||||||
@ -222,7 +222,7 @@ mod tests {
|
|||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
// different type
|
// different type
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new().service(
|
App::new().service(
|
||||||
web::resource("/")
|
web::resource("/")
|
||||||
.data(10u32)
|
.data(10u32)
|
||||||
@ -237,7 +237,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_override_data() {
|
async fn test_override_data() {
|
||||||
let mut srv = init_service(App::new().data(1usize).service(
|
let srv = init_service(App::new().data(1usize).service(
|
||||||
web::resource("/").data(10usize).route(web::get().to(
|
web::resource("/").data(10usize).route(web::get().to(
|
||||||
|data: web::Data<usize>| {
|
|data: web::Data<usize>| {
|
||||||
assert_eq!(**data, 10);
|
assert_eq!(**data, 10);
|
||||||
|
@ -113,11 +113,11 @@ where
|
|||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = HandlerServiceFuture<F, T, R>;
|
type Future = HandlerServiceFuture<F, T, R>;
|
||||||
|
|
||||||
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
fn poll_ready(&self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
Poll::Ready(Ok(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: ServiceRequest) -> Self::Future {
|
fn call(&self, req: ServiceRequest) -> Self::Future {
|
||||||
let (req, mut payload) = req.into_parts();
|
let (req, mut payload) = req.into_parts();
|
||||||
let fut = T::from_request(&req, &mut payload);
|
let fut = T::from_request(&req, &mut payload);
|
||||||
HandlerServiceFuture::Extract(fut, Some(req), self.hnd.clone())
|
HandlerServiceFuture::Extract(fut, Some(req), self.hnd.clone())
|
||||||
|
@ -80,11 +80,11 @@ where
|
|||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = CompatMiddlewareFuture<S::Future>;
|
type Future = CompatMiddlewareFuture<S::Future>;
|
||||||
|
|
||||||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
self.service.poll_ready(cx).map_err(From::from)
|
self.service.poll_ready(cx).map_err(From::from)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: Req) -> Self::Future {
|
fn call(&self, req: Req) -> Self::Future {
|
||||||
let fut = self.service.call(req);
|
let fut = self.service.call(req);
|
||||||
CompatMiddlewareFuture { fut }
|
CompatMiddlewareFuture { fut }
|
||||||
}
|
}
|
||||||
@ -138,7 +138,7 @@ mod tests {
|
|||||||
let logger = Logger::default();
|
let logger = Logger::default();
|
||||||
let compress = Compress::default();
|
let compress = Compress::default();
|
||||||
|
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new().service(
|
App::new().service(
|
||||||
web::scope("app")
|
web::scope("app")
|
||||||
.wrap(Compat::new(logger))
|
.wrap(Compat::new(logger))
|
||||||
@ -151,7 +151,7 @@ mod tests {
|
|||||||
.await;
|
.await;
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/app/test").to_request();
|
let req = TestRequest::with_uri("/app/test").to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,7 +160,7 @@ mod tests {
|
|||||||
let logger = Logger::default();
|
let logger = Logger::default();
|
||||||
let compress = Compress::default();
|
let compress = Compress::default();
|
||||||
|
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new().service(
|
App::new().service(
|
||||||
web::resource("app/test")
|
web::resource("app/test")
|
||||||
.wrap(Compat::new(logger))
|
.wrap(Compat::new(logger))
|
||||||
@ -171,7 +171,7 @@ mod tests {
|
|||||||
.await;
|
.await;
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/app/test").to_request();
|
let req = TestRequest::with_uri("/app/test").to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -185,11 +185,11 @@ mod tests {
|
|||||||
|
|
||||||
let logger = Logger::default();
|
let logger = Logger::default();
|
||||||
|
|
||||||
let mut mw = Condition::new(true, Compat::new(logger))
|
let mw = Condition::new(true, Compat::new(logger))
|
||||||
.new_transform(srv.into_service())
|
.new_transform(srv.into_service())
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let resp = call_service(&mut mw, TestRequest::default().to_srv_request()).await;
|
let resp = call_service(&mw, TestRequest::default().to_srv_request()).await;
|
||||||
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -90,7 +90,7 @@ where
|
|||||||
actix_service::forward_ready!(service);
|
actix_service::forward_ready!(service);
|
||||||
|
|
||||||
#[allow(clippy::borrow_interior_mutable_const)]
|
#[allow(clippy::borrow_interior_mutable_const)]
|
||||||
fn call(&mut self, req: ServiceRequest) -> Self::Future {
|
fn call(&self, req: ServiceRequest) -> Self::Future {
|
||||||
// negotiate content-encoding
|
// negotiate content-encoding
|
||||||
let encoding = if let Some(val) = req.headers().get(&ACCEPT_ENCODING) {
|
let encoding = if let Some(val) = req.headers().get(&ACCEPT_ENCODING) {
|
||||||
if let Ok(enc) = val.to_str() {
|
if let Ok(enc) = val.to_str() {
|
||||||
|
@ -76,14 +76,14 @@ where
|
|||||||
type Error = E::Error;
|
type Error = E::Error;
|
||||||
type Future = Either<E::Future, D::Future>;
|
type Future = Either<E::Future, D::Future>;
|
||||||
|
|
||||||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
match self {
|
match self {
|
||||||
ConditionMiddleware::Enable(service) => service.poll_ready(cx),
|
ConditionMiddleware::Enable(service) => service.poll_ready(cx),
|
||||||
ConditionMiddleware::Disable(service) => service.poll_ready(cx),
|
ConditionMiddleware::Disable(service) => service.poll_ready(cx),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: Req) -> Self::Future {
|
fn call(&self, req: Req) -> Self::Future {
|
||||||
match self {
|
match self {
|
||||||
ConditionMiddleware::Enable(service) => Either::Left(service.call(req)),
|
ConditionMiddleware::Enable(service) => Either::Left(service.call(req)),
|
||||||
ConditionMiddleware::Disable(service) => Either::Right(service.call(req)),
|
ConditionMiddleware::Disable(service) => Either::Right(service.call(req)),
|
||||||
@ -123,12 +123,12 @@ mod tests {
|
|||||||
let mw =
|
let mw =
|
||||||
ErrorHandlers::new().handler(StatusCode::INTERNAL_SERVER_ERROR, render_500);
|
ErrorHandlers::new().handler(StatusCode::INTERNAL_SERVER_ERROR, render_500);
|
||||||
|
|
||||||
let mut mw = Condition::new(true, mw)
|
let mw = Condition::new(true, mw)
|
||||||
.new_transform(srv.into_service())
|
.new_transform(srv.into_service())
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let resp =
|
let resp =
|
||||||
test::call_service(&mut mw, TestRequest::default().to_srv_request()).await;
|
test::call_service(&mw, TestRequest::default().to_srv_request()).await;
|
||||||
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "0001");
|
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "0001");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -141,13 +141,13 @@ mod tests {
|
|||||||
let mw =
|
let mw =
|
||||||
ErrorHandlers::new().handler(StatusCode::INTERNAL_SERVER_ERROR, render_500);
|
ErrorHandlers::new().handler(StatusCode::INTERNAL_SERVER_ERROR, render_500);
|
||||||
|
|
||||||
let mut mw = Condition::new(false, mw)
|
let mw = Condition::new(false, mw)
|
||||||
.new_transform(srv.into_service())
|
.new_transform(srv.into_service())
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let resp =
|
let resp =
|
||||||
test::call_service(&mut mw, TestRequest::default().to_srv_request()).await;
|
test::call_service(&mw, TestRequest::default().to_srv_request()).await;
|
||||||
assert_eq!(resp.headers().get(CONTENT_TYPE), None);
|
assert_eq!(resp.headers().get(CONTENT_TYPE), None);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -143,7 +143,7 @@ where
|
|||||||
|
|
||||||
actix_service::forward_ready!(service);
|
actix_service::forward_ready!(service);
|
||||||
|
|
||||||
fn call(&mut self, req: ServiceRequest) -> Self::Future {
|
fn call(&self, req: ServiceRequest) -> Self::Future {
|
||||||
let inner = self.inner.clone();
|
let inner = self.inner.clone();
|
||||||
let fut = self.service.call(req);
|
let fut = self.service.call(req);
|
||||||
|
|
||||||
@ -200,7 +200,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_default_headers() {
|
async fn test_default_headers() {
|
||||||
let mut mw = DefaultHeaders::new()
|
let mw = DefaultHeaders::new()
|
||||||
.header(CONTENT_TYPE, "0001")
|
.header(CONTENT_TYPE, "0001")
|
||||||
.new_transform(ok_service())
|
.new_transform(ok_service())
|
||||||
.await
|
.await
|
||||||
@ -218,7 +218,7 @@ mod tests {
|
|||||||
.finish(),
|
.finish(),
|
||||||
))
|
))
|
||||||
};
|
};
|
||||||
let mut mw = DefaultHeaders::new()
|
let mw = DefaultHeaders::new()
|
||||||
.header(CONTENT_TYPE, "0001")
|
.header(CONTENT_TYPE, "0001")
|
||||||
.new_transform(srv.into_service())
|
.new_transform(srv.into_service())
|
||||||
.await
|
.await
|
||||||
@ -231,7 +231,7 @@ mod tests {
|
|||||||
async fn test_content_type() {
|
async fn test_content_type() {
|
||||||
let srv =
|
let srv =
|
||||||
|req: ServiceRequest| ok(req.into_response(HttpResponse::Ok().finish()));
|
|req: ServiceRequest| ok(req.into_response(HttpResponse::Ok().finish()));
|
||||||
let mut mw = DefaultHeaders::new()
|
let mw = DefaultHeaders::new()
|
||||||
.add_content_type()
|
.add_content_type()
|
||||||
.new_transform(srv.into_service())
|
.new_transform(srv.into_service())
|
||||||
.await
|
.await
|
||||||
|
@ -123,7 +123,7 @@ where
|
|||||||
|
|
||||||
actix_service::forward_ready!(service);
|
actix_service::forward_ready!(service);
|
||||||
|
|
||||||
fn call(&mut self, req: ServiceRequest) -> Self::Future {
|
fn call(&self, req: ServiceRequest) -> Self::Future {
|
||||||
let handlers = self.handlers.clone();
|
let handlers = self.handlers.clone();
|
||||||
let fut = self.service.call(req);
|
let fut = self.service.call(req);
|
||||||
ErrorHandlersFuture::ServiceFuture { fut, handlers }
|
ErrorHandlersFuture::ServiceFuture { fut, handlers }
|
||||||
@ -196,14 +196,14 @@ mod tests {
|
|||||||
ok(req.into_response(HttpResponse::InternalServerError().finish()))
|
ok(req.into_response(HttpResponse::InternalServerError().finish()))
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut mw = ErrorHandlers::new()
|
let mw = ErrorHandlers::new()
|
||||||
.handler(StatusCode::INTERNAL_SERVER_ERROR, render_500)
|
.handler(StatusCode::INTERNAL_SERVER_ERROR, render_500)
|
||||||
.new_transform(srv.into_service())
|
.new_transform(srv.into_service())
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let resp =
|
let resp =
|
||||||
test::call_service(&mut mw, TestRequest::default().to_srv_request()).await;
|
test::call_service(&mw, TestRequest::default().to_srv_request()).await;
|
||||||
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "0001");
|
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "0001");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -223,14 +223,14 @@ mod tests {
|
|||||||
ok(req.into_response(HttpResponse::InternalServerError().finish()))
|
ok(req.into_response(HttpResponse::InternalServerError().finish()))
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut mw = ErrorHandlers::new()
|
let mw = ErrorHandlers::new()
|
||||||
.handler(StatusCode::INTERNAL_SERVER_ERROR, render_500_async)
|
.handler(StatusCode::INTERNAL_SERVER_ERROR, render_500_async)
|
||||||
.new_transform(srv.into_service())
|
.new_transform(srv.into_service())
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let resp =
|
let resp =
|
||||||
test::call_service(&mut mw, TestRequest::default().to_srv_request()).await;
|
test::call_service(&mw, TestRequest::default().to_srv_request()).await;
|
||||||
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "0001");
|
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "0001");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -219,7 +219,7 @@ where
|
|||||||
|
|
||||||
actix_service::forward_ready!(service);
|
actix_service::forward_ready!(service);
|
||||||
|
|
||||||
fn call(&mut self, req: ServiceRequest) -> Self::Future {
|
fn call(&self, req: ServiceRequest) -> Self::Future {
|
||||||
if self.inner.exclude.contains(req.path())
|
if self.inner.exclude.contains(req.path())
|
||||||
|| self.inner.exclude_regex.is_match(req.path())
|
|| self.inner.exclude_regex.is_match(req.path())
|
||||||
{
|
{
|
||||||
@ -609,7 +609,7 @@ mod tests {
|
|||||||
};
|
};
|
||||||
let logger = Logger::new("%% %{User-Agent}i %{X-Test}o %{HOME}e %D test");
|
let logger = Logger::new("%% %{User-Agent}i %{X-Test}o %{HOME}e %D test");
|
||||||
|
|
||||||
let mut srv = logger.new_transform(srv.into_service()).await.unwrap();
|
let srv = logger.new_transform(srv.into_service()).await.unwrap();
|
||||||
|
|
||||||
let req = TestRequest::default()
|
let req = TestRequest::default()
|
||||||
.insert_header((
|
.insert_header((
|
||||||
@ -632,7 +632,7 @@ mod tests {
|
|||||||
let logger = Logger::new("%% %{User-Agent}i %{X-Test}o %{HOME}e %D test")
|
let logger = Logger::new("%% %{User-Agent}i %{X-Test}o %{HOME}e %D test")
|
||||||
.exclude_regex("\\w");
|
.exclude_regex("\\w");
|
||||||
|
|
||||||
let mut srv = logger.new_transform(srv.into_service()).await.unwrap();
|
let srv = logger.new_transform(srv.into_service()).await.unwrap();
|
||||||
|
|
||||||
let req = TestRequest::default()
|
let req = TestRequest::default()
|
||||||
.insert_header((
|
.insert_header((
|
||||||
@ -806,7 +806,7 @@ mod tests {
|
|||||||
captured.to_owned()
|
captured.to_owned()
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut srv = logger.new_transform(test::ok_service()).await.unwrap();
|
let srv = logger.new_transform(test::ok_service()).await.unwrap();
|
||||||
|
|
||||||
let req = TestRequest::default().to_srv_request();
|
let req = TestRequest::default().to_srv_request();
|
||||||
srv.call(req).await.unwrap();
|
srv.call(req).await.unwrap();
|
||||||
|
@ -57,7 +57,7 @@ impl Default for TrailingSlash {
|
|||||||
/// ```rust
|
/// ```rust
|
||||||
/// use actix_web::{web, middleware, App};
|
/// use actix_web::{web, middleware, App};
|
||||||
///
|
///
|
||||||
/// # actix_web::rt::System::new("doctest").block_on(async {
|
/// # actix_web::rt::System::new().block_on(async {
|
||||||
/// let app = App::new()
|
/// let app = App::new()
|
||||||
/// .wrap(middleware::NormalizePath::default())
|
/// .wrap(middleware::NormalizePath::default())
|
||||||
/// .route("/test", web::get().to(|| async { "test" }))
|
/// .route("/test", web::get().to(|| async { "test" }))
|
||||||
@ -66,22 +66,22 @@ impl Default for TrailingSlash {
|
|||||||
/// use actix_web::http::StatusCode;
|
/// use actix_web::http::StatusCode;
|
||||||
/// use actix_web::test::{call_service, init_service, TestRequest};
|
/// use actix_web::test::{call_service, init_service, TestRequest};
|
||||||
///
|
///
|
||||||
/// let mut app = init_service(app).await;
|
/// let app = init_service(app).await;
|
||||||
///
|
///
|
||||||
/// let req = TestRequest::with_uri("/test").to_request();
|
/// let req = TestRequest::with_uri("/test").to_request();
|
||||||
/// let res = call_service(&mut app, req).await;
|
/// let res = call_service(&app, req).await;
|
||||||
/// assert_eq!(res.status(), StatusCode::OK);
|
/// assert_eq!(res.status(), StatusCode::OK);
|
||||||
///
|
///
|
||||||
/// let req = TestRequest::with_uri("/test/").to_request();
|
/// let req = TestRequest::with_uri("/test/").to_request();
|
||||||
/// let res = call_service(&mut app, req).await;
|
/// let res = call_service(&app, req).await;
|
||||||
/// assert_eq!(res.status(), StatusCode::OK);
|
/// assert_eq!(res.status(), StatusCode::OK);
|
||||||
///
|
///
|
||||||
/// let req = TestRequest::with_uri("/unmatchable").to_request();
|
/// let req = TestRequest::with_uri("/unmatchable").to_request();
|
||||||
/// let res = call_service(&mut app, req).await;
|
/// let res = call_service(&app, req).await;
|
||||||
/// assert_eq!(res.status(), StatusCode::NOT_FOUND);
|
/// assert_eq!(res.status(), StatusCode::NOT_FOUND);
|
||||||
///
|
///
|
||||||
/// let req = TestRequest::with_uri("/unmatchable/").to_request();
|
/// let req = TestRequest::with_uri("/unmatchable/").to_request();
|
||||||
/// let res = call_service(&mut app, req).await;
|
/// let res = call_service(&app, req).await;
|
||||||
/// assert_eq!(res.status(), StatusCode::NOT_FOUND);
|
/// assert_eq!(res.status(), StatusCode::NOT_FOUND);
|
||||||
/// # })
|
/// # })
|
||||||
/// ```
|
/// ```
|
||||||
@ -132,7 +132,7 @@ where
|
|||||||
|
|
||||||
actix_service::forward_ready!(service);
|
actix_service::forward_ready!(service);
|
||||||
|
|
||||||
fn call(&mut self, mut req: ServiceRequest) -> Self::Future {
|
fn call(&self, mut req: ServiceRequest) -> Self::Future {
|
||||||
let head = req.head_mut();
|
let head = req.head_mut();
|
||||||
|
|
||||||
let original_path = head.uri.path();
|
let original_path = head.uri.path();
|
||||||
@ -195,7 +195,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_wrap() {
|
async fn test_wrap() {
|
||||||
let mut app = init_service(
|
let app = init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.wrap(NormalizePath::default())
|
.wrap(NormalizePath::default())
|
||||||
.service(web::resource("/").to(HttpResponse::Ok))
|
.service(web::resource("/").to(HttpResponse::Ok))
|
||||||
@ -204,37 +204,37 @@ mod tests {
|
|||||||
.await;
|
.await;
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/").to_request();
|
let req = TestRequest::with_uri("/").to_request();
|
||||||
let res = call_service(&mut app, req).await;
|
let res = call_service(&app, req).await;
|
||||||
assert!(res.status().is_success());
|
assert!(res.status().is_success());
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/?query=test").to_request();
|
let req = TestRequest::with_uri("/?query=test").to_request();
|
||||||
let res = call_service(&mut app, req).await;
|
let res = call_service(&app, req).await;
|
||||||
assert!(res.status().is_success());
|
assert!(res.status().is_success());
|
||||||
|
|
||||||
let req = TestRequest::with_uri("///").to_request();
|
let req = TestRequest::with_uri("///").to_request();
|
||||||
let res = call_service(&mut app, req).await;
|
let res = call_service(&app, req).await;
|
||||||
assert!(res.status().is_success());
|
assert!(res.status().is_success());
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/v1//something////").to_request();
|
let req = TestRequest::with_uri("/v1//something////").to_request();
|
||||||
let res = call_service(&mut app, req).await;
|
let res = call_service(&app, req).await;
|
||||||
assert!(res.status().is_success());
|
assert!(res.status().is_success());
|
||||||
|
|
||||||
let req2 = TestRequest::with_uri("//v1/something").to_request();
|
let req2 = TestRequest::with_uri("//v1/something").to_request();
|
||||||
let res2 = call_service(&mut app, req2).await;
|
let res2 = call_service(&app, req2).await;
|
||||||
assert!(res2.status().is_success());
|
assert!(res2.status().is_success());
|
||||||
|
|
||||||
let req3 = TestRequest::with_uri("//v1//////something").to_request();
|
let req3 = TestRequest::with_uri("//v1//////something").to_request();
|
||||||
let res3 = call_service(&mut app, req3).await;
|
let res3 = call_service(&app, req3).await;
|
||||||
assert!(res3.status().is_success());
|
assert!(res3.status().is_success());
|
||||||
|
|
||||||
let req4 = TestRequest::with_uri("/v1//something").to_request();
|
let req4 = TestRequest::with_uri("/v1//something").to_request();
|
||||||
let res4 = call_service(&mut app, req4).await;
|
let res4 = call_service(&app, req4).await;
|
||||||
assert!(res4.status().is_success());
|
assert!(res4.status().is_success());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn trim_trailing_slashes() {
|
async fn trim_trailing_slashes() {
|
||||||
let mut app = init_service(
|
let app = init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.wrap(NormalizePath(TrailingSlash::Trim))
|
.wrap(NormalizePath(TrailingSlash::Trim))
|
||||||
.service(web::resource("/").to(HttpResponse::Ok))
|
.service(web::resource("/").to(HttpResponse::Ok))
|
||||||
@ -244,37 +244,37 @@ mod tests {
|
|||||||
|
|
||||||
// root paths should still work
|
// root paths should still work
|
||||||
let req = TestRequest::with_uri("/").to_request();
|
let req = TestRequest::with_uri("/").to_request();
|
||||||
let res = call_service(&mut app, req).await;
|
let res = call_service(&app, req).await;
|
||||||
assert!(res.status().is_success());
|
assert!(res.status().is_success());
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/?query=test").to_request();
|
let req = TestRequest::with_uri("/?query=test").to_request();
|
||||||
let res = call_service(&mut app, req).await;
|
let res = call_service(&app, req).await;
|
||||||
assert!(res.status().is_success());
|
assert!(res.status().is_success());
|
||||||
|
|
||||||
let req = TestRequest::with_uri("///").to_request();
|
let req = TestRequest::with_uri("///").to_request();
|
||||||
let res = call_service(&mut app, req).await;
|
let res = call_service(&app, req).await;
|
||||||
assert!(res.status().is_success());
|
assert!(res.status().is_success());
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/v1/something////").to_request();
|
let req = TestRequest::with_uri("/v1/something////").to_request();
|
||||||
let res = call_service(&mut app, req).await;
|
let res = call_service(&app, req).await;
|
||||||
assert!(res.status().is_success());
|
assert!(res.status().is_success());
|
||||||
|
|
||||||
let req2 = TestRequest::with_uri("/v1/something/").to_request();
|
let req2 = TestRequest::with_uri("/v1/something/").to_request();
|
||||||
let res2 = call_service(&mut app, req2).await;
|
let res2 = call_service(&app, req2).await;
|
||||||
assert!(res2.status().is_success());
|
assert!(res2.status().is_success());
|
||||||
|
|
||||||
let req3 = TestRequest::with_uri("//v1//something//").to_request();
|
let req3 = TestRequest::with_uri("//v1//something//").to_request();
|
||||||
let res3 = call_service(&mut app, req3).await;
|
let res3 = call_service(&app, req3).await;
|
||||||
assert!(res3.status().is_success());
|
assert!(res3.status().is_success());
|
||||||
|
|
||||||
let req4 = TestRequest::with_uri("//v1//something").to_request();
|
let req4 = TestRequest::with_uri("//v1//something").to_request();
|
||||||
let res4 = call_service(&mut app, req4).await;
|
let res4 = call_service(&app, req4).await;
|
||||||
assert!(res4.status().is_success());
|
assert!(res4.status().is_success());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn keep_trailing_slash_unchanged() {
|
async fn keep_trailing_slash_unchanged() {
|
||||||
let mut app = init_service(
|
let app = init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.wrap(NormalizePath(TrailingSlash::MergeOnly))
|
.wrap(NormalizePath(TrailingSlash::MergeOnly))
|
||||||
.service(web::resource("/").to(HttpResponse::Ok))
|
.service(web::resource("/").to(HttpResponse::Ok))
|
||||||
@ -299,7 +299,7 @@ mod tests {
|
|||||||
|
|
||||||
for (path, success) in tests {
|
for (path, success) in tests {
|
||||||
let req = TestRequest::with_uri(path).to_request();
|
let req = TestRequest::with_uri(path).to_request();
|
||||||
let res = call_service(&mut app, req).await;
|
let res = call_service(&app, req).await;
|
||||||
assert_eq!(res.status().is_success(), success);
|
assert_eq!(res.status().is_success(), success);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -311,7 +311,7 @@ mod tests {
|
|||||||
ready(Ok(req.into_response(HttpResponse::Ok().finish())))
|
ready(Ok(req.into_response(HttpResponse::Ok().finish())))
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut normalize = NormalizePath::default()
|
let normalize = NormalizePath::default()
|
||||||
.new_transform(srv.into_service())
|
.new_transform(srv.into_service())
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
@ -342,7 +342,7 @@ mod tests {
|
|||||||
ready(Ok(req.into_response(HttpResponse::Ok().finish())))
|
ready(Ok(req.into_response(HttpResponse::Ok().finish())))
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut normalize = NormalizePath::default()
|
let normalize = NormalizePath::default()
|
||||||
.new_transform(srv.into_service())
|
.new_transform(srv.into_service())
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
@ -359,7 +359,7 @@ mod tests {
|
|||||||
ready(Ok(req.into_response(HttpResponse::Ok().finish())))
|
ready(Ok(req.into_response(HttpResponse::Ok().finish())))
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut normalize = NormalizePath::default()
|
let normalize = NormalizePath::default()
|
||||||
.new_transform(srv.into_service())
|
.new_transform(srv.into_service())
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
@ -556,7 +556,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_drop_http_request_pool() {
|
async fn test_drop_http_request_pool() {
|
||||||
let mut srv = init_service(App::new().service(web::resource("/").to(
|
let srv = init_service(App::new().service(web::resource("/").to(
|
||||||
|req: HttpRequest| {
|
|req: HttpRequest| {
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
.insert_header(("pool_cap", req.app_state().pool().cap))
|
.insert_header(("pool_cap", req.app_state().pool().cap))
|
||||||
@ -566,7 +566,7 @@ mod tests {
|
|||||||
.await;
|
.await;
|
||||||
|
|
||||||
let req = TestRequest::default().to_request();
|
let req = TestRequest::default().to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
|
|
||||||
drop(srv);
|
drop(srv);
|
||||||
|
|
||||||
@ -575,7 +575,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_data() {
|
async fn test_data() {
|
||||||
let mut srv = init_service(App::new().app_data(10usize).service(
|
let srv = init_service(App::new().app_data(10usize).service(
|
||||||
web::resource("/").to(|req: HttpRequest| {
|
web::resource("/").to(|req: HttpRequest| {
|
||||||
if req.app_data::<usize>().is_some() {
|
if req.app_data::<usize>().is_some() {
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
@ -587,10 +587,10 @@ mod tests {
|
|||||||
.await;
|
.await;
|
||||||
|
|
||||||
let req = TestRequest::default().to_request();
|
let req = TestRequest::default().to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
let mut srv = init_service(App::new().app_data(10u32).service(
|
let srv = init_service(App::new().app_data(10u32).service(
|
||||||
web::resource("/").to(|req: HttpRequest| {
|
web::resource("/").to(|req: HttpRequest| {
|
||||||
if req.app_data::<usize>().is_some() {
|
if req.app_data::<usize>().is_some() {
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
@ -602,7 +602,7 @@ mod tests {
|
|||||||
.await;
|
.await;
|
||||||
|
|
||||||
let req = TestRequest::default().to_request();
|
let req = TestRequest::default().to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -614,7 +614,7 @@ mod tests {
|
|||||||
HttpResponse::Ok().body(num.to_string())
|
HttpResponse::Ok().body(num.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.app_data(88usize)
|
.app_data(88usize)
|
||||||
.service(web::resource("/").route(web::get().to(echo_usize)))
|
.service(web::resource("/").route(web::get().to(echo_usize)))
|
||||||
@ -645,7 +645,7 @@ mod tests {
|
|||||||
HttpResponse::Ok().body(num.to_string())
|
HttpResponse::Ok().body(num.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.app_data(88usize)
|
.app_data(88usize)
|
||||||
.service(web::resource("/").route(web::get().to(echo_usize)))
|
.service(web::resource("/").route(web::get().to(echo_usize)))
|
||||||
@ -685,7 +685,7 @@ mod tests {
|
|||||||
let tracker = Rc::new(RefCell::new(Tracker { dropped: false }));
|
let tracker = Rc::new(RefCell::new(Tracker { dropped: false }));
|
||||||
{
|
{
|
||||||
let tracker2 = Rc::clone(&tracker);
|
let tracker2 = Rc::clone(&tracker);
|
||||||
let mut srv = init_service(App::new().data(10u32).service(
|
let srv = init_service(App::new().data(10u32).service(
|
||||||
web::resource("/").to(move |req: HttpRequest| {
|
web::resource("/").to(move |req: HttpRequest| {
|
||||||
req.extensions_mut().insert(Foo {
|
req.extensions_mut().insert(Foo {
|
||||||
tracker: Rc::clone(&tracker2),
|
tracker: Rc::clone(&tracker2),
|
||||||
@ -696,7 +696,7 @@ mod tests {
|
|||||||
.await;
|
.await;
|
||||||
|
|
||||||
let req = TestRequest::default().to_request();
|
let req = TestRequest::default().to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -705,7 +705,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn extract_path_pattern() {
|
async fn extract_path_pattern() {
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new().service(
|
App::new().service(
|
||||||
web::scope("/user/{id}")
|
web::scope("/user/{id}")
|
||||||
.service(web::resource("/profile").route(web::get().to(
|
.service(web::resource("/profile").route(web::get().to(
|
||||||
@ -727,17 +727,17 @@ mod tests {
|
|||||||
.await;
|
.await;
|
||||||
|
|
||||||
let req = TestRequest::get().uri("/user/22/profile").to_request();
|
let req = TestRequest::get().uri("/user/22/profile").to_request();
|
||||||
let res = call_service(&mut srv, req).await;
|
let res = call_service(&srv, req).await;
|
||||||
assert_eq!(res.status(), StatusCode::OK);
|
assert_eq!(res.status(), StatusCode::OK);
|
||||||
|
|
||||||
let req = TestRequest::get().uri("/user/22/not-exist").to_request();
|
let req = TestRequest::get().uri("/user/22/not-exist").to_request();
|
||||||
let res = call_service(&mut srv, req).await;
|
let res = call_service(&srv, req).await;
|
||||||
assert_eq!(res.status(), StatusCode::OK);
|
assert_eq!(res.status(), StatusCode::OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn extract_path_pattern_complex() {
|
async fn extract_path_pattern_complex() {
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.service(web::scope("/user").service(web::scope("/{id}").service(
|
.service(web::scope("/user").service(web::scope("/{id}").service(
|
||||||
web::resource("").to(move |req: HttpRequest| {
|
web::resource("").to(move |req: HttpRequest| {
|
||||||
@ -759,15 +759,15 @@ mod tests {
|
|||||||
.await;
|
.await;
|
||||||
|
|
||||||
let req = TestRequest::get().uri("/user/test").to_request();
|
let req = TestRequest::get().uri("/user/test").to_request();
|
||||||
let res = call_service(&mut srv, req).await;
|
let res = call_service(&srv, req).await;
|
||||||
assert_eq!(res.status(), StatusCode::OK);
|
assert_eq!(res.status(), StatusCode::OK);
|
||||||
|
|
||||||
let req = TestRequest::get().uri("/").to_request();
|
let req = TestRequest::get().uri("/").to_request();
|
||||||
let res = call_service(&mut srv, req).await;
|
let res = call_service(&srv, req).await;
|
||||||
assert_eq!(res.status(), StatusCode::OK);
|
assert_eq!(res.status(), StatusCode::OK);
|
||||||
|
|
||||||
let req = TestRequest::get().uri("/not-exist").to_request();
|
let req = TestRequest::get().uri("/not-exist").to_request();
|
||||||
let res = call_service(&mut srv, req).await;
|
let res = call_service(&srv, req).await;
|
||||||
assert_eq!(res.status(), StatusCode::OK);
|
assert_eq!(res.status(), StatusCode::OK);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -102,7 +102,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn req_data_extractor() {
|
async fn req_data_extractor() {
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.wrap_fn(|req, srv| {
|
.wrap_fn(|req, srv| {
|
||||||
if req.method() == Method::POST {
|
if req.method() == Method::POST {
|
||||||
@ -142,7 +142,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn req_data_internal_mutability() {
|
async fn req_data_internal_mutability() {
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.wrap_fn(|req, srv| {
|
.wrap_fn(|req, srv| {
|
||||||
let data_before = Rc::new(RefCell::new(42u32));
|
let data_before = Rc::new(RefCell::new(42u32));
|
||||||
|
@ -328,7 +328,7 @@ where
|
|||||||
>,
|
>,
|
||||||
>
|
>
|
||||||
where
|
where
|
||||||
F: FnMut(ServiceRequest, &mut T::Service) -> R + Clone,
|
F: Fn(ServiceRequest, &T::Service) -> R + Clone,
|
||||||
R: Future<Output = Result<ServiceResponse, Error>>,
|
R: Future<Output = Result<ServiceResponse, Error>>,
|
||||||
{
|
{
|
||||||
Resource {
|
Resource {
|
||||||
@ -471,8 +471,8 @@ impl Service<ServiceRequest> for ResourceService {
|
|||||||
|
|
||||||
actix_service::always_ready!();
|
actix_service::always_ready!();
|
||||||
|
|
||||||
fn call(&mut self, mut req: ServiceRequest) -> Self::Future {
|
fn call(&self, mut req: ServiceRequest) -> Self::Future {
|
||||||
for route in self.routes.iter_mut() {
|
for route in self.routes.iter() {
|
||||||
if route.check(&mut req) {
|
if route.check(&mut req) {
|
||||||
if let Some(ref app_data) = self.app_data {
|
if let Some(ref app_data) = self.app_data {
|
||||||
req.add_data_container(app_data.clone());
|
req.add_data_container(app_data.clone());
|
||||||
@ -530,7 +530,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_middleware() {
|
async fn test_middleware() {
|
||||||
let mut srv =
|
let srv =
|
||||||
init_service(
|
init_service(
|
||||||
App::new().service(
|
App::new().service(
|
||||||
web::resource("/test")
|
web::resource("/test")
|
||||||
@ -544,7 +544,7 @@ mod tests {
|
|||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
let req = TestRequest::with_uri("/test").to_request();
|
let req = TestRequest::with_uri("/test").to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
||||||
@ -554,7 +554,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_middleware_fn() {
|
async fn test_middleware_fn() {
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new().service(
|
App::new().service(
|
||||||
web::resource("/test")
|
web::resource("/test")
|
||||||
.wrap_fn(|req, srv| {
|
.wrap_fn(|req, srv| {
|
||||||
@ -574,7 +574,7 @@ mod tests {
|
|||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
let req = TestRequest::with_uri("/test").to_request();
|
let req = TestRequest::with_uri("/test").to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
||||||
@ -584,20 +584,20 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_to() {
|
async fn test_to() {
|
||||||
let mut srv =
|
let srv =
|
||||||
init_service(App::new().service(web::resource("/test").to(|| async {
|
init_service(App::new().service(web::resource("/test").to(|| async {
|
||||||
sleep(Duration::from_millis(100)).await;
|
sleep(Duration::from_millis(100)).await;
|
||||||
Ok::<_, Error>(HttpResponse::Ok())
|
Ok::<_, Error>(HttpResponse::Ok())
|
||||||
})))
|
})))
|
||||||
.await;
|
.await;
|
||||||
let req = TestRequest::with_uri("/test").to_request();
|
let req = TestRequest::with_uri("/test").to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_pattern() {
|
async fn test_pattern() {
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new().service(
|
App::new().service(
|
||||||
web::resource(["/test", "/test2"])
|
web::resource(["/test", "/test2"])
|
||||||
.to(|| async { Ok::<_, Error>(HttpResponse::Ok()) }),
|
.to(|| async { Ok::<_, Error>(HttpResponse::Ok()) }),
|
||||||
@ -605,16 +605,16 @@ mod tests {
|
|||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
let req = TestRequest::with_uri("/test").to_request();
|
let req = TestRequest::with_uri("/test").to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
let req = TestRequest::with_uri("/test2").to_request();
|
let req = TestRequest::with_uri("/test2").to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_default_resource() {
|
async fn test_default_resource() {
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.service(web::resource("/test").route(web::get().to(HttpResponse::Ok)))
|
.service(web::resource("/test").route(web::get().to(HttpResponse::Ok)))
|
||||||
.default_service(|r: ServiceRequest| {
|
.default_service(|r: ServiceRequest| {
|
||||||
@ -623,16 +623,16 @@ mod tests {
|
|||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
let req = TestRequest::with_uri("/test").to_request();
|
let req = TestRequest::with_uri("/test").to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/test")
|
let req = TestRequest::with_uri("/test")
|
||||||
.method(Method::POST)
|
.method(Method::POST)
|
||||||
.to_request();
|
.to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);
|
assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);
|
||||||
|
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new().service(
|
App::new().service(
|
||||||
web::resource("/test")
|
web::resource("/test")
|
||||||
.route(web::get().to(HttpResponse::Ok))
|
.route(web::get().to(HttpResponse::Ok))
|
||||||
@ -644,19 +644,19 @@ mod tests {
|
|||||||
.await;
|
.await;
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/test").to_request();
|
let req = TestRequest::with_uri("/test").to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/test")
|
let req = TestRequest::with_uri("/test")
|
||||||
.method(Method::POST)
|
.method(Method::POST)
|
||||||
.to_request();
|
.to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_resource_guards() {
|
async fn test_resource_guards() {
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.service(
|
.service(
|
||||||
web::resource("/test/{p}")
|
web::resource("/test/{p}")
|
||||||
@ -679,25 +679,25 @@ mod tests {
|
|||||||
let req = TestRequest::with_uri("/test/it")
|
let req = TestRequest::with_uri("/test/it")
|
||||||
.method(Method::GET)
|
.method(Method::GET)
|
||||||
.to_request();
|
.to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/test/it")
|
let req = TestRequest::with_uri("/test/it")
|
||||||
.method(Method::PUT)
|
.method(Method::PUT)
|
||||||
.to_request();
|
.to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::CREATED);
|
assert_eq!(resp.status(), StatusCode::CREATED);
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/test/it")
|
let req = TestRequest::with_uri("/test/it")
|
||||||
.method(Method::DELETE)
|
.method(Method::DELETE)
|
||||||
.to_request();
|
.to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::NO_CONTENT);
|
assert_eq!(resp.status(), StatusCode::NO_CONTENT);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_data() {
|
async fn test_data() {
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.data(1.0f64)
|
.data(1.0f64)
|
||||||
.data(1usize)
|
.data(1usize)
|
||||||
@ -723,13 +723,13 @@ mod tests {
|
|||||||
.await;
|
.await;
|
||||||
|
|
||||||
let req = TestRequest::get().uri("/test").to_request();
|
let req = TestRequest::get().uri("/test").to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_data_default_service() {
|
async fn test_data_default_service() {
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new().data(1usize).service(
|
App::new().data(1usize).service(
|
||||||
web::resource("/test")
|
web::resource("/test")
|
||||||
.data(10usize)
|
.data(10usize)
|
||||||
@ -742,7 +742,7 @@ mod tests {
|
|||||||
.await;
|
.await;
|
||||||
|
|
||||||
let req = TestRequest::get().uri("/test").to_request();
|
let req = TestRequest::get().uri("/test").to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -260,7 +260,7 @@ pub(crate) mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_option_responder() {
|
async fn test_option_responder() {
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.service(
|
.service(
|
||||||
web::resource("/none").to(|| async { Option::<&'static str>::None }),
|
web::resource("/none").to(|| async { Option::<&'static str>::None }),
|
||||||
|
22
src/route.rs
22
src/route.rs
@ -121,11 +121,11 @@ impl Service<ServiceRequest> for RouteService {
|
|||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
|
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
|
||||||
|
|
||||||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
self.service.poll_ready(cx)
|
self.service.poll_ready(cx)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: ServiceRequest) -> Self::Future {
|
fn call(&self, req: ServiceRequest) -> Self::Future {
|
||||||
self.service.call(req)
|
self.service.call(req)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -299,11 +299,11 @@ where
|
|||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
|
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
|
||||||
|
|
||||||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
self.service.poll_ready(cx)
|
self.service.poll_ready(cx)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: ServiceRequest) -> Self::Future {
|
fn call(&self, req: ServiceRequest) -> Self::Future {
|
||||||
Box::pin(self.service.call(req))
|
Box::pin(self.service.call(req))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -327,7 +327,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_route() {
|
async fn test_route() {
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.service(
|
.service(
|
||||||
web::resource("/test")
|
web::resource("/test")
|
||||||
@ -356,35 +356,35 @@ mod tests {
|
|||||||
let req = TestRequest::with_uri("/test")
|
let req = TestRequest::with_uri("/test")
|
||||||
.method(Method::GET)
|
.method(Method::GET)
|
||||||
.to_request();
|
.to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/test")
|
let req = TestRequest::with_uri("/test")
|
||||||
.method(Method::POST)
|
.method(Method::POST)
|
||||||
.to_request();
|
.to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::CREATED);
|
assert_eq!(resp.status(), StatusCode::CREATED);
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/test")
|
let req = TestRequest::with_uri("/test")
|
||||||
.method(Method::PUT)
|
.method(Method::PUT)
|
||||||
.to_request();
|
.to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/test")
|
let req = TestRequest::with_uri("/test")
|
||||||
.method(Method::DELETE)
|
.method(Method::DELETE)
|
||||||
.to_request();
|
.to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/test")
|
let req = TestRequest::with_uri("/test")
|
||||||
.method(Method::HEAD)
|
.method(Method::HEAD)
|
||||||
.to_request();
|
.to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);
|
assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/json").to_request();
|
let req = TestRequest::with_uri("/json").to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
let body = read_body(resp).await;
|
let body = read_body(resp).await;
|
||||||
|
129
src/scope.rs
129
src/scope.rs
@ -380,7 +380,7 @@ where
|
|||||||
>,
|
>,
|
||||||
>
|
>
|
||||||
where
|
where
|
||||||
F: FnMut(ServiceRequest, &mut T::Service) -> R + Clone,
|
F: Fn(ServiceRequest, &T::Service) -> R + Clone,
|
||||||
R: Future<Output = Result<ServiceResponse, Error>>,
|
R: Future<Output = Result<ServiceResponse, Error>>,
|
||||||
{
|
{
|
||||||
Scope {
|
Scope {
|
||||||
@ -526,8 +526,8 @@ impl Service<ServiceRequest> for ScopeService {
|
|||||||
|
|
||||||
actix_service::always_ready!();
|
actix_service::always_ready!();
|
||||||
|
|
||||||
fn call(&mut self, mut req: ServiceRequest) -> Self::Future {
|
fn call(&self, mut req: ServiceRequest) -> Self::Future {
|
||||||
let res = self.router.recognize_mut_checked(&mut req, |req, guards| {
|
let res = self.router.recognize_checked(&mut req, |req, guards| {
|
||||||
if let Some(ref guards) = guards {
|
if let Some(ref guards) = guards {
|
||||||
for f in guards {
|
for f in guards {
|
||||||
if !f.check(req.head()) {
|
if !f.check(req.head()) {
|
||||||
@ -589,7 +589,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_scope() {
|
async fn test_scope() {
|
||||||
let mut srv = init_service(App::new().service(
|
let srv = init_service(App::new().service(
|
||||||
web::scope("/app").service(web::resource("/path1").to(HttpResponse::Ok)),
|
web::scope("/app").service(web::resource("/path1").to(HttpResponse::Ok)),
|
||||||
))
|
))
|
||||||
.await;
|
.await;
|
||||||
@ -601,7 +601,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_scope_root() {
|
async fn test_scope_root() {
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new().service(
|
App::new().service(
|
||||||
web::scope("/app")
|
web::scope("/app")
|
||||||
.service(web::resource("").to(HttpResponse::Ok))
|
.service(web::resource("").to(HttpResponse::Ok))
|
||||||
@ -621,7 +621,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_scope_root2() {
|
async fn test_scope_root2() {
|
||||||
let mut srv = init_service(App::new().service(
|
let srv = init_service(App::new().service(
|
||||||
web::scope("/app/").service(web::resource("").to(HttpResponse::Ok)),
|
web::scope("/app/").service(web::resource("").to(HttpResponse::Ok)),
|
||||||
))
|
))
|
||||||
.await;
|
.await;
|
||||||
@ -637,7 +637,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_scope_root3() {
|
async fn test_scope_root3() {
|
||||||
let mut srv = init_service(App::new().service(
|
let srv = init_service(App::new().service(
|
||||||
web::scope("/app/").service(web::resource("/").to(HttpResponse::Ok)),
|
web::scope("/app/").service(web::resource("/").to(HttpResponse::Ok)),
|
||||||
))
|
))
|
||||||
.await;
|
.await;
|
||||||
@ -653,7 +653,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_scope_route() {
|
async fn test_scope_route() {
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new().service(
|
App::new().service(
|
||||||
web::scope("app")
|
web::scope("app")
|
||||||
.route("/path1", web::get().to(HttpResponse::Ok))
|
.route("/path1", web::get().to(HttpResponse::Ok))
|
||||||
@ -681,7 +681,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_scope_route_without_leading_slash() {
|
async fn test_scope_route_without_leading_slash() {
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new().service(
|
App::new().service(
|
||||||
web::scope("app").service(
|
web::scope("app").service(
|
||||||
web::resource("path1")
|
web::resource("path1")
|
||||||
@ -711,7 +711,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_scope_guard() {
|
async fn test_scope_guard() {
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new().service(
|
App::new().service(
|
||||||
web::scope("/app")
|
web::scope("/app")
|
||||||
.guard(guard::Get())
|
.guard(guard::Get())
|
||||||
@ -735,14 +735,13 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_scope_variable_segment() {
|
async fn test_scope_variable_segment() {
|
||||||
let mut srv =
|
let srv = init_service(App::new().service(web::scope("/ab-{project}").service(
|
||||||
init_service(App::new().service(web::scope("/ab-{project}").service(
|
web::resource("/path1").to(|r: HttpRequest| {
|
||||||
web::resource("/path1").to(|r: HttpRequest| {
|
HttpResponse::Ok()
|
||||||
HttpResponse::Ok()
|
.body(format!("project: {}", &r.match_info()["project"]))
|
||||||
.body(format!("project: {}", &r.match_info()["project"]))
|
}),
|
||||||
}),
|
)))
|
||||||
)))
|
.await;
|
||||||
.await;
|
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/ab-project1/path1").to_request();
|
let req = TestRequest::with_uri("/ab-project1/path1").to_request();
|
||||||
let resp = srv.call(req).await.unwrap();
|
let resp = srv.call(req).await.unwrap();
|
||||||
@ -763,7 +762,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_nested_scope() {
|
async fn test_nested_scope() {
|
||||||
let mut srv = init_service(App::new().service(web::scope("/app").service(
|
let srv = init_service(App::new().service(web::scope("/app").service(
|
||||||
web::scope("/t1").service(web::resource("/path1").to(HttpResponse::Created)),
|
web::scope("/t1").service(web::resource("/path1").to(HttpResponse::Created)),
|
||||||
)))
|
)))
|
||||||
.await;
|
.await;
|
||||||
@ -775,7 +774,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_nested_scope_no_slash() {
|
async fn test_nested_scope_no_slash() {
|
||||||
let mut srv = init_service(App::new().service(web::scope("/app").service(
|
let srv = init_service(App::new().service(web::scope("/app").service(
|
||||||
web::scope("t1").service(web::resource("/path1").to(HttpResponse::Created)),
|
web::scope("t1").service(web::resource("/path1").to(HttpResponse::Created)),
|
||||||
)))
|
)))
|
||||||
.await;
|
.await;
|
||||||
@ -787,7 +786,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_nested_scope_root() {
|
async fn test_nested_scope_root() {
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new().service(
|
App::new().service(
|
||||||
web::scope("/app").service(
|
web::scope("/app").service(
|
||||||
web::scope("/t1")
|
web::scope("/t1")
|
||||||
@ -809,7 +808,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_nested_scope_filter() {
|
async fn test_nested_scope_filter() {
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new().service(
|
App::new().service(
|
||||||
web::scope("/app").service(
|
web::scope("/app").service(
|
||||||
web::scope("/t1")
|
web::scope("/t1")
|
||||||
@ -835,7 +834,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_nested_scope_with_variable_segment() {
|
async fn test_nested_scope_with_variable_segment() {
|
||||||
let mut srv = init_service(App::new().service(web::scope("/app").service(
|
let srv = init_service(App::new().service(web::scope("/app").service(
|
||||||
web::scope("/{project_id}").service(web::resource("/path1").to(
|
web::scope("/{project_id}").service(web::resource("/path1").to(
|
||||||
|r: HttpRequest| {
|
|r: HttpRequest| {
|
||||||
HttpResponse::Created()
|
HttpResponse::Created()
|
||||||
@ -860,7 +859,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_nested2_scope_with_variable_segment() {
|
async fn test_nested2_scope_with_variable_segment() {
|
||||||
let mut srv = init_service(App::new().service(web::scope("/app").service(
|
let srv = init_service(App::new().service(web::scope("/app").service(
|
||||||
web::scope("/{project}").service(web::scope("/{id}").service(
|
web::scope("/{project}").service(web::scope("/{id}").service(
|
||||||
web::resource("/path1").to(|r: HttpRequest| {
|
web::resource("/path1").to(|r: HttpRequest| {
|
||||||
HttpResponse::Created().body(format!(
|
HttpResponse::Created().body(format!(
|
||||||
@ -892,7 +891,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_default_resource() {
|
async fn test_default_resource() {
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new().service(
|
App::new().service(
|
||||||
web::scope("/app")
|
web::scope("/app")
|
||||||
.service(web::resource("/path1").to(HttpResponse::Ok))
|
.service(web::resource("/path1").to(HttpResponse::Ok))
|
||||||
@ -914,7 +913,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_default_resource_propagation() {
|
async fn test_default_resource_propagation() {
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.service(
|
.service(
|
||||||
web::scope("/app1")
|
web::scope("/app1")
|
||||||
@ -942,7 +941,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_middleware() {
|
async fn test_middleware() {
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new().service(
|
App::new().service(
|
||||||
web::scope("app")
|
web::scope("app")
|
||||||
.wrap(
|
.wrap(
|
||||||
@ -959,7 +958,7 @@ mod tests {
|
|||||||
.await;
|
.await;
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/app/test").to_request();
|
let req = TestRequest::with_uri("/app/test").to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
||||||
@ -969,7 +968,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_middleware_fn() {
|
async fn test_middleware_fn() {
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new().service(
|
App::new().service(
|
||||||
web::scope("app")
|
web::scope("app")
|
||||||
.wrap_fn(|req, srv| {
|
.wrap_fn(|req, srv| {
|
||||||
@ -989,7 +988,7 @@ mod tests {
|
|||||||
.await;
|
.await;
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/app/test").to_request();
|
let req = TestRequest::with_uri("/app/test").to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
||||||
@ -999,7 +998,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_override_data() {
|
async fn test_override_data() {
|
||||||
let mut srv = init_service(App::new().data(1usize).service(
|
let srv = init_service(App::new().data(1usize).service(
|
||||||
web::scope("app").data(10usize).route(
|
web::scope("app").data(10usize).route(
|
||||||
"/t",
|
"/t",
|
||||||
web::get().to(|data: web::Data<usize>| {
|
web::get().to(|data: web::Data<usize>| {
|
||||||
@ -1011,13 +1010,13 @@ mod tests {
|
|||||||
.await;
|
.await;
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/app/t").to_request();
|
let req = TestRequest::with_uri("/app/t").to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_override_data_default_service() {
|
async fn test_override_data_default_service() {
|
||||||
let mut srv = init_service(App::new().data(1usize).service(
|
let srv = init_service(App::new().data(1usize).service(
|
||||||
web::scope("app").data(10usize).default_service(web::to(
|
web::scope("app").data(10usize).default_service(web::to(
|
||||||
|data: web::Data<usize>| {
|
|data: web::Data<usize>| {
|
||||||
assert_eq!(**data, 10);
|
assert_eq!(**data, 10);
|
||||||
@ -1028,13 +1027,13 @@ mod tests {
|
|||||||
.await;
|
.await;
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/app/t").to_request();
|
let req = TestRequest::with_uri("/app/t").to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_override_app_data() {
|
async fn test_override_app_data() {
|
||||||
let mut srv = init_service(App::new().app_data(web::Data::new(1usize)).service(
|
let srv = init_service(App::new().app_data(web::Data::new(1usize)).service(
|
||||||
web::scope("app").app_data(web::Data::new(10usize)).route(
|
web::scope("app").app_data(web::Data::new(10usize)).route(
|
||||||
"/t",
|
"/t",
|
||||||
web::get().to(|data: web::Data<usize>| {
|
web::get().to(|data: web::Data<usize>| {
|
||||||
@ -1046,17 +1045,16 @@ mod tests {
|
|||||||
.await;
|
.await;
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/app/t").to_request();
|
let req = TestRequest::with_uri("/app/t").to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_scope_config() {
|
async fn test_scope_config() {
|
||||||
let mut srv =
|
let srv = init_service(App::new().service(web::scope("/app").configure(|s| {
|
||||||
init_service(App::new().service(web::scope("/app").configure(|s| {
|
s.route("/path1", web::get().to(HttpResponse::Ok));
|
||||||
s.route("/path1", web::get().to(HttpResponse::Ok));
|
})))
|
||||||
})))
|
.await;
|
||||||
.await;
|
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/app/path1").to_request();
|
let req = TestRequest::with_uri("/app/path1").to_request();
|
||||||
let resp = srv.call(req).await.unwrap();
|
let resp = srv.call(req).await.unwrap();
|
||||||
@ -1065,13 +1063,12 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_scope_config_2() {
|
async fn test_scope_config_2() {
|
||||||
let mut srv =
|
let srv = init_service(App::new().service(web::scope("/app").configure(|s| {
|
||||||
init_service(App::new().service(web::scope("/app").configure(|s| {
|
s.service(web::scope("/v1").configure(|s| {
|
||||||
s.service(web::scope("/v1").configure(|s| {
|
s.route("/", web::get().to(HttpResponse::Ok));
|
||||||
s.route("/", web::get().to(HttpResponse::Ok));
|
}));
|
||||||
}));
|
})))
|
||||||
})))
|
.await;
|
||||||
.await;
|
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/app/v1/").to_request();
|
let req = TestRequest::with_uri("/app/v1/").to_request();
|
||||||
let resp = srv.call(req).await.unwrap();
|
let resp = srv.call(req).await.unwrap();
|
||||||
@ -1080,24 +1077,20 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_url_for_external() {
|
async fn test_url_for_external() {
|
||||||
let mut srv =
|
let srv = init_service(App::new().service(web::scope("/app").configure(|s| {
|
||||||
init_service(App::new().service(web::scope("/app").configure(|s| {
|
s.service(web::scope("/v1").configure(|s| {
|
||||||
s.service(web::scope("/v1").configure(|s| {
|
s.external_resource("youtube", "https://youtube.com/watch/{video_id}");
|
||||||
s.external_resource(
|
s.route(
|
||||||
"youtube",
|
"/",
|
||||||
"https://youtube.com/watch/{video_id}",
|
web::get().to(|req: HttpRequest| {
|
||||||
);
|
HttpResponse::Ok().body(
|
||||||
s.route(
|
req.url_for("youtube", &["xxxxxx"]).unwrap().to_string(),
|
||||||
"/",
|
)
|
||||||
web::get().to(|req: HttpRequest| {
|
}),
|
||||||
HttpResponse::Ok().body(
|
);
|
||||||
req.url_for("youtube", &["xxxxxx"]).unwrap().to_string(),
|
}));
|
||||||
)
|
})))
|
||||||
}),
|
.await;
|
||||||
);
|
|
||||||
}));
|
|
||||||
})))
|
|
||||||
.await;
|
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/app/v1/").to_request();
|
let req = TestRequest::with_uri("/app/v1/").to_request();
|
||||||
let resp = srv.call(req).await.unwrap();
|
let resp = srv.call(req).await.unwrap();
|
||||||
@ -1108,7 +1101,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_url_for_nested() {
|
async fn test_url_for_nested() {
|
||||||
let mut srv = init_service(App::new().service(web::scope("/a").service(
|
let srv = init_service(App::new().service(web::scope("/a").service(
|
||||||
web::scope("/b").service(web::resource("/c/{stuff}").name("c").route(
|
web::scope("/b").service(web::resource("/c/{stuff}").name("c").route(
|
||||||
web::get().to(|req: HttpRequest| {
|
web::get().to(|req: HttpRequest| {
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
@ -1119,7 +1112,7 @@ mod tests {
|
|||||||
.await;
|
.await;
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/a/b/c/test").to_request();
|
let req = TestRequest::with_uri("/a/b/c/test").to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
let body = read_body(resp).await;
|
let body = read_body(resp).await;
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
@ -630,7 +630,7 @@ where
|
|||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn run(self) -> Server {
|
pub fn run(self) -> Server {
|
||||||
self.builder.start()
|
self.builder.run()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -542,7 +542,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_service() {
|
async fn test_service() {
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new().service(web::service("/test").name("test").finish(
|
App::new().service(web::service("/test").name("test").finish(
|
||||||
|req: ServiceRequest| ok(req.into_response(HttpResponse::Ok().finish())),
|
|req: ServiceRequest| ok(req.into_response(HttpResponse::Ok().finish())),
|
||||||
)),
|
)),
|
||||||
@ -552,7 +552,7 @@ mod tests {
|
|||||||
let resp = srv.call(req).await.unwrap();
|
let resp = srv.call(req).await.unwrap();
|
||||||
assert_eq!(resp.status(), http::StatusCode::OK);
|
assert_eq!(resp.status(), http::StatusCode::OK);
|
||||||
|
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new().service(web::service("/test").guard(guard::Get()).finish(
|
App::new().service(web::service("/test").guard(guard::Get()).finish(
|
||||||
|req: ServiceRequest| ok(req.into_response(HttpResponse::Ok().finish())),
|
|req: ServiceRequest| ok(req.into_response(HttpResponse::Ok().finish())),
|
||||||
)),
|
)),
|
||||||
@ -567,7 +567,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_service_data() {
|
async fn test_service_data() {
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.data(42u32)
|
.data(42u32)
|
||||||
.service(web::service("/test").name("test").finish(
|
.service(web::service("/test").name("test").finish(
|
||||||
|
72
src/test.rs
72
src/test.rs
@ -60,7 +60,7 @@ pub fn default_service(
|
|||||||
///
|
///
|
||||||
/// #[actix_rt::test]
|
/// #[actix_rt::test]
|
||||||
/// async fn test_init_service() {
|
/// async fn test_init_service() {
|
||||||
/// let mut app = test::init_service(
|
/// let app = test::init_service(
|
||||||
/// App::new()
|
/// App::new()
|
||||||
/// .service(web::resource("/test").to(|| async { HttpResponse::Ok() }))
|
/// .service(web::resource("/test").to(|| async { HttpResponse::Ok() }))
|
||||||
/// ).await;
|
/// ).await;
|
||||||
@ -116,7 +116,7 @@ where
|
|||||||
///
|
///
|
||||||
/// #[actix_rt::test]
|
/// #[actix_rt::test]
|
||||||
/// async fn test_response() {
|
/// async fn test_response() {
|
||||||
/// let mut app = test::init_service(
|
/// let app = test::init_service(
|
||||||
/// App::new()
|
/// App::new()
|
||||||
/// .service(web::resource("/test").to(|| async {
|
/// .service(web::resource("/test").to(|| async {
|
||||||
/// HttpResponse::Ok()
|
/// HttpResponse::Ok()
|
||||||
@ -127,11 +127,11 @@ where
|
|||||||
/// let req = test::TestRequest::with_uri("/test").to_request();
|
/// let req = test::TestRequest::with_uri("/test").to_request();
|
||||||
///
|
///
|
||||||
/// // Call application
|
/// // Call application
|
||||||
/// let resp = test::call_service(&mut app, req).await;
|
/// let resp = test::call_service(&app, req).await;
|
||||||
/// assert_eq!(resp.status(), StatusCode::OK);
|
/// assert_eq!(resp.status(), StatusCode::OK);
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub async fn call_service<S, R, B, E>(app: &mut S, req: R) -> S::Response
|
pub async fn call_service<S, R, B, E>(app: &S, req: R) -> S::Response
|
||||||
where
|
where
|
||||||
S: Service<R, Response = ServiceResponse<B>, Error = E>,
|
S: Service<R, Response = ServiceResponse<B>, Error = E>,
|
||||||
E: std::fmt::Debug,
|
E: std::fmt::Debug,
|
||||||
@ -147,7 +147,7 @@ where
|
|||||||
///
|
///
|
||||||
/// #[actix_rt::test]
|
/// #[actix_rt::test]
|
||||||
/// async fn test_index() {
|
/// async fn test_index() {
|
||||||
/// let mut app = test::init_service(
|
/// let app = test::init_service(
|
||||||
/// App::new().service(
|
/// App::new().service(
|
||||||
/// web::resource("/index.html")
|
/// web::resource("/index.html")
|
||||||
/// .route(web::post().to(|| async {
|
/// .route(web::post().to(|| async {
|
||||||
@ -160,11 +160,11 @@ where
|
|||||||
/// .header(header::CONTENT_TYPE, "application/json")
|
/// .header(header::CONTENT_TYPE, "application/json")
|
||||||
/// .to_request();
|
/// .to_request();
|
||||||
///
|
///
|
||||||
/// let result = test::read_response(&mut app, req).await;
|
/// let result = test::read_response(&app, req).await;
|
||||||
/// assert_eq!(result, Bytes::from_static(b"welcome!"));
|
/// assert_eq!(result, Bytes::from_static(b"welcome!"));
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub async fn read_response<S, B>(app: &mut S, req: Request) -> Bytes
|
pub async fn read_response<S, B>(app: &S, req: Request) -> Bytes
|
||||||
where
|
where
|
||||||
S: Service<Request, Response = ServiceResponse<B>, Error = Error>,
|
S: Service<Request, Response = ServiceResponse<B>, Error = Error>,
|
||||||
B: MessageBody + Unpin,
|
B: MessageBody + Unpin,
|
||||||
@ -190,7 +190,7 @@ where
|
|||||||
///
|
///
|
||||||
/// #[actix_rt::test]
|
/// #[actix_rt::test]
|
||||||
/// async fn test_index() {
|
/// async fn test_index() {
|
||||||
/// let mut app = test::init_service(
|
/// let app = test::init_service(
|
||||||
/// App::new().service(
|
/// App::new().service(
|
||||||
/// web::resource("/index.html")
|
/// web::resource("/index.html")
|
||||||
/// .route(web::post().to(|| async {
|
/// .route(web::post().to(|| async {
|
||||||
@ -203,7 +203,7 @@ where
|
|||||||
/// .header(header::CONTENT_TYPE, "application/json")
|
/// .header(header::CONTENT_TYPE, "application/json")
|
||||||
/// .to_request();
|
/// .to_request();
|
||||||
///
|
///
|
||||||
/// let resp = test::call_service(&mut app, req).await;
|
/// let resp = test::call_service(&app, req).await;
|
||||||
/// let result = test::read_body(resp).await;
|
/// let result = test::read_body(resp).await;
|
||||||
/// assert_eq!(result, Bytes::from_static(b"welcome!"));
|
/// assert_eq!(result, Bytes::from_static(b"welcome!"));
|
||||||
/// }
|
/// }
|
||||||
@ -234,7 +234,7 @@ where
|
|||||||
///
|
///
|
||||||
/// #[actix_rt::test]
|
/// #[actix_rt::test]
|
||||||
/// async fn test_post_person() {
|
/// async fn test_post_person() {
|
||||||
/// let mut app = test::init_service(
|
/// let app = test::init_service(
|
||||||
/// App::new().service(
|
/// App::new().service(
|
||||||
/// web::resource("/people")
|
/// web::resource("/people")
|
||||||
/// .route(web::post().to(|person: web::Json<Person>| async {
|
/// .route(web::post().to(|person: web::Json<Person>| async {
|
||||||
@ -294,7 +294,7 @@ where
|
|||||||
///
|
///
|
||||||
/// #[actix_rt::test]
|
/// #[actix_rt::test]
|
||||||
/// async fn test_add_person() {
|
/// async fn test_add_person() {
|
||||||
/// let mut app = test::init_service(
|
/// let app = test::init_service(
|
||||||
/// App::new().service(
|
/// App::new().service(
|
||||||
/// web::resource("/people")
|
/// web::resource("/people")
|
||||||
/// .route(web::post().to(|person: web::Json<Person>| async {
|
/// .route(web::post().to(|person: web::Json<Person>| async {
|
||||||
@ -314,7 +314,7 @@ where
|
|||||||
/// let result: Person = test::read_response_json(&mut app, req).await;
|
/// let result: Person = test::read_response_json(&mut app, req).await;
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub async fn read_response_json<S, B, T>(app: &mut S, req: Request) -> T
|
pub async fn read_response_json<S, B, T>(app: &S, req: Request) -> T
|
||||||
where
|
where
|
||||||
S: Service<Request, Response = ServiceResponse<B>, Error = Error>,
|
S: Service<Request, Response = ServiceResponse<B>, Error = Error>,
|
||||||
B: MessageBody + Unpin,
|
B: MessageBody + Unpin,
|
||||||
@ -569,7 +569,7 @@ impl TestRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Complete request creation, calls service and waits for response future completion.
|
/// Complete request creation, calls service and waits for response future completion.
|
||||||
pub async fn send_request<S, B, E>(self, app: &mut S) -> S::Response
|
pub async fn send_request<S, B, E>(self, app: &S) -> S::Response
|
||||||
where
|
where
|
||||||
S: Service<Request, Response = ServiceResponse<B>, Error = E>,
|
S: Service<Request, Response = ServiceResponse<B>, Error = E>,
|
||||||
E: std::fmt::Debug,
|
E: std::fmt::Debug,
|
||||||
@ -595,7 +595,7 @@ impl TestRequest {
|
|||||||
///
|
///
|
||||||
/// #[actix_rt::test]
|
/// #[actix_rt::test]
|
||||||
/// async fn test_example() {
|
/// async fn test_example() {
|
||||||
/// let mut srv = test::start(
|
/// let srv = test::start(
|
||||||
/// || App::new().service(
|
/// || App::new().service(
|
||||||
/// web::resource("/").to(my_handler))
|
/// web::resource("/").to(my_handler))
|
||||||
/// );
|
/// );
|
||||||
@ -635,7 +635,7 @@ where
|
|||||||
///
|
///
|
||||||
/// #[actix_rt::test]
|
/// #[actix_rt::test]
|
||||||
/// async fn test_example() {
|
/// async fn test_example() {
|
||||||
/// let mut srv = test::start_with(test::config().h1(), ||
|
/// let srv = test::start_with(test::config().h1(), ||
|
||||||
/// App::new().service(web::resource("/").to(my_handler))
|
/// App::new().service(web::resource("/").to(my_handler))
|
||||||
/// );
|
/// );
|
||||||
///
|
///
|
||||||
@ -667,7 +667,7 @@ where
|
|||||||
|
|
||||||
// run server in separate thread
|
// run server in separate thread
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
let sys = System::new("actix-test-server");
|
let sys = System::new();
|
||||||
let tcp = net::TcpListener::bind("127.0.0.1:0").unwrap();
|
let tcp = net::TcpListener::bind("127.0.0.1:0").unwrap();
|
||||||
let local_addr = tcp.local_addr().unwrap();
|
let local_addr = tcp.local_addr().unwrap();
|
||||||
let factory = factory.clone();
|
let factory = factory.clone();
|
||||||
@ -760,7 +760,7 @@ where
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
sys.block_on(async {
|
sys.block_on(async {
|
||||||
let srv = srv.start();
|
let srv = srv.run();
|
||||||
tx.send((System::current(), srv, local_addr)).unwrap();
|
tx.send((System::current(), srv, local_addr)).unwrap();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -1043,7 +1043,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_request_methods() {
|
async fn test_request_methods() {
|
||||||
let mut app = init_service(
|
let app = init_service(
|
||||||
App::new().service(
|
App::new().service(
|
||||||
web::resource("/index.html")
|
web::resource("/index.html")
|
||||||
.route(web::put().to(|| HttpResponse::Ok().body("put!")))
|
.route(web::put().to(|| HttpResponse::Ok().body("put!")))
|
||||||
@ -1058,7 +1058,7 @@ mod tests {
|
|||||||
.insert_header((header::CONTENT_TYPE, "application/json"))
|
.insert_header((header::CONTENT_TYPE, "application/json"))
|
||||||
.to_request();
|
.to_request();
|
||||||
|
|
||||||
let result = read_response(&mut app, put_req).await;
|
let result = read_response(&app, put_req).await;
|
||||||
assert_eq!(result, Bytes::from_static(b"put!"));
|
assert_eq!(result, Bytes::from_static(b"put!"));
|
||||||
|
|
||||||
let patch_req = TestRequest::patch()
|
let patch_req = TestRequest::patch()
|
||||||
@ -1066,17 +1066,17 @@ mod tests {
|
|||||||
.insert_header((header::CONTENT_TYPE, "application/json"))
|
.insert_header((header::CONTENT_TYPE, "application/json"))
|
||||||
.to_request();
|
.to_request();
|
||||||
|
|
||||||
let result = read_response(&mut app, patch_req).await;
|
let result = read_response(&app, patch_req).await;
|
||||||
assert_eq!(result, Bytes::from_static(b"patch!"));
|
assert_eq!(result, Bytes::from_static(b"patch!"));
|
||||||
|
|
||||||
let delete_req = TestRequest::delete().uri("/index.html").to_request();
|
let delete_req = TestRequest::delete().uri("/index.html").to_request();
|
||||||
let result = read_response(&mut app, delete_req).await;
|
let result = read_response(&app, delete_req).await;
|
||||||
assert_eq!(result, Bytes::from_static(b"delete!"));
|
assert_eq!(result, Bytes::from_static(b"delete!"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_response() {
|
async fn test_response() {
|
||||||
let mut app = init_service(
|
let app = init_service(
|
||||||
App::new().service(
|
App::new().service(
|
||||||
web::resource("/index.html")
|
web::resource("/index.html")
|
||||||
.route(web::post().to(|| HttpResponse::Ok().body("welcome!"))),
|
.route(web::post().to(|| HttpResponse::Ok().body("welcome!"))),
|
||||||
@ -1089,13 +1089,13 @@ mod tests {
|
|||||||
.insert_header((header::CONTENT_TYPE, "application/json"))
|
.insert_header((header::CONTENT_TYPE, "application/json"))
|
||||||
.to_request();
|
.to_request();
|
||||||
|
|
||||||
let result = read_response(&mut app, req).await;
|
let result = read_response(&app, req).await;
|
||||||
assert_eq!(result, Bytes::from_static(b"welcome!"));
|
assert_eq!(result, Bytes::from_static(b"welcome!"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_send_request() {
|
async fn test_send_request() {
|
||||||
let mut app = init_service(
|
let app = init_service(
|
||||||
App::new().service(
|
App::new().service(
|
||||||
web::resource("/index.html")
|
web::resource("/index.html")
|
||||||
.route(web::get().to(|| HttpResponse::Ok().body("welcome!"))),
|
.route(web::get().to(|| HttpResponse::Ok().body("welcome!"))),
|
||||||
@ -1105,7 +1105,7 @@ mod tests {
|
|||||||
|
|
||||||
let resp = TestRequest::get()
|
let resp = TestRequest::get()
|
||||||
.uri("/index.html")
|
.uri("/index.html")
|
||||||
.send_request(&mut app)
|
.send_request(&app)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
let result = read_body(resp).await;
|
let result = read_body(resp).await;
|
||||||
@ -1120,7 +1120,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_response_json() {
|
async fn test_response_json() {
|
||||||
let mut app = init_service(App::new().service(web::resource("/people").route(
|
let app = init_service(App::new().service(web::resource("/people").route(
|
||||||
web::post().to(|person: web::Json<Person>| HttpResponse::Ok().json(person)),
|
web::post().to(|person: web::Json<Person>| HttpResponse::Ok().json(person)),
|
||||||
)))
|
)))
|
||||||
.await;
|
.await;
|
||||||
@ -1133,13 +1133,13 @@ mod tests {
|
|||||||
.set_payload(payload)
|
.set_payload(payload)
|
||||||
.to_request();
|
.to_request();
|
||||||
|
|
||||||
let result: Person = read_response_json(&mut app, req).await;
|
let result: Person = read_response_json(&app, req).await;
|
||||||
assert_eq!(&result.id, "12345");
|
assert_eq!(&result.id, "12345");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_body_json() {
|
async fn test_body_json() {
|
||||||
let mut app = init_service(App::new().service(web::resource("/people").route(
|
let app = init_service(App::new().service(web::resource("/people").route(
|
||||||
web::post().to(|person: web::Json<Person>| HttpResponse::Ok().json(person)),
|
web::post().to(|person: web::Json<Person>| HttpResponse::Ok().json(person)),
|
||||||
)))
|
)))
|
||||||
.await;
|
.await;
|
||||||
@ -1150,7 +1150,7 @@ mod tests {
|
|||||||
.uri("/people")
|
.uri("/people")
|
||||||
.insert_header((header::CONTENT_TYPE, "application/json"))
|
.insert_header((header::CONTENT_TYPE, "application/json"))
|
||||||
.set_payload(payload)
|
.set_payload(payload)
|
||||||
.send_request(&mut app)
|
.send_request(&app)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
let result: Person = read_body_json(resp).await;
|
let result: Person = read_body_json(resp).await;
|
||||||
@ -1159,7 +1159,7 @@ mod tests {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_request_response_form() {
|
async fn test_request_response_form() {
|
||||||
let mut app = init_service(App::new().service(web::resource("/people").route(
|
let app = init_service(App::new().service(web::resource("/people").route(
|
||||||
web::post().to(|person: web::Form<Person>| HttpResponse::Ok().json(person)),
|
web::post().to(|person: web::Form<Person>| HttpResponse::Ok().json(person)),
|
||||||
)))
|
)))
|
||||||
.await;
|
.await;
|
||||||
@ -1176,14 +1176,14 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(req.content_type(), "application/x-www-form-urlencoded");
|
assert_eq!(req.content_type(), "application/x-www-form-urlencoded");
|
||||||
|
|
||||||
let result: Person = read_response_json(&mut app, req).await;
|
let result: Person = read_response_json(&app, req).await;
|
||||||
assert_eq!(&result.id, "12345");
|
assert_eq!(&result.id, "12345");
|
||||||
assert_eq!(&result.name, "User name");
|
assert_eq!(&result.name, "User name");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_request_response_json() {
|
async fn test_request_response_json() {
|
||||||
let mut app = init_service(App::new().service(web::resource("/people").route(
|
let app = init_service(App::new().service(web::resource("/people").route(
|
||||||
web::post().to(|person: web::Json<Person>| HttpResponse::Ok().json(person)),
|
web::post().to(|person: web::Json<Person>| HttpResponse::Ok().json(person)),
|
||||||
)))
|
)))
|
||||||
.await;
|
.await;
|
||||||
@ -1200,7 +1200,7 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(req.content_type(), "application/json");
|
assert_eq!(req.content_type(), "application/json");
|
||||||
|
|
||||||
let result: Person = read_response_json(&mut app, req).await;
|
let result: Person = read_response_json(&app, req).await;
|
||||||
assert_eq!(&result.id, "12345");
|
assert_eq!(&result.id, "12345");
|
||||||
assert_eq!(&result.name, "User name");
|
assert_eq!(&result.name, "User name");
|
||||||
}
|
}
|
||||||
@ -1218,7 +1218,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut app = init_service(
|
let app = init_service(
|
||||||
App::new().service(web::resource("/index.html").to(async_with_block)),
|
App::new().service(web::resource("/index.html").to(async_with_block)),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
@ -1235,7 +1235,7 @@ mod tests {
|
|||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut app = init_service(
|
let app = init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.data(10usize)
|
.data(10usize)
|
||||||
.service(web::resource("/index.html").to(handler)),
|
.service(web::resource("/index.html").to(handler)),
|
||||||
@ -1291,7 +1291,7 @@ mod tests {
|
|||||||
.data(addr.clone())
|
.data(addr.clone())
|
||||||
.service(web::resource("/").to(actor_handler));
|
.service(web::resource("/").to(actor_handler));
|
||||||
|
|
||||||
let mut app = init_service(srv).await;
|
let app = init_service(srv).await;
|
||||||
|
|
||||||
let req = TestRequest::post().uri("/").to_request();
|
let req = TestRequest::post().uri("/").to_request();
|
||||||
let res = app.call(req).await.unwrap();
|
let res = app.call(req).await.unwrap();
|
||||||
|
@ -392,7 +392,7 @@ mod tests {
|
|||||||
"payload is probably json string"
|
"payload is probably json string"
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut srv = init_service(
|
let srv = init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.service(
|
.service(
|
||||||
web::resource("/bytes-app-data")
|
web::resource("/bytes-app-data")
|
||||||
@ -422,43 +422,43 @@ mod tests {
|
|||||||
.await;
|
.await;
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/bytes-app-data").to_request();
|
let req = TestRequest::with_uri("/bytes-app-data").to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/bytes-data").to_request();
|
let req = TestRequest::with_uri("/bytes-data").to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/string-app-data").to_request();
|
let req = TestRequest::with_uri("/string-app-data").to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/string-data").to_request();
|
let req = TestRequest::with_uri("/string-data").to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/bytes-app-data")
|
let req = TestRequest::with_uri("/bytes-app-data")
|
||||||
.insert_header(header::ContentType(mime::APPLICATION_JSON))
|
.insert_header(header::ContentType(mime::APPLICATION_JSON))
|
||||||
.to_request();
|
.to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/bytes-data")
|
let req = TestRequest::with_uri("/bytes-data")
|
||||||
.insert_header(header::ContentType(mime::APPLICATION_JSON))
|
.insert_header(header::ContentType(mime::APPLICATION_JSON))
|
||||||
.to_request();
|
.to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/string-app-data")
|
let req = TestRequest::with_uri("/string-app-data")
|
||||||
.insert_header(header::ContentType(mime::APPLICATION_JSON))
|
.insert_header(header::ContentType(mime::APPLICATION_JSON))
|
||||||
.to_request();
|
.to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/string-data")
|
let req = TestRequest::with_uri("/string-data")
|
||||||
.insert_header(header::ContentType(mime::APPLICATION_JSON))
|
.insert_header(header::ContentType(mime::APPLICATION_JSON))
|
||||||
.to_request();
|
.to_request();
|
||||||
let resp = call_service(&mut srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ async fn test_start() {
|
|||||||
let (tx, rx) = mpsc::channel();
|
let (tx, rx) = mpsc::channel();
|
||||||
|
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
let sys = actix_rt::System::new("test");
|
let sys = actix_rt::System::new();
|
||||||
|
|
||||||
sys.block_on(async {
|
sys.block_on(async {
|
||||||
let srv = HttpServer::new(|| {
|
let srv = HttpServer::new(|| {
|
||||||
@ -91,7 +91,7 @@ async fn test_start_ssl() {
|
|||||||
let (tx, rx) = mpsc::channel();
|
let (tx, rx) = mpsc::channel();
|
||||||
|
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
let sys = actix_rt::System::new("test");
|
let sys = actix_rt::System::new();
|
||||||
let builder = ssl_acceptor().unwrap();
|
let builder = ssl_acceptor().unwrap();
|
||||||
|
|
||||||
let srv = HttpServer::new(|| {
|
let srv = HttpServer::new(|| {
|
||||||
|
Loading…
Reference in New Issue
Block a user