Skip to content

Commit fc98016

Browse files
committed
All necessary changes to remain compatible to iron after the upgrade to hyper 0.12
1 parent 82cdd92 commit fc98016

File tree

6 files changed

+71
-77
lines changed

6 files changed

+71
-77
lines changed

examples/custom_404.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ extern crate router;
55
// To use, go to http://localhost:3000/foobar to see the custom 404
66
// Or, go to http://localhost:3000 for a standard 200 OK
77

8-
use iron::{Iron, Request, Response, IronResult, AfterMiddleware, Chain};
8+
use iron::{Iron, Request, Response, IronResult, AfterMiddleware, Chain, StatusCode};
99
use iron::error::{IronError};
10-
use iron::status;
1110
use router::{Router, NoRoute};
1211

1312
struct Custom404;
@@ -17,7 +16,7 @@ impl AfterMiddleware for Custom404 {
1716
println!("Hitting custom 404 middleware");
1817

1918
if err.error.is::<NoRoute>() {
20-
Ok(Response::with((status::NotFound, "Custom 404 response")))
19+
Ok(Response::with((StatusCode::NOT_FOUND, "Custom 404 response")))
2120
} else {
2221
Err(err)
2322
}
@@ -31,9 +30,9 @@ fn main() {
3130
let mut chain = Chain::new(router);
3231
chain.link_after(Custom404);
3332

34-
Iron::new(chain).http("localhost:3000").unwrap();
33+
Iron::new(chain).http("localhost:3000");
3534
}
3635

3736
fn handler(_: &mut Request) -> IronResult<Response> {
38-
Ok(Response::with((status::Ok, "Handling response")))
37+
Ok(Response::with((StatusCode::OK, "Handling response")))
3938
}

examples/simple.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,24 @@ extern crate router;
55
// To use, go to http://localhost:3000/test and see output "test"
66
// Or, go to http://localhost:3000 to see a default "OK"
77

8-
use iron::{Iron, Request, Response, IronResult};
9-
use iron::status;
8+
use iron::{Iron, Request, Response, IronResult, StatusCode};
109
use router::{Router};
1110

1211
fn main() {
1312
let mut router = Router::new();
1413
router.get("/", handler, "handler");
1514
router.get("/:query", query_handler, "query_handler");
1615

17-
Iron::new(router).http("localhost:3000").unwrap();
16+
Iron::new(router).http("localhost:3000");
1817

1918
fn handler(_: &mut Request) -> IronResult<Response> {
20-
Ok(Response::with((status::Ok, "OK")))
19+
Ok(Response::with((StatusCode::OK, "OK")))
2120
}
2221

2322
fn query_handler(req: &mut Request) -> IronResult<Response> {
2423
let ref query = req.extensions.get::<Router>()
2524
.unwrap().find("query").unwrap_or("/");
26-
Ok(Response::with((status::Ok, *query)))
25+
Ok(Response::with((StatusCode::OK, *query)))
2726
}
2827

2928

examples/simple_with_macro.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,21 @@ extern crate router;
66
// To use, go to http://localhost:3000/test and see output "test"
77
// Or, go to http://localhost:3000 to see a default "OK"
88

9-
use iron::{Iron, Request, Response, IronResult};
10-
use iron::status;
9+
use iron::{Iron, Request, Response, IronResult, StatusCode};
1110
use router::{Router};
1211

1312
fn main() {
1413
let router = router!(root: get "/" => handler, query: get "/:query" => query_handler);
1514

16-
Iron::new(router).http("localhost:3000").unwrap();
15+
Iron::new(router).http("localhost:3000");
1716

1817
fn handler(_: &mut Request) -> IronResult<Response> {
19-
Ok(Response::with((status::Ok, "OK")))
18+
Ok(Response::with((StatusCode::OK, "OK")))
2019
}
2120

2221
fn query_handler(req: &mut Request) -> IronResult<Response> {
2322
let ref query = req.extensions.get::<Router>()
2423
.unwrap().find("query").unwrap_or("/");
25-
Ok(Response::with((status::Ok, *query)))
24+
Ok(Response::with((StatusCode::OK, *query)))
2625
}
2726
}

examples/struct_handler.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
extern crate iron;
22
extern crate router;
33

4-
use iron::Handler;
5-
use iron::status;
6-
use iron::IronResult;
7-
use iron::Response;
8-
use iron::Request;
9-
use iron::Iron;
4+
use iron::{Handler, StatusCode, IronResult, Response, Request, Iron};
105
use router::Router;
116

127
struct MessageHandler {
@@ -15,7 +10,7 @@ struct MessageHandler {
1510

1611
impl Handler for MessageHandler {
1712
fn handle(&self, _: &mut Request) -> IronResult<Response> {
18-
Ok(Response::with((status::Ok, self.message.clone())))
13+
Ok(Response::with((StatusCode::OK, self.message.clone())))
1914
}
2015
}
2116

@@ -27,5 +22,5 @@ fn main() {
2722
let mut router = Router::new();
2823
router.get("/", handler, "index");
2924

30-
Iron::new(router).http("localhost:3000").unwrap();
25+
Iron::new(router).http("localhost:3000");
3126
}

examples/url_for.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ extern crate iron;
88
// Go to http://localhost:3000/foo to see "foo".
99

1010
use iron::prelude::*;
11-
use iron::status;
11+
use iron::StatusCode;
1212
use router::Router;
1313

1414
fn main() {
@@ -17,11 +17,11 @@ fn main() {
1717
id_2: get "/:query" => query_handler
1818
};
1919

20-
Iron::new(router).http("localhost:3000").unwrap();
20+
Iron::new(router).http("localhost:3000");
2121

2222
fn handler(r: &mut Request) -> IronResult<Response> {
2323
Ok(Response::with((
24-
status::Ok,
24+
StatusCode::OK,
2525
format!("Please go to: {}",
2626
url_for!(r, "id_2",
2727
"query" => "test",
@@ -32,7 +32,7 @@ fn main() {
3232
fn query_handler(req: &mut Request) -> IronResult<Response> {
3333
let ref query = req.extensions.get::<Router>()
3434
.unwrap().find("query").unwrap_or("/");
35-
Ok(Response::with((status::Ok, *query)))
35+
Ok(Response::with((StatusCode::OK, *query)))
3636
}
3737

3838

src/router.rs

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::fmt;
44
use std::sync::Arc;
55

66
use iron::{Request, Response, Handler, IronResult, IronError};
7-
use iron::{status, method, headers};
7+
use iron::{StatusCode, method, Method, headers};
88
use iron::typemap::Key;
99
use iron::modifiers::Redirect;
1010

@@ -94,37 +94,37 @@ impl Router {
9494

9595
/// Like route, but specialized to the `Get` method.
9696
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)
9898
}
9999

100100
/// Like route, but specialized to the `Post` method.
101101
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)
103103
}
104104

105105
/// Like route, but specialized to the `Put` method.
106106
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)
108108
}
109109

110110
/// Like route, but specialized to the `Delete` method.
111111
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)
113113
}
114114

115115
/// Like route, but specialized to the `Head` method.
116116
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)
118118
}
119119

120120
/// Like route, but specialized to the `Patch` method.
121121
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)
123123
}
124124

125125
/// Like route, but specialized to the `Options` method.
126126
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)
128128
}
129129

130130
/// Route will match any method, including gibberish.
@@ -143,8 +143,8 @@ impl Router {
143143

144144
fn handle_options(&self, path: &str) -> Response {
145145
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];
148148

149149
// Get all the available methods and return them.
150150
let mut options = vec![];
@@ -157,12 +157,14 @@ impl Router {
157157
});
158158
}
159159
// 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);
162162
}
163163

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+
}
166168
res
167169
}
168170

@@ -188,7 +190,7 @@ impl Router {
188190

189191
self.recognize(&req.method, &path).and(
190192
Some(IronError::new(TrailingSlash,
191-
(status::MovedPermanently, Redirect(url))))
193+
(StatusCode::MOVED_PERMANENTLY, Redirect(url))))
192194
)
193195
}
194196

@@ -211,15 +213,15 @@ impl Handler for Router {
211213

212214
self.handle_method(req, &path).unwrap_or_else(||
213215
match req.method {
214-
method::Options => Ok(self.handle_options(&path)),
216+
Method::OPTIONS => Ok(self.handle_options(&path)),
215217
// 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;
218220
self.handle_method(req, &path).unwrap_or(
219-
Err(IronError::new(NoRoute, status::NotFound))
221+
Err(IronError::new(NoRoute, StatusCode::NOT_FOUND))
220222
)
221223
}
222-
_ => Err(IronError::new(NoRoute, status::NotFound))
224+
_ => Err(IronError::new(NoRoute, StatusCode::NOT_FOUND))
223225
}
224226
)
225227
}
@@ -258,96 +260,96 @@ impl Error for TrailingSlash {
258260
#[cfg(test)]
259261
mod test {
260262
use super::Router;
261-
use iron::{headers, method, status, Request, Response};
263+
use iron::{headers, method, Method, StatusCode, Request, Response};
262264

263265
#[test]
264266
fn test_handle_options_post() {
265267
let mut router = Router::new();
266268
router.post("/", |_: &mut Request| {
267-
Ok(Response::with((status::Ok, "")))
269+
Ok(Response::with((StatusCode::OK, "")))
268270
}, "");
269271
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);
273275
}
274276

275277
#[test]
276278
fn test_handle_options_get_head() {
277279
let mut router = Router::new();
278280
router.get("/", |_: &mut Request| {
279-
Ok(Response::with((status::Ok, "")))
281+
Ok(Response::with((StatusCode::OK, "")))
280282
}, "");
281283
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);
285287
}
286288

287289
#[test]
288290
fn test_handle_any_ok() {
289291
let mut router = Router::new();
290292
router.post("/post", |_: &mut Request| {
291-
Ok(Response::with((status::Ok, "")))
293+
Ok(Response::with((StatusCode::OK, "")))
292294
}, "");
293295
router.any("/post", |_: &mut Request| {
294-
Ok(Response::with((status::Ok, "")))
296+
Ok(Response::with((StatusCode::OK, "")))
295297
}, "");
296298
router.put("/post", |_: &mut Request| {
297-
Ok(Response::with((status::Ok, "")))
299+
Ok(Response::with((StatusCode::OK, "")))
298300
}, "");
299301
router.any("/get", |_: &mut Request| {
300-
Ok(Response::with((status::Ok, "")))
302+
Ok(Response::with((StatusCode::OK, "")))
301303
}, "any");
302304

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());
305307
}
306308

307309
#[test]
308310
fn test_request() {
309311
let mut router = Router::new();
310312
router.post("/post", |_: &mut Request| {
311-
Ok(Response::with((status::Ok, "")))
313+
Ok(Response::with((StatusCode::OK, "")))
312314
}, "");
313315
router.get("/post", |_: &mut Request| {
314-
Ok(Response::with((status::Ok, "")))
316+
Ok(Response::with((StatusCode::OK, "")))
315317
}, "");
316318

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());
321323
}
322324

323325
#[test]
324326
fn test_not_found() {
325327
let mut router = Router::new();
326328
router.put("/put", |_: &mut Request| {
327-
Ok(Response::with((status::Ok, "")))
329+
Ok(Response::with((StatusCode::OK, "")))
328330
}, "");
329-
assert!(router.recognize(&method::Patch, "/patch").is_none());
331+
assert!(router.recognize(&Method::PATCH, "/patch").is_none());
330332
}
331333

332334
#[test]
333335
#[should_panic]
334336
fn test_same_route_id() {
335337
let mut router = Router::new();
336338
router.put("/put", |_: &mut Request| {
337-
Ok(Response::with((status::Ok, "")))
339+
Ok(Response::with((StatusCode::OK, "")))
338340
}, "my_route_id");
339341
router.get("/get", |_: &mut Request| {
340-
Ok(Response::with((status::Ok, "")))
342+
Ok(Response::with((StatusCode::OK, "")))
341343
}, "my_route_id");
342344
}
343345

344346
#[test]
345347
fn test_wildcard_regression() {
346348
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());
352354
}
353355
}

0 commit comments

Comments
 (0)