|
| 1 | +use futures::future::BoxFuture; |
| 2 | +use http_service::Body; |
| 3 | +use http_service_mock::make_server; |
| 4 | +use tide::Middleware; |
| 5 | + |
| 6 | +struct TestMiddleware(&'static str, &'static str); |
| 7 | + |
| 8 | +impl TestMiddleware { |
| 9 | + fn with_header_name(name: &'static str, value: &'static str) -> Self { |
| 10 | + Self(name, value) |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +impl<State: Send + Sync + 'static> Middleware<State> for TestMiddleware { |
| 15 | + fn handle<'a>( |
| 16 | + &'a self, |
| 17 | + req: tide::Request<State>, |
| 18 | + next: tide::Next<'a, State>, |
| 19 | + ) -> BoxFuture<'a, tide::Response> { |
| 20 | + Box::pin(async move { |
| 21 | + let res = next.run(req).await; |
| 22 | + res.set_header(self.0, self.1) |
| 23 | + }) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +async fn echo_path<State>(req: tide::Request<State>) -> String { |
| 28 | + req.uri().path().to_string() |
| 29 | +} |
| 30 | + |
| 31 | +#[test] |
| 32 | +fn route_middleware() { |
| 33 | + let mut app = tide::new(); |
| 34 | + let mut foo_route = app.at("/foo"); |
| 35 | + foo_route // /foo |
| 36 | + .middleware(TestMiddleware::with_header_name("X-Foo", "foo")) |
| 37 | + .get(echo_path); |
| 38 | + foo_route |
| 39 | + .at("/bar") // nested, /foo/bar |
| 40 | + .middleware(TestMiddleware::with_header_name("X-Bar", "bar")) |
| 41 | + .get(echo_path); |
| 42 | + foo_route // /foo |
| 43 | + .post(echo_path) |
| 44 | + .reset_middleware() |
| 45 | + .put(echo_path); |
| 46 | + let mut server = make_server(app.into_http_service()).unwrap(); |
| 47 | + |
| 48 | + let req = http::Request::get("/foo").body(Body::empty()).unwrap(); |
| 49 | + let res = server.simulate(req).unwrap(); |
| 50 | + assert_eq!(res.headers().get("X-Foo"), Some(&"foo".parse().unwrap())); |
| 51 | + |
| 52 | + let req = http::Request::post("/foo").body(Body::empty()).unwrap(); |
| 53 | + let res = server.simulate(req).unwrap(); |
| 54 | + assert_eq!(res.headers().get("X-Foo"), Some(&"foo".parse().unwrap())); |
| 55 | + |
| 56 | + let req = http::Request::put("/foo").body(Body::empty()).unwrap(); |
| 57 | + let res = server.simulate(req).unwrap(); |
| 58 | + assert_eq!(res.headers().get("X-Foo"), None); |
| 59 | + |
| 60 | + let req = http::Request::get("/foo/bar").body(Body::empty()).unwrap(); |
| 61 | + let res = server.simulate(req).unwrap(); |
| 62 | + assert_eq!(res.headers().get("X-Foo"), Some(&"foo".parse().unwrap())); |
| 63 | + assert_eq!(res.headers().get("X-Bar"), Some(&"bar".parse().unwrap())); |
| 64 | +} |
| 65 | + |
| 66 | +#[test] |
| 67 | +fn app_and_route_middleware() { |
| 68 | + let mut app = tide::new(); |
| 69 | + app.middleware(TestMiddleware::with_header_name("X-Root", "root")); |
| 70 | + app.at("/foo") |
| 71 | + .middleware(TestMiddleware::with_header_name("X-Foo", "foo")) |
| 72 | + .get(echo_path); |
| 73 | + app.at("/bar") |
| 74 | + .middleware(TestMiddleware::with_header_name("X-Bar", "bar")) |
| 75 | + .get(echo_path); |
| 76 | + let mut server = make_server(app.into_http_service()).unwrap(); |
| 77 | + |
| 78 | + let req = http::Request::get("/foo").body(Body::empty()).unwrap(); |
| 79 | + let res = server.simulate(req).unwrap(); |
| 80 | + assert_eq!(res.headers().get("X-Root"), Some(&"root".parse().unwrap())); |
| 81 | + assert_eq!(res.headers().get("X-Foo"), Some(&"foo".parse().unwrap())); |
| 82 | + assert_eq!(res.headers().get("X-Bar"), None); |
| 83 | + |
| 84 | + let req = http::Request::get("/bar").body(Body::empty()).unwrap(); |
| 85 | + let res = server.simulate(req).unwrap(); |
| 86 | + assert_eq!(res.headers().get("X-Root"), Some(&"root".parse().unwrap())); |
| 87 | + assert_eq!(res.headers().get("X-Foo"), None); |
| 88 | + assert_eq!(res.headers().get("X-Bar"), Some(&"bar".parse().unwrap())); |
| 89 | +} |
| 90 | + |
| 91 | +#[test] |
| 92 | +fn nested_app_with_route_middleware() { |
| 93 | + let mut inner = tide::new(); |
| 94 | + inner.middleware(TestMiddleware::with_header_name("X-Inner", "inner")); |
| 95 | + inner |
| 96 | + .at("/baz") |
| 97 | + .middleware(TestMiddleware::with_header_name("X-Baz", "baz")) |
| 98 | + .get(echo_path); |
| 99 | + |
| 100 | + let mut app = tide::new(); |
| 101 | + app.middleware(TestMiddleware::with_header_name("X-Root", "root")); |
| 102 | + app.at("/foo") |
| 103 | + .middleware(TestMiddleware::with_header_name("X-Foo", "foo")) |
| 104 | + .get(echo_path); |
| 105 | + app.at("/bar") |
| 106 | + .middleware(TestMiddleware::with_header_name("X-Bar", "bar")) |
| 107 | + .nest(inner); |
| 108 | + let mut server = make_server(app.into_http_service()).unwrap(); |
| 109 | + |
| 110 | + let req = http::Request::get("/foo").body(Body::empty()).unwrap(); |
| 111 | + let res = server.simulate(req).unwrap(); |
| 112 | + assert_eq!(res.headers().get("X-Root"), Some(&"root".parse().unwrap())); |
| 113 | + assert_eq!(res.headers().get("X-Inner"), None); |
| 114 | + assert_eq!(res.headers().get("X-Foo"), Some(&"foo".parse().unwrap())); |
| 115 | + assert_eq!(res.headers().get("X-Bar"), None); |
| 116 | + assert_eq!(res.headers().get("X-Baz"), None); |
| 117 | + |
| 118 | + let req = http::Request::get("/bar/baz").body(Body::empty()).unwrap(); |
| 119 | + let res = server.simulate(req).unwrap(); |
| 120 | + assert_eq!(res.headers().get("X-Root"), Some(&"root".parse().unwrap())); |
| 121 | + assert_eq!( |
| 122 | + res.headers().get("X-Inner"), |
| 123 | + Some(&"inner".parse().unwrap()) |
| 124 | + ); |
| 125 | + assert_eq!(res.headers().get("X-Foo"), None); |
| 126 | + assert_eq!(res.headers().get("X-Bar"), Some(&"bar".parse().unwrap())); |
| 127 | + assert_eq!(res.headers().get("X-Baz"), Some(&"baz".parse().unwrap())); |
| 128 | +} |
| 129 | + |
| 130 | +#[test] |
| 131 | +fn subroute_not_nested() { |
| 132 | + let mut app = tide::new(); |
| 133 | + app.at("/parent") // /parent |
| 134 | + .middleware(TestMiddleware::with_header_name("X-Parent", "Parent")) |
| 135 | + .get(echo_path); |
| 136 | + app.at("/parent/child") // /parent/child, not nested |
| 137 | + .middleware(TestMiddleware::with_header_name("X-Child", "child")) |
| 138 | + .get(echo_path); |
| 139 | + let mut server = make_server(app.into_http_service()).unwrap(); |
| 140 | + |
| 141 | + let req = http::Request::get("/parent/child") |
| 142 | + .body(Body::empty()) |
| 143 | + .unwrap(); |
| 144 | + let res = server.simulate(req).unwrap(); |
| 145 | + assert_eq!(res.headers().get("X-Parent"), None); |
| 146 | + assert_eq!( |
| 147 | + res.headers().get("X-Child"), |
| 148 | + Some(&"child".parse().unwrap()) |
| 149 | + ); |
| 150 | +} |
0 commit comments