@@ -4,7 +4,7 @@ use std::fmt;
4
4
use std:: sync:: Arc ;
5
5
6
6
use iron:: { Request , Response , Handler , IronResult , IronError } ;
7
- use iron:: { status , method, headers} ;
7
+ use iron:: { StatusCode , method, Method , headers} ;
8
8
use iron:: typemap:: Key ;
9
9
use iron:: modifiers:: Redirect ;
10
10
@@ -94,37 +94,37 @@ impl Router {
94
94
95
95
/// Like route, but specialized to the `Get` method.
96
96
pub fn get < S : AsRef < str > , H : Handler , I : AsRef < str > > ( & mut self , glob : S , handler : H , route_id : I ) -> & mut Router {
97
- self . route ( method :: Get , glob, handler, route_id)
97
+ self . route ( Method :: GET , glob, handler, route_id)
98
98
}
99
99
100
100
/// Like route, but specialized to the `Post` method.
101
101
pub fn post < S : AsRef < str > , H : Handler , I : AsRef < str > > ( & mut self , glob : S , handler : H , route_id : I ) -> & mut Router {
102
- self . route ( method :: Post , glob, handler, route_id)
102
+ self . route ( Method :: POST , glob, handler, route_id)
103
103
}
104
104
105
105
/// Like route, but specialized to the `Put` method.
106
106
pub fn put < S : AsRef < str > , H : Handler , I : AsRef < str > > ( & mut self , glob : S , handler : H , route_id : I ) -> & mut Router {
107
- self . route ( method :: Put , glob, handler, route_id)
107
+ self . route ( Method :: PUT , glob, handler, route_id)
108
108
}
109
109
110
110
/// Like route, but specialized to the `Delete` method.
111
111
pub fn delete < S : AsRef < str > , H : Handler , I : AsRef < str > > ( & mut self , glob : S , handler : H , route_id : I ) -> & mut Router {
112
- self . route ( method :: Delete , glob, handler, route_id)
112
+ self . route ( Method :: DELETE , glob, handler, route_id)
113
113
}
114
114
115
115
/// Like route, but specialized to the `Head` method.
116
116
pub fn head < S : AsRef < str > , H : Handler , I : AsRef < str > > ( & mut self , glob : S , handler : H , route_id : I ) -> & mut Router {
117
- self . route ( method :: Head , glob, handler, route_id)
117
+ self . route ( Method :: HEAD , glob, handler, route_id)
118
118
}
119
119
120
120
/// Like route, but specialized to the `Patch` method.
121
121
pub fn patch < S : AsRef < str > , H : Handler , I : AsRef < str > > ( & mut self , glob : S , handler : H , route_id : I ) -> & mut Router {
122
- self . route ( method :: Patch , glob, handler, route_id)
122
+ self . route ( Method :: PATCH , glob, handler, route_id)
123
123
}
124
124
125
125
/// Like route, but specialized to the `Options` method.
126
126
pub fn options < S : AsRef < str > , H : Handler , I : AsRef < str > > ( & mut self , glob : S , handler : H , route_id : I ) -> & mut Router {
127
- self . route ( method :: Options , glob, handler, route_id)
127
+ self . route ( Method :: OPTIONS , glob, handler, route_id)
128
128
}
129
129
130
130
/// Route will match any method, including gibberish.
@@ -143,8 +143,8 @@ impl Router {
143
143
144
144
fn handle_options ( & self , path : & str ) -> Response {
145
145
static METHODS : & ' static [ method:: Method ] =
146
- & [ method :: Get , method :: Post , method :: Put ,
147
- method :: Delete , method :: Head , method :: Patch ] ;
146
+ & [ Method :: GET , Method :: POST , Method :: PUT ,
147
+ Method :: DELETE , Method :: HEAD , Method :: PATCH ] ;
148
148
149
149
// Get all the available methods and return them.
150
150
let mut options = vec ! [ ] ;
@@ -157,12 +157,14 @@ impl Router {
157
157
} ) ;
158
158
}
159
159
// If GET is there, HEAD is also there.
160
- if options. contains ( & method :: Get ) && !options. contains ( & method :: Head ) {
161
- options. push ( method :: Head ) ;
160
+ if options. contains ( & Method :: GET ) && !options. contains ( & Method :: HEAD ) {
161
+ options. push ( Method :: HEAD ) ;
162
162
}
163
163
164
- let mut res = Response :: with ( status:: Ok ) ;
165
- res. headers . set ( headers:: Allow ( options) ) ;
164
+ let mut res = Response :: with ( StatusCode :: OK ) ;
165
+ for option in options {
166
+ res. headers . append ( headers:: ALLOW , option. as_str ( ) . parse ( ) . unwrap ( ) ) ;
167
+ }
166
168
res
167
169
}
168
170
@@ -188,7 +190,7 @@ impl Router {
188
190
189
191
self . recognize ( & req. method , & path) . and (
190
192
Some ( IronError :: new ( TrailingSlash ,
191
- ( status :: MovedPermanently , Redirect ( url) ) ) )
193
+ ( StatusCode :: MOVED_PERMANENTLY , Redirect ( url) ) ) )
192
194
)
193
195
}
194
196
@@ -211,15 +213,15 @@ impl Handler for Router {
211
213
212
214
self . handle_method ( req, & path) . unwrap_or_else ( ||
213
215
match req. method {
214
- method :: Options => Ok ( self . handle_options ( & path) ) ,
216
+ Method :: OPTIONS => Ok ( self . handle_options ( & path) ) ,
215
217
// For HEAD, fall back to GET. Hyper ensures no response body is written.
216
- method :: Head => {
217
- req. method = method :: Get ;
218
+ Method :: HEAD => {
219
+ req. method = Method :: GET ;
218
220
self . handle_method ( req, & path) . unwrap_or (
219
- Err ( IronError :: new ( NoRoute , status :: NotFound ) )
221
+ Err ( IronError :: new ( NoRoute , StatusCode :: NOT_FOUND ) )
220
222
)
221
223
}
222
- _ => Err ( IronError :: new ( NoRoute , status :: NotFound ) )
224
+ _ => Err ( IronError :: new ( NoRoute , StatusCode :: NOT_FOUND ) )
223
225
}
224
226
)
225
227
}
@@ -258,96 +260,96 @@ impl Error for TrailingSlash {
258
260
#[ cfg( test) ]
259
261
mod test {
260
262
use super :: Router ;
261
- use iron:: { headers, method, status , Request , Response } ;
263
+ use iron:: { headers, method, Method , StatusCode , Request , Response } ;
262
264
263
265
#[ test]
264
266
fn test_handle_options_post ( ) {
265
267
let mut router = Router :: new ( ) ;
266
268
router. post ( "/" , |_: & mut Request | {
267
- Ok ( Response :: with ( ( status :: Ok , "" ) ) )
269
+ Ok ( Response :: with ( ( StatusCode :: OK , "" ) ) )
268
270
} , "" ) ;
269
271
let resp = router. handle_options ( "/" ) ;
270
- let headers = resp. headers . get :: < headers:: Allow > ( ) . unwrap ( ) ;
271
- let expected = headers :: Allow ( vec ! [ method :: Method :: Post ] ) ;
272
- assert_eq ! ( & expected, headers) ;
272
+ let headers : Vec < method :: Method > = resp. headers . get_all ( headers:: ALLOW ) . into_iter ( ) . map ( |s| s . to_str ( ) . unwrap ( ) . parse ( ) . unwrap ( ) ) . collect ( ) ;
273
+ let expected = vec ! [ Method :: POST ] ;
274
+ assert_eq ! ( expected, headers) ;
273
275
}
274
276
275
277
#[ test]
276
278
fn test_handle_options_get_head ( ) {
277
279
let mut router = Router :: new ( ) ;
278
280
router. get ( "/" , |_: & mut Request | {
279
- Ok ( Response :: with ( ( status :: Ok , "" ) ) )
281
+ Ok ( Response :: with ( ( StatusCode :: OK , "" ) ) )
280
282
} , "" ) ;
281
283
let resp = router. handle_options ( "/" ) ;
282
- let headers = resp. headers . get :: < headers:: Allow > ( ) . unwrap ( ) ;
283
- let expected = headers :: Allow ( vec ! [ method:: Method :: Get , method:: Method :: Head ] ) ;
284
- assert_eq ! ( & expected, headers) ;
284
+ let headers : Vec < method :: Method > = resp. headers . get_all ( headers:: ALLOW ) . into_iter ( ) . map ( |s| s . to_str ( ) . unwrap ( ) . parse ( ) . unwrap ( ) ) . collect ( ) ;
285
+ let expected = vec ! [ method:: Method :: GET , method:: Method :: HEAD ] ;
286
+ assert_eq ! ( expected, headers) ;
285
287
}
286
288
287
289
#[ test]
288
290
fn test_handle_any_ok ( ) {
289
291
let mut router = Router :: new ( ) ;
290
292
router. post ( "/post" , |_: & mut Request | {
291
- Ok ( Response :: with ( ( status :: Ok , "" ) ) )
293
+ Ok ( Response :: with ( ( StatusCode :: OK , "" ) ) )
292
294
} , "" ) ;
293
295
router. any ( "/post" , |_: & mut Request | {
294
- Ok ( Response :: with ( ( status :: Ok , "" ) ) )
296
+ Ok ( Response :: with ( ( StatusCode :: OK , "" ) ) )
295
297
} , "" ) ;
296
298
router. put ( "/post" , |_: & mut Request | {
297
- Ok ( Response :: with ( ( status :: Ok , "" ) ) )
299
+ Ok ( Response :: with ( ( StatusCode :: OK , "" ) ) )
298
300
} , "" ) ;
299
301
router. any ( "/get" , |_: & mut Request | {
300
- Ok ( Response :: with ( ( status :: Ok , "" ) ) )
302
+ Ok ( Response :: with ( ( StatusCode :: OK , "" ) ) )
301
303
} , "any" ) ;
302
304
303
- assert ! ( router. recognize( & method :: Get , "/post" ) . is_some( ) ) ;
304
- assert ! ( router. recognize( & method :: Get , "/get" ) . is_some( ) ) ;
305
+ assert ! ( router. recognize( & Method :: GET , "/post" ) . is_some( ) ) ;
306
+ assert ! ( router. recognize( & Method :: GET , "/get" ) . is_some( ) ) ;
305
307
}
306
308
307
309
#[ test]
308
310
fn test_request ( ) {
309
311
let mut router = Router :: new ( ) ;
310
312
router. post ( "/post" , |_: & mut Request | {
311
- Ok ( Response :: with ( ( status :: Ok , "" ) ) )
313
+ Ok ( Response :: with ( ( StatusCode :: OK , "" ) ) )
312
314
} , "" ) ;
313
315
router. get ( "/post" , |_: & mut Request | {
314
- Ok ( Response :: with ( ( status :: Ok , "" ) ) )
316
+ Ok ( Response :: with ( ( StatusCode :: OK , "" ) ) )
315
317
} , "" ) ;
316
318
317
- assert ! ( router. recognize( & method :: Post , "/post" ) . is_some( ) ) ;
318
- assert ! ( router. recognize( & method :: Get , "/post" ) . is_some( ) ) ;
319
- assert ! ( router. recognize( & method :: Put , "/post" ) . is_none( ) ) ;
320
- assert ! ( router. recognize( & method :: Get , "/post/" ) . is_none( ) ) ;
319
+ assert ! ( router. recognize( & Method :: POST , "/post" ) . is_some( ) ) ;
320
+ assert ! ( router. recognize( & Method :: GET , "/post" ) . is_some( ) ) ;
321
+ assert ! ( router. recognize( & Method :: PUT , "/post" ) . is_none( ) ) ;
322
+ assert ! ( router. recognize( & Method :: GET , "/post/" ) . is_none( ) ) ;
321
323
}
322
324
323
325
#[ test]
324
326
fn test_not_found ( ) {
325
327
let mut router = Router :: new ( ) ;
326
328
router. put ( "/put" , |_: & mut Request | {
327
- Ok ( Response :: with ( ( status :: Ok , "" ) ) )
329
+ Ok ( Response :: with ( ( StatusCode :: OK , "" ) ) )
328
330
} , "" ) ;
329
- assert ! ( router. recognize( & method :: Patch , "/patch" ) . is_none( ) ) ;
331
+ assert ! ( router. recognize( & Method :: PATCH , "/patch" ) . is_none( ) ) ;
330
332
}
331
333
332
334
#[ test]
333
335
#[ should_panic]
334
336
fn test_same_route_id ( ) {
335
337
let mut router = Router :: new ( ) ;
336
338
router. put ( "/put" , |_: & mut Request | {
337
- Ok ( Response :: with ( ( status :: Ok , "" ) ) )
339
+ Ok ( Response :: with ( ( StatusCode :: OK , "" ) ) )
338
340
} , "my_route_id" ) ;
339
341
router. get ( "/get" , |_: & mut Request | {
340
- Ok ( Response :: with ( ( status :: Ok , "" ) ) )
342
+ Ok ( Response :: with ( ( StatusCode :: OK , "" ) ) )
341
343
} , "my_route_id" ) ;
342
344
}
343
345
344
346
#[ test]
345
347
fn test_wildcard_regression ( ) {
346
348
let mut router = Router :: new ( ) ;
347
- router. options ( "*" , |_: & mut Request | Ok ( Response :: with ( ( status :: Ok , "" ) ) ) , "id1" ) ;
348
- router. put ( "/upload/*filename" , |_: & mut Request | Ok ( Response :: with ( ( status :: Ok , "" ) ) ) , "id2" ) ;
349
- assert ! ( router. recognize( & method :: Options , "/foo" ) . is_some( ) ) ;
350
- assert ! ( router. recognize( & method :: Put , "/foo" ) . is_none( ) ) ;
351
- assert ! ( router. recognize( & method :: Put , "/upload/foo" ) . is_some( ) ) ;
349
+ router. options ( "*" , |_: & mut Request | Ok ( Response :: with ( ( StatusCode :: OK , "" ) ) ) , "id1" ) ;
350
+ router. put ( "/upload/*filename" , |_: & mut Request | Ok ( Response :: with ( ( StatusCode :: OK , "" ) ) ) , "id2" ) ;
351
+ assert ! ( router. recognize( & Method :: OPTIONS , "/foo" ) . is_some( ) ) ;
352
+ assert ! ( router. recognize( & Method :: PUT , "/foo" ) . is_none( ) ) ;
353
+ assert ! ( router. recognize( & Method :: PUT , "/upload/foo" ) . is_some( ) ) ;
352
354
}
353
355
}
0 commit comments