1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
use std::marker::PhantomData;

use regex::Regex;
use futures::future::{Future, ok, err};
use http::{header, StatusCode, Error as HttpError};

use body::Body;
use error::Error;
use httprequest::HttpRequest;
use httpresponse::HttpResponse;

/// Trait defines object that could be registered as route handler
#[allow(unused_variables)]
pub trait Handler<S>: 'static {

    /// The type of value that handler will return.
    type Result: Responder;

    /// Handle request
    fn handle(&mut self, req: HttpRequest<S>) -> Self::Result;
}

/// Trait implemented by types that generate responses for clients.
///
/// Types that implement this trait can be used as the return type of a handler.
pub trait Responder {
    /// The associated item which can be returned.
    type Item: Into<Reply>;

    /// The associated error which can be returned.
    type Error: Into<Error>;

    /// Convert itself to `Reply` or `Error`.
    fn respond_to(self, req: HttpRequest) -> Result<Self::Item, Self::Error>;
}

/// Combines two different responder types into a single type
///
/// ```rust
/// # extern crate actix_web;
/// # extern crate futures;
/// # use futures::future::Future;
/// use actix_web::AsyncResponder;
/// use futures::future::result;
/// use actix_web::{Either, Error, HttpRequest, HttpResponse, httpcodes};
///
/// type RegisterResult = Either<HttpResponse, Box<Future<Item=HttpResponse, Error=Error>>>;
///
///
/// fn index(req: HttpRequest) -> RegisterResult {
///     if is_a_variant() { // <- choose variant A
///         Either::A(
///             httpcodes::HttpBadRequest.with_body("Bad data"))
///     } else {
///         Either::B(      // <- variant B
///             result(HttpResponse::Ok()
///                    .content_type("text/html")
///                    .body(format!("Hello!"))
///                    .map_err(|e| e.into())).responder())
///     }
/// }
/// # fn is_a_variant() -> bool { true }
/// # fn main() {}
/// ```
#[derive(Debug)]
pub enum Either<A, B> {
    /// First branch of the type
    A(A),
    /// Second branch of the type
    B(B),
}

impl<A, B> Responder for Either<A, B>
    where A: Responder, B: Responder
{
    type Item = Reply;
    type Error = Error;

    fn respond_to(self, req: HttpRequest) -> Result<Reply, Error> {
        match self {
            Either::A(a) => match a.respond_to(req) {
                Ok(val) => Ok(val.into()),
                Err(err) => Err(err.into()),
            },
            Either::B(b) => match b.respond_to(req) {
                Ok(val) => Ok(val.into()),
                Err(err) => Err(err.into()),
            },
        }
    }
}


#[doc(hidden)]
/// Convenience trait that convert `Future` object into `Boxed` future
pub trait AsyncResponder<I, E>: Sized {
    fn responder(self) -> Box<Future<Item=I, Error=E>>;
}

impl<F, I, E> AsyncResponder<I, E> for F
    where F: Future<Item=I, Error=E> + 'static,
          I: Responder + 'static,
          E: Into<Error> + 'static,
{
    fn responder(self) -> Box<Future<Item=I, Error=E>> {
        Box::new(self)
    }
}

/// Handler<S> for Fn()
impl<F, R, S> Handler<S> for F
    where F: Fn(HttpRequest<S>) -> R + 'static,
          R: Responder + 'static
{
    type Result = R;

    fn handle(&mut self, req: HttpRequest<S>) -> R {
        (self)(req)
    }
}

/// Represents response process.
pub struct Reply(ReplyItem);

pub(crate) enum ReplyItem {
    Message(HttpResponse),
    Future(Box<Future<Item=HttpResponse, Error=Error>>),
}

impl Reply {

    /// Create async response
    #[inline]
    pub fn async<F>(fut: F) -> Reply
        where F: Future<Item=HttpResponse, Error=Error> + 'static
    {
        Reply(ReplyItem::Future(Box::new(fut)))
    }

    /// Send response
    #[inline]
    pub fn response<R: Into<HttpResponse>>(response: R) -> Reply {
        Reply(ReplyItem::Message(response.into()))
    }

    #[inline]
    pub(crate) fn into(self) -> ReplyItem {
        self.0
    }

    #[cfg(test)]
    pub(crate) fn as_response(&self) -> Option<&HttpResponse> {
        match self.0 {
            ReplyItem::Message(ref resp) => Some(resp),
            _ => None,
        }
    }
}

impl Responder for Reply {
    type Item = Reply;
    type Error = Error;

    fn respond_to(self, _: HttpRequest) -> Result<Reply, Error> {
        Ok(self)
    }
}

impl Responder for HttpResponse {
    type Item = Reply;
    type Error = Error;

    #[inline]
    fn respond_to(self, _: HttpRequest) -> Result<Reply, Error> {
        Ok(Reply(ReplyItem::Message(self)))
    }
}

impl From<HttpResponse> for Reply {

    #[inline]
    fn from(resp: HttpResponse) -> Reply {
        Reply(ReplyItem::Message(resp))
    }
}

impl<T: Responder, E: Into<Error>> Responder for Result<T, E>
{
    type Item = <T as Responder>::Item;
    type Error = Error;

    fn respond_to(self, req: HttpRequest) -> Result<Self::Item, Self::Error> {
        match self {
            Ok(val) => match val.respond_to(req) {
                Ok(val) => Ok(val),
                Err(err) => Err(err.into()),
            },
            Err(err) => Err(err.into()),
        }
    }
}

impl<E: Into<Error>> From<Result<Reply, E>> for Reply {
    #[inline]
    fn from(res: Result<Reply, E>) -> Self {
        match res {
            Ok(val) => val,
            Err(err) => Reply(ReplyItem::Message(err.into().into())),
        }
    }
}

impl<E: Into<Error>> From<Result<HttpResponse, E>> for Reply {
    #[inline]
    fn from(res: Result<HttpResponse, E>) -> Self {
        match res {
            Ok(val) => Reply(ReplyItem::Message(val)),
            Err(err) => Reply(ReplyItem::Message(err.into().into())),
        }
    }
}

impl From<Box<Future<Item=HttpResponse, Error=Error>>> for Reply {
    #[inline]
    fn from(fut: Box<Future<Item=HttpResponse, Error=Error>>) -> Reply {
        Reply(ReplyItem::Future(fut))
    }
}

impl<I, E> Responder for Box<Future<Item=I, Error=E>>
    where I: Responder + 'static,
          E: Into<Error> + 'static
{
    type Item = Reply;
    type Error = Error;

    #[inline]
    fn respond_to(self, req: HttpRequest) -> Result<Reply, Error> {
        let fut = self.map_err(|e| e.into())
            .then(move |r| {
                match r.respond_to(req) {
                    Ok(reply) => match reply.into().0 {
                        ReplyItem::Message(resp) => ok(resp),
                        _ => panic!("Nested async replies are not supported"),
                    },
                    Err(e) => err(e),
                }
            });
        Ok(Reply::async(fut))
    }
}

/// Trait defines object that could be registered as resource route
pub(crate) trait RouteHandler<S>: 'static {
    fn handle(&mut self, req: HttpRequest<S>) -> Reply;
}

/// Route handler wrapper for Handler
pub(crate)
struct WrapHandler<S, H, R>
    where H: Handler<S, Result=R>,
          R: Responder,
          S: 'static,
{
    h: H,
    s: PhantomData<S>,
}

impl<S, H, R> WrapHandler<S, H, R>
    where H: Handler<S, Result=R>,
          R: Responder,
          S: 'static,
{
    pub fn new(h: H) -> Self {
        WrapHandler{h, s: PhantomData}
    }
}

impl<S, H, R> RouteHandler<S> for WrapHandler<S, H, R>
    where H: Handler<S, Result=R>,
          R: Responder + 'static,
          S: 'static,
{
    fn handle(&mut self, req: HttpRequest<S>) -> Reply {
        let req2 = req.without_state();
        match self.h.handle(req).respond_to(req2) {
            Ok(reply) => reply.into(),
            Err(err) => Reply::response(err.into()),
        }
    }
}

/// Async route handler
pub(crate)
struct AsyncHandler<S, H, F, R, E>
    where H: Fn(HttpRequest<S>) -> F + 'static,
          F: Future<Item=R, Error=E> + 'static,
          R: Responder + 'static,
          E: Into<Error> + 'static,
          S: 'static,
{
    h: Box<H>,
    s: PhantomData<S>,
}

impl<S, H, F, R, E> AsyncHandler<S, H, F, R, E>
    where H: Fn(HttpRequest<S>) -> F + 'static,
          F: Future<Item=R, Error=E> + 'static,
          R: Responder + 'static,
          E: Into<Error> + 'static,
          S: 'static,
{
    pub fn new(h: H) -> Self {
        AsyncHandler{h: Box::new(h), s: PhantomData}
    }
}

impl<S, H, F, R, E> RouteHandler<S> for AsyncHandler<S, H, F, R, E>
    where H: Fn(HttpRequest<S>) -> F + 'static,
          F: Future<Item=R, Error=E> + 'static,
          R: Responder + 'static,
          E: Into<Error> + 'static,
          S: 'static,
{
    fn handle(&mut self, req: HttpRequest<S>) -> Reply {
        let req2 = req.without_state();
        let fut = (self.h)(req)
            .map_err(|e| e.into())
            .then(move |r| {
                match r.respond_to(req2) {
                    Ok(reply) => match reply.into().0 {
                        ReplyItem::Message(resp) => ok(resp),
                        _ => panic!("Nested async replies are not supported"),
                    },
                    Err(e) => err(e),
                }
            });
        Reply::async(fut)
    }
}

/// Path normalization helper
///
/// By normalizing it means:
///
/// - Add a trailing slash to the path.
/// - Remove a trailing slash from the path.
/// - Double slashes are replaced by one.
///
/// The handler returns as soon as it finds a path that resolves
/// correctly. The order if all enable is 1) merge, 3) both merge and append
/// and 3) append. If the path resolves with
/// at least one of those conditions, it will redirect to the new path.
///
/// If *append* is *true* append slash when needed. If a resource is
/// defined with trailing slash and the request comes without it, it will
/// append it automatically.
///
/// If *merge* is *true*, merge multiple consecutive slashes in the path into one.
///
/// This handler designed to be use as a handler for application's *default resource*.
///
/// ```rust
/// # extern crate actix_web;
/// # #[macro_use] extern crate serde_derive;
/// # use actix_web::*;
/// #
/// # fn index(req: HttpRequest) -> httpcodes::StaticResponse {
/// #     httpcodes::HttpOk
/// # }
/// fn main() {
///     let app = Application::new()
///         .resource("/test/", |r| r.f(index))
///         .default_resource(|r| r.h(NormalizePath::default()))
///         .finish();
/// }
/// ```
/// In this example `/test`, `/test///` will be redirected to `/test/` url.
pub struct NormalizePath {
    append: bool,
    merge: bool,
    re_merge: Regex,
    redirect: StatusCode,
    not_found: StatusCode,
}

impl Default for NormalizePath {
    /// Create default `NormalizePath` instance, *append* is set to *true*,
    /// *merge* is set to *true* and *redirect* is set to `StatusCode::MOVED_PERMANENTLY`
    fn default() -> NormalizePath {
        NormalizePath {
            append: true,
            merge: true,
            re_merge: Regex::new("//+").unwrap(),
            redirect: StatusCode::MOVED_PERMANENTLY,
            not_found: StatusCode::NOT_FOUND,
        }
    }
}

impl NormalizePath {
    /// Create new `NormalizePath` instance
    pub fn new(append: bool, merge: bool, redirect: StatusCode) -> NormalizePath {
        NormalizePath {
            append,
            merge,
            redirect,
            re_merge: Regex::new("//+").unwrap(),
            not_found: StatusCode::NOT_FOUND,
        }
    }
}

impl<S> Handler<S> for NormalizePath {
    type Result = Result<HttpResponse, HttpError>;

    fn handle(&mut self, req: HttpRequest<S>) -> Self::Result {
        if let Some(router) = req.router() {
            let query = req.query_string();
            if self.merge {
                // merge slashes
                let p = self.re_merge.replace_all(req.path(), "/");
                if p.len() != req.path().len() {
                    if router.has_route(p.as_ref()) {
                        let p = if !query.is_empty() { p + "?" + query } else { p };
                        return HttpResponse::build(self.redirect)
                            .header(header::LOCATION, p.as_ref())
                            .body(Body::Empty);
                    }
                    // merge slashes and append trailing slash
                    if self.append && !p.ends_with('/') {
                        let p = p.as_ref().to_owned() + "/";
                        if router.has_route(&p) {
                            let p = if !query.is_empty() { p + "?" + query } else { p };
                            return HttpResponse::build(self.redirect)
                                .header(header::LOCATION, p.as_str())
                                .body(Body::Empty);
                        }
                    }

                    // try to remove trailing slash
                    if p.ends_with('/') {
                        let p = p.as_ref().trim_right_matches('/');
                        if router.has_route(p) {
                            let mut req = HttpResponse::build(self.redirect);
                            return if !query.is_empty() {
                                req.header(header::LOCATION, (p.to_owned() + "?" + query).as_str())
                            } else {
                                req.header(header::LOCATION, p)
                            }
                            .body(Body::Empty);
                        }
                    }
                } else if p.ends_with('/') {
                    // try to remove trailing slash
                    let p = p.as_ref().trim_right_matches('/');
                    if router.has_route(p) {
                        let mut req = HttpResponse::build(self.redirect);
                        return if !query.is_empty() {
                            req.header(header::LOCATION, (p.to_owned() + "?" + query).as_str())
                        } else {
                            req.header(header::LOCATION, p)
                        }
                        .body(Body::Empty);
                    }
                }
            }
            // append trailing slash
            if self.append && !req.path().ends_with('/') {
                let p = req.path().to_owned() + "/";
                if router.has_route(&p) {
                    let p = if !query.is_empty() { p + "?" + query } else { p };
                    return HttpResponse::build(self.redirect)
                        .header(header::LOCATION, p.as_str())
                        .body(Body::Empty);
                }
            }
        }
        Ok(HttpResponse::new(self.not_found, Body::Empty))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use http::{header, Method};
    use test::TestRequest;
    use application::Application;

    fn index(_req: HttpRequest) -> HttpResponse {
        HttpResponse::new(StatusCode::OK, Body::Empty)
    }

    #[test]
    fn test_normalize_path_trailing_slashes() {
        let mut app = Application::new()
            .resource("/resource1", |r| r.method(Method::GET).f(index))
            .resource("/resource2/", |r| r.method(Method::GET).f(index))
            .default_resource(|r| r.h(NormalizePath::default()))
            .finish();

        // trailing slashes
        let params =
            vec![("/resource1", "", StatusCode::OK),
                 ("/resource1/", "/resource1", StatusCode::MOVED_PERMANENTLY),
                 ("/resource2", "/resource2/", StatusCode::MOVED_PERMANENTLY),
                 ("/resource2/", "", StatusCode::OK),
                 ("/resource1?p1=1&p2=2", "", StatusCode::OK),
                 ("/resource1/?p1=1&p2=2", "/resource1?p1=1&p2=2", StatusCode::MOVED_PERMANENTLY),
                 ("/resource2?p1=1&p2=2", "/resource2/?p1=1&p2=2",
                  StatusCode::MOVED_PERMANENTLY),
                 ("/resource2/?p1=1&p2=2", "", StatusCode::OK)
            ];
        for (path, target, code) in params {
            let req = app.prepare_request(TestRequest::with_uri(path).finish());
            let resp = app.run(req);
            let r = resp.as_response().unwrap();
            assert_eq!(r.status(), code);
            if !target.is_empty() {
                assert_eq!(
                    target,
                    r.headers().get(header::LOCATION).unwrap().to_str().unwrap());
            }
        }
    }

    #[test]
    fn test_normalize_path_trailing_slashes_disabled() {
        let mut app = Application::new()
            .resource("/resource1", |r| r.method(Method::GET).f(index))
            .resource("/resource2/", |r| r.method(Method::GET).f(index))
            .default_resource(|r| r.h(
                NormalizePath::new(false, true, StatusCode::MOVED_PERMANENTLY)))
            .finish();

        // trailing slashes
        let params = vec![("/resource1", StatusCode::OK),
                          ("/resource1/", StatusCode::MOVED_PERMANENTLY),
                          ("/resource2", StatusCode::NOT_FOUND),
                          ("/resource2/", StatusCode::OK),
                          ("/resource1?p1=1&p2=2", StatusCode::OK),
                          ("/resource1/?p1=1&p2=2", StatusCode::MOVED_PERMANENTLY),
                          ("/resource2?p1=1&p2=2", StatusCode::NOT_FOUND),
                          ("/resource2/?p1=1&p2=2", StatusCode::OK)
        ];
        for (path, code) in params {
            let req = app.prepare_request(TestRequest::with_uri(path).finish());
            let resp = app.run(req);
            let r = resp.as_response().unwrap();
            assert_eq!(r.status(), code);
        }
    }

    #[test]
    fn test_normalize_path_merge_slashes() {
        let mut app = Application::new()
            .resource("/resource1", |r| r.method(Method::GET).f(index))
            .resource("/resource1/a/b", |r| r.method(Method::GET).f(index))
            .default_resource(|r| r.h(NormalizePath::default()))
            .finish();

        // trailing slashes
        let params = vec![
            ("/resource1/a/b", "", StatusCode::OK),
            ("/resource1/", "/resource1", StatusCode::MOVED_PERMANENTLY),
            ("/resource1//", "/resource1", StatusCode::MOVED_PERMANENTLY),
            ("//resource1//a//b", "/resource1/a/b", StatusCode::MOVED_PERMANENTLY),
            ("//resource1//a//b/", "/resource1/a/b", StatusCode::MOVED_PERMANENTLY),
            ("//resource1//a//b//", "/resource1/a/b", StatusCode::MOVED_PERMANENTLY),
            ("///resource1//a//b", "/resource1/a/b", StatusCode::MOVED_PERMANENTLY),
            ("/////resource1/a///b", "/resource1/a/b", StatusCode::MOVED_PERMANENTLY),
            ("/////resource1/a//b/", "/resource1/a/b", StatusCode::MOVED_PERMANENTLY),
            ("/resource1/a/b?p=1", "", StatusCode::OK),
            ("//resource1//a//b?p=1", "/resource1/a/b?p=1", StatusCode::MOVED_PERMANENTLY),
            ("//resource1//a//b/?p=1", "/resource1/a/b?p=1", StatusCode::MOVED_PERMANENTLY),
            ("///resource1//a//b?p=1", "/resource1/a/b?p=1", StatusCode::MOVED_PERMANENTLY),
            ("/////resource1/a///b?p=1", "/resource1/a/b?p=1", StatusCode::MOVED_PERMANENTLY),
            ("/////resource1/a//b/?p=1", "/resource1/a/b?p=1", StatusCode::MOVED_PERMANENTLY),
            ("/////resource1/a//b//?p=1", "/resource1/a/b?p=1", StatusCode::MOVED_PERMANENTLY),
        ];
        for (path, target, code) in params {
            let req = app.prepare_request(TestRequest::with_uri(path).finish());
            let resp = app.run(req);
            let r = resp.as_response().unwrap();
            assert_eq!(r.status(), code);
            if !target.is_empty() {
                assert_eq!(
                    target,
                    r.headers().get(header::LOCATION).unwrap().to_str().unwrap());
            }
        }
    }

    #[test]
    fn test_normalize_path_merge_and_append_slashes() {
        let mut app = Application::new()
            .resource("/resource1", |r| r.method(Method::GET).f(index))
            .resource("/resource2/", |r| r.method(Method::GET).f(index))
            .resource("/resource1/a/b", |r| r.method(Method::GET).f(index))
            .resource("/resource2/a/b/", |r| r.method(Method::GET).f(index))
            .default_resource(|r| r.h(NormalizePath::default()))
            .finish();

        // trailing slashes
        let params = vec![
            ("/resource1/a/b", "", StatusCode::OK),
            ("/resource1/a/b/", "/resource1/a/b", StatusCode::MOVED_PERMANENTLY),
            ("//resource2//a//b", "/resource2/a/b/", StatusCode::MOVED_PERMANENTLY),
            ("//resource2//a//b/", "/resource2/a/b/", StatusCode::MOVED_PERMANENTLY),
            ("//resource2//a//b//", "/resource2/a/b/", StatusCode::MOVED_PERMANENTLY),
            ("///resource1//a//b", "/resource1/a/b", StatusCode::MOVED_PERMANENTLY),
            ("///resource1//a//b/", "/resource1/a/b", StatusCode::MOVED_PERMANENTLY),
            ("/////resource1/a///b", "/resource1/a/b", StatusCode::MOVED_PERMANENTLY),
            ("/////resource1/a///b/", "/resource1/a/b", StatusCode::MOVED_PERMANENTLY),
            ("/resource2/a/b", "/resource2/a/b/", StatusCode::MOVED_PERMANENTLY),
            ("/resource2/a/b/", "", StatusCode::OK),
            ("//resource2//a//b", "/resource2/a/b/", StatusCode::MOVED_PERMANENTLY),
            ("//resource2//a//b/", "/resource2/a/b/", StatusCode::MOVED_PERMANENTLY),
            ("///resource2//a//b", "/resource2/a/b/", StatusCode::MOVED_PERMANENTLY),
            ("///resource2//a//b/", "/resource2/a/b/", StatusCode::MOVED_PERMANENTLY),
            ("/////resource2/a///b", "/resource2/a/b/", StatusCode::MOVED_PERMANENTLY),
            ("/////resource2/a///b/", "/resource2/a/b/", StatusCode::MOVED_PERMANENTLY),
            ("/resource1/a/b?p=1", "", StatusCode::OK),
            ("/resource1/a/b/?p=1", "/resource1/a/b?p=1", StatusCode::MOVED_PERMANENTLY),
            ("//resource2//a//b?p=1", "/resource2/a/b/?p=1", StatusCode::MOVED_PERMANENTLY),
            ("//resource2//a//b/?p=1", "/resource2/a/b/?p=1", StatusCode::MOVED_PERMANENTLY),
            ("///resource1//a//b?p=1", "/resource1/a/b?p=1", StatusCode::MOVED_PERMANENTLY),
            ("///resource1//a//b/?p=1", "/resource1/a/b?p=1", StatusCode::MOVED_PERMANENTLY),
            ("/////resource1/a///b?p=1", "/resource1/a/b?p=1", StatusCode::MOVED_PERMANENTLY),
            ("/////resource1/a///b/?p=1", "/resource1/a/b?p=1", StatusCode::MOVED_PERMANENTLY),
            ("/////resource1/a///b//?p=1", "/resource1/a/b?p=1", StatusCode::MOVED_PERMANENTLY),
            ("/resource2/a/b?p=1", "/resource2/a/b/?p=1", StatusCode::MOVED_PERMANENTLY),
            ("//resource2//a//b?p=1", "/resource2/a/b/?p=1", StatusCode::MOVED_PERMANENTLY),
            ("//resource2//a//b/?p=1", "/resource2/a/b/?p=1", StatusCode::MOVED_PERMANENTLY),
            ("///resource2//a//b?p=1", "/resource2/a/b/?p=1", StatusCode::MOVED_PERMANENTLY),
            ("///resource2//a//b/?p=1", "/resource2/a/b/?p=1", StatusCode::MOVED_PERMANENTLY),
            ("/////resource2/a///b?p=1", "/resource2/a/b/?p=1", StatusCode::MOVED_PERMANENTLY),
            ("/////resource2/a///b/?p=1", "/resource2/a/b/?p=1", StatusCode::MOVED_PERMANENTLY),
        ];
        for (path, target, code) in params {
            let req = app.prepare_request(TestRequest::with_uri(path).finish());
            let resp = app.run(req);
            let r = resp.as_response().unwrap();
            assert_eq!(r.status(), code);
            if !target.is_empty() {
                assert_eq!(
                    target, r.headers().get(header::LOCATION).unwrap().to_str().unwrap());
            }
        }
    }


}