diff --git a/axum/src/extract/connect_info.rs b/axum/src/extract/connect_info.rs index 14cae7b4..9a37513d 100644 --- a/axum/src/extract/connect_info.rs +++ b/axum/src/extract/connect_info.rs @@ -159,7 +159,7 @@ mod tests { let (tx, rx) = tokio::sync::oneshot::channel(); tokio::spawn(async move { - let app = Router::new().route("/", get(handler)).state(()); + let app = Router::without_state().route("/", get(handler)); let server = Server::from_tcp(listener) .unwrap() .serve(app.into_make_service_with_connect_info::()); @@ -199,7 +199,7 @@ mod tests { let (tx, rx) = tokio::sync::oneshot::channel(); tokio::spawn(async move { - let app = Router::new().route("/", get(handler)).state(()); + let app = Router::without_state().route("/", get(handler)); let server = Server::from_tcp(listener) .unwrap() .serve(app.into_make_service_with_connect_info::()); diff --git a/axum/src/extract/content_length_limit.rs b/axum/src/extract/content_length_limit.rs index 29fbd46c..fd9fb1b2 100644 --- a/axum/src/extract/content_length_limit.rs +++ b/axum/src/extract/content_length_limit.rs @@ -116,12 +116,10 @@ mod tests { const LIMIT: u64 = 8; - let app = Router::new() - .route( - "/", - post(|_body: ContentLengthLimit| async {}), - ) - .state(()); + let app = Router::without_state().route( + "/", + post(|_body: ContentLengthLimit| async {}), + ); let client = TestClient::new(app); let res = client @@ -157,9 +155,8 @@ mod tests { #[tokio::test] async fn get_request_without_content_length_is_accepted() { - let app = Router::new() - .route("/", get(|_body: ContentLengthLimit| async {})) - .state(()); + let app = Router::without_state() + .route("/", get(|_body: ContentLengthLimit| async {})); let client = TestClient::new(app); @@ -169,9 +166,8 @@ mod tests { #[tokio::test] async fn get_request_with_content_length_is_rejected() { - let app = Router::new() - .route("/", get(|_body: ContentLengthLimit| async {})) - .state(()); + let app = Router::without_state() + .route("/", get(|_body: ContentLengthLimit| async {})); let client = TestClient::new(app); @@ -186,9 +182,8 @@ mod tests { #[tokio::test] async fn get_request_with_chunked_encoding_is_rejected() { - let app = Router::new() - .route("/", get(|_body: ContentLengthLimit| async {})) - .state(()); + let app = Router::without_state() + .route("/", get(|_body: ContentLengthLimit| async {})); let client = TestClient::new(app); diff --git a/axum/src/extract/host.rs b/axum/src/extract/host.rs index d7e49a5a..8336755f 100644 --- a/axum/src/extract/host.rs +++ b/axum/src/extract/host.rs @@ -85,7 +85,7 @@ mod tests { host } - TestClient::new(Router::new().route("/", get(host_as_body)).state(())) + TestClient::new(Router::without_state().route("/", get(host_as_body))) } #[tokio::test] diff --git a/axum/src/extract/matched_path.rs b/axum/src/extract/matched_path.rs index f8e67525..e7b5cd25 100644 --- a/axum/src/extract/matched_path.rs +++ b/axum/src/extract/matched_path.rs @@ -140,7 +140,7 @@ mod tests { ) } - let app = Router::new() + let app = Router::without_state() .route( "/:key", get(|path: MatchedPath| async move { path.as_str().to_owned() }), @@ -151,8 +151,7 @@ mod tests { Router::new().route("/assets/*path", get(handler)), ) .nest_service("/foo", handler.into_service(())) - .layer(tower::layer::layer_fn(SetMatchedPathExtension)) - .state(()); + .layer(tower::layer::layer_fn(SetMatchedPathExtension)); let client = TestClient::new(app); @@ -183,17 +182,13 @@ mod tests { #[tokio::test] async fn nested_opaque_routers_append_to_matched_path() { - let app = Router::new() - .nest_service( - "/:a", - Router::new() - .route( - "/:b", - get(|path: MatchedPath| async move { path.as_str().to_owned() }), - ) - .state(()), - ) - .state(()); + let app = Router::without_state().nest_service( + "/:a", + Router::without_state().route( + "/:b", + get(|path: MatchedPath| async move { path.as_str().to_owned() }), + ), + ); let client = TestClient::new(app); diff --git a/axum/src/extract/mod.rs b/axum/src/extract/mod.rs index dcc9e70d..6b72942f 100644 --- a/axum/src/extract/mod.rs +++ b/axum/src/extract/mod.rs @@ -105,9 +105,7 @@ mod tests { #[tokio::test] async fn consume_body() { - let app = Router::new() - .route("/", get(|body: String| async { body })) - .state(()); + let app = Router::without_state().route("/", get(|body: String| async { body })); let client = TestClient::new(app); let res = client.get("/").body("foo").send().await; diff --git a/axum/src/extract/multipart.rs b/axum/src/extract/multipart.rs index 168f3ef2..d5ca9601 100644 --- a/axum/src/extract/multipart.rs +++ b/axum/src/extract/multipart.rs @@ -258,7 +258,7 @@ mod tests { assert!(multipart.next_field().await.unwrap().is_none()); } - let app = Router::new().route("/", post(handle)).state(()); + let app = Router::without_state().route("/", post(handle)); let client = TestClient::new(app); diff --git a/axum/src/extract/path/mod.rs b/axum/src/extract/path/mod.rs index 590f80ee..5bda833e 100644 --- a/axum/src/extract/path/mod.rs +++ b/axum/src/extract/path/mod.rs @@ -419,17 +419,15 @@ mod tests { #[tokio::test] async fn extracting_url_params() { - let app = Router::new() - .route( - "/users/:id", - get(|Path(id): Path| async move { - assert_eq!(id, 42); - }) - .post(|Path(params_map): Path>| async move { - assert_eq!(params_map.get("id").unwrap(), &1337); - }), - ) - .state(()); + let app = Router::without_state().route( + "/users/:id", + get(|Path(id): Path| async move { + assert_eq!(id, 42); + }) + .post(|Path(params_map): Path>| async move { + assert_eq!(params_map.get("id").unwrap(), &1337); + }), + ); let client = TestClient::new(app); @@ -442,9 +440,8 @@ mod tests { #[tokio::test] async fn extracting_url_params_multiple_times() { - let app = Router::new() - .route("/users/:id", get(|_: Path, _: Path| async {})) - .state(()); + let app = Router::without_state() + .route("/users/:id", get(|_: Path, _: Path| async {})); let client = TestClient::new(app); @@ -454,12 +451,10 @@ mod tests { #[tokio::test] async fn percent_decoding() { - let app = Router::new() - .route( - "/:key", - get(|Path(param): Path| async move { param }), - ) - .state(()); + let app = Router::without_state().route( + "/:key", + get(|Path(param): Path| async move { param }), + ); let client = TestClient::new(app); @@ -470,7 +465,7 @@ mod tests { #[tokio::test] async fn supports_128_bit_numbers() { - let app = Router::new() + let app = Router::without_state() .route( "/i/:key", get(|Path(param): Path| async move { param.to_string() }), @@ -478,8 +473,7 @@ mod tests { .route( "/u/:key", get(|Path(param): Path| async move { param.to_string() }), - ) - .state(()); + ); let client = TestClient::new(app); @@ -492,7 +486,7 @@ mod tests { #[tokio::test] async fn wildcard() { - let app = Router::new() + let app = Router::without_state() .route( "/foo/*rest", get(|Path(param): Path| async move { param }), @@ -502,8 +496,7 @@ mod tests { get(|Path(params): Path>| async move { params.get("rest").unwrap().clone() }), - ) - .state(()); + ); let client = TestClient::new(app); @@ -516,7 +509,7 @@ mod tests { #[tokio::test] async fn captures_dont_match_empty_segments() { - let app = Router::new().route("/:key", get(|| async {})).state(()); + let app = Router::without_state().route("/:key", get(|| async {})); let client = TestClient::new(app); @@ -529,9 +522,8 @@ mod tests { #[tokio::test] async fn when_extensions_are_missing() { - let app = Router::new() - .route("/:key", get(|_: Request, _: Path| async {})) - .state(()); + let app = Router::without_state() + .route("/:key", get(|_: Request, _: Path| async {})); let client = TestClient::new(app); @@ -556,9 +548,8 @@ mod tests { } } - let app = Router::new() - .route("/:key", get(|param: Path| async move { param.0 .0 })) - .state(()); + let app = Router::without_state() + .route("/:key", get(|param: Path| async move { param.0 .0 })); let client = TestClient::new(app); @@ -572,9 +563,8 @@ mod tests { #[tokio::test] async fn two_path_extractors() { - let app = Router::new() - .route("/:a/:b", get(|_: Path, _: Path| async {})) - .state(()); + let app = Router::without_state() + .route("/:a/:b", get(|_: Path, _: Path| async {})); let client = TestClient::new(app); @@ -589,20 +579,18 @@ mod tests { #[tokio::test] async fn deserialize_into_vec_of_tuples() { - let app = Router::new() - .route( - "/:a/:b", - get(|Path(params): Path>| async move { - assert_eq!( - params, - vec![ - ("a".to_owned(), "foo".to_owned()), - ("b".to_owned(), "bar".to_owned()) - ] - ); - }), - ) - .state(()); + let app = Router::without_state().route( + "/:a/:b", + get(|Path(params): Path>| async move { + assert_eq!( + params, + vec![ + ("a".to_owned(), "foo".to_owned()), + ("b".to_owned(), "bar".to_owned()) + ] + ); + }), + ); let client = TestClient::new(app); diff --git a/axum/src/extract/request_parts.rs b/axum/src/extract/request_parts.rs index 81976783..52fca38a 100644 --- a/axum/src/extract/request_parts.rs +++ b/axum/src/extract/request_parts.rs @@ -226,7 +226,7 @@ mod tests { async fn multiple_request_extractors() { async fn handler(_: Request, _: Request) {} - let app = Router::new().route("/", post(handler)).state(()); + let app = Router::without_state().route("/", post(handler)); let client = TestClient::new(app); @@ -252,10 +252,9 @@ mod tests { } let client = TestClient::new( - Router::new() + Router::without_state() .route("/", get(handler)) - .layer(Extension(Ext)) - .state(()), + .layer(Extension(Ext)), ); let res = client.get("/").header("x-foo", "123").send().await; @@ -271,7 +270,7 @@ mod tests { assert_eq!(body, "foo"); } - let client = TestClient::new(Router::new().route("/", get(handler)).state(())); + let client = TestClient::new(Router::without_state().route("/", get(handler))); let res = client.get("/").body("foo").send().await; assert_eq!(res.status(), StatusCode::OK); diff --git a/axum/src/json.rs b/axum/src/json.rs index 6fd2fd5a..8fdaab03 100644 --- a/axum/src/json.rs +++ b/axum/src/json.rs @@ -222,9 +222,8 @@ mod tests { foo: String, } - let app = Router::new() - .route("/", post(|input: Json| async { input.0.foo })) - .state(()); + let app = + Router::without_state().route("/", post(|input: Json| async { input.0.foo })); let client = TestClient::new(app); let res = client.post("/").json(&json!({ "foo": "bar" })).send().await; @@ -240,9 +239,8 @@ mod tests { foo: String, } - let app = Router::new() - .route("/", post(|input: Json| async { input.0.foo })) - .state(()); + let app = + Router::without_state().route("/", post(|input: Json| async { input.0.foo })); let client = TestClient::new(app); let res = client.post("/").body(r#"{ "foo": "bar" }"#).send().await; @@ -258,9 +256,7 @@ mod tests { async fn valid_json_content_type(content_type: &str) -> bool { println!("testing {:?}", content_type); - let app = Router::new() - .route("/", post(|Json(_): Json| async {})) - .state(()); + let app = Router::without_state().route("/", post(|Json(_): Json| async {})); let res = TestClient::new(app) .post("/") @@ -281,9 +277,7 @@ mod tests { #[tokio::test] async fn invalid_json_syntax() { - let app = Router::new() - .route("/", post(|_: Json| async {})) - .state(()); + let app = Router::without_state().route("/", post(|_: Json| async {})); let client = TestClient::new(app); let res = client diff --git a/axum/src/routing/mod.rs b/axum/src/routing/mod.rs index 8bb15948..db378aa6 100644 --- a/axum/src/routing/mod.rs +++ b/axum/src/routing/mod.rs @@ -141,8 +141,7 @@ where } } - /// TODO(david): docs - pub fn state(self, state: S) -> Router + fn state(self, state: S) -> Router where S: Clone, { @@ -151,8 +150,10 @@ where .into_iter() .map(|(id, endpoint)| { let endpoint = match endpoint { - Endpoint::MethodRouter(router) => { - Endpoint::MethodRouter(router.state(state.clone())) + Endpoint::MethodRouter(method_router) => { + // the state will be provided later in `::call`, so its safe to + // ignore that it hasn't been provided yet + Endpoint::MethodRouter(method_router.change_state_marker()) } Endpoint::Route(route) => Endpoint::Route(route), }; diff --git a/axum/src/typed_header.rs b/axum/src/typed_header.rs index de61056a..25708cd5 100644 --- a/axum/src/typed_header.rs +++ b/axum/src/typed_header.rs @@ -178,7 +178,7 @@ mod tests { user_agent.to_string() } - let app = Router::new().route("/", get(handle)).state(()); + let app = Router::without_state().route("/", get(handle)); let client = TestClient::new(app);