1
- use async_std:: io:: prelude:: * ;
2
- use futures:: executor:: block_on;
3
1
use futures:: future:: BoxFuture ;
4
2
use http_service:: Body ;
5
3
use http_service_mock:: make_server;
6
4
use tide:: Middleware ;
7
5
8
- struct TestMiddleware ( & ' static str ) ;
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
+ }
9
13
10
14
impl < State : Send + Sync + ' static > Middleware < State > for TestMiddleware {
11
15
fn handle < ' a > (
@@ -15,7 +19,7 @@ impl<State: Send + Sync + 'static> Middleware<State> for TestMiddleware {
15
19
) -> BoxFuture < ' a , tide:: Response > {
16
20
Box :: pin ( async move {
17
21
let res = next. run ( req) . await ;
18
- res. set_header ( "X-Tide-Test" , self . 0 )
22
+ res. set_header ( self . 0 , self . 1 )
19
23
} )
20
24
}
21
25
}
@@ -28,52 +32,55 @@ async fn echo_path<State>(req: tide::Request<State>) -> String {
28
32
fn route_middleware ( ) {
29
33
let mut app = tide:: new ( ) ;
30
34
let mut foo_route = app. at ( "/foo" ) ;
31
- foo_route. middleware ( TestMiddleware ( "foo" ) ) . get ( echo_path) ;
35
+ foo_route // /foo
36
+ . middleware ( TestMiddleware :: with_header_name ( "X-Foo" , "foo" ) )
37
+ . get ( echo_path) ;
32
38
foo_route
33
- . at ( "/bar" )
34
- . middleware ( TestMiddleware ( "bar" ) )
39
+ . at ( "/bar" ) // nested, /foo/bar
40
+ . middleware ( TestMiddleware :: with_header_name ( "X-Bar" , "bar" ) )
35
41
. get ( echo_path) ;
36
- foo_route. post ( echo_path) . reset_middleware ( ) . put ( echo_path) ;
42
+ foo_route // /foo
43
+ . post ( echo_path)
44
+ . reset_middleware ( )
45
+ . put ( echo_path) ;
37
46
let mut server = make_server ( app. into_http_service ( ) ) . unwrap ( ) ;
38
47
39
- let mut buf = Vec :: new ( ) ;
40
48
let req = http:: Request :: get ( "/foo" ) . body ( Body :: empty ( ) ) . unwrap ( ) ;
41
49
let res = server. simulate ( req) . unwrap ( ) ;
42
- assert_eq ! (
43
- res. headers( ) . get( "X-Tide-Test" ) ,
44
- Some ( & "foo" . parse( ) . unwrap( ) )
45
- ) ;
46
- assert_eq ! ( res. status( ) , 200 ) ;
47
- block_on ( res. into_body ( ) . read_to_end ( & mut buf) ) . unwrap ( ) ;
48
- assert_eq ! ( & * buf, & * b"/foo" ) ;
50
+ assert_eq ! ( res. headers( ) . get( "X-Foo" ) , Some ( & "foo" . parse( ) . unwrap( ) ) ) ;
49
51
50
- buf. clear ( ) ;
51
52
let req = http:: Request :: post ( "/foo" ) . body ( Body :: empty ( ) ) . unwrap ( ) ;
52
53
let res = server. simulate ( req) . unwrap ( ) ;
53
- assert_eq ! (
54
- res. headers( ) . get( "X-Tide-Test" ) ,
55
- Some ( & "foo" . parse( ) . unwrap( ) )
56
- ) ;
57
- assert_eq ! ( res. status( ) , 200 ) ;
58
- block_on ( res. into_body ( ) . read_to_end ( & mut buf) ) . unwrap ( ) ;
59
- assert_eq ! ( & * buf, & * b"/foo" ) ;
54
+ assert_eq ! ( res. headers( ) . get( "X-Foo" ) , Some ( & "foo" . parse( ) . unwrap( ) ) ) ;
60
55
61
- buf. clear ( ) ;
62
56
let req = http:: Request :: put ( "/foo" ) . body ( Body :: empty ( ) ) . unwrap ( ) ;
63
57
let res = server. simulate ( req) . unwrap ( ) ;
64
- assert_eq ! ( res. headers( ) . get( "X-Tide-Test" ) , None ) ;
65
- assert_eq ! ( res. status( ) , 200 ) ;
66
- block_on ( res. into_body ( ) . read_to_end ( & mut buf) ) . unwrap ( ) ;
67
- assert_eq ! ( & * buf, & * b"/foo" ) ;
58
+ assert_eq ! ( res. headers( ) . get( "X-Foo" ) , None ) ;
68
59
69
- buf. clear ( ) ;
70
60
let req = http:: Request :: get ( "/foo/bar" ) . body ( Body :: empty ( ) ) . unwrap ( ) ;
71
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 subroute_not_nested ( ) {
68
+ let mut app = tide:: new ( ) ;
69
+ app. at ( "/parent" ) // /parent
70
+ . middleware ( TestMiddleware :: with_header_name ( "X-Parent" , "Parent" ) )
71
+ . get ( echo_path) ;
72
+ app. at ( "/parent/child" ) // /parent/child, not nested
73
+ . middleware ( TestMiddleware :: with_header_name ( "X-Child" , "child" ) )
74
+ . get ( echo_path) ;
75
+ let mut server = make_server ( app. into_http_service ( ) ) . unwrap ( ) ;
76
+
77
+ let req = http:: Request :: get ( "/parent/child" )
78
+ . body ( Body :: empty ( ) )
79
+ . unwrap ( ) ;
80
+ let res = server. simulate ( req) . unwrap ( ) ;
81
+ assert_eq ! ( res. headers( ) . get( "X-Parent" ) , None ) ;
72
82
assert_eq ! (
73
- res. headers( ) . get( "X-Tide-Test " ) ,
74
- Some ( & "bar " . parse( ) . unwrap( ) )
83
+ res. headers( ) . get( "X-Child " ) ,
84
+ Some ( & "child " . parse( ) . unwrap( ) )
75
85
) ;
76
- assert_eq ! ( res. status( ) , 200 ) ;
77
- block_on ( res. into_body ( ) . read_to_end ( & mut buf) ) . unwrap ( ) ;
78
- assert_eq ! ( & * buf, & * b"/foo/bar" ) ;
79
86
}
0 commit comments