Skip to content

Commit e4f2f2d

Browse files
committed
Add more tests
1 parent a4ee7fe commit e4f2f2d

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

tests/route_middleware.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,70 @@ fn route_middleware() {
6363
assert_eq!(res.headers().get("X-Bar"), Some(&"bar".parse().unwrap()));
6464
}
6565

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+
66130
#[test]
67131
fn subroute_not_nested() {
68132
let mut app = tide::new();

0 commit comments

Comments
 (0)