summaryrefslogtreecommitdiffstats
path: root/web/server/h2o/libh2o/t/50redirect.t
blob: b407063a06709a42e46b2087812ea5b0473b0d20 (plain)
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
use strict;
use warnings;
use Test::More;
use t::Util;

subtest "basic" => sub {
    plan skip_all => 'curl not found'
        unless prog_exists('curl');

    my $curl = "curl --insecure";
    $curl .= " --http1.1"
        if curl_supports_http2();

    my $server = spawn_h2o(<< "EOT");
hosts:
  default:
    paths:
      /:
        redirect: https://example.com/
      /abc/:
        redirect:
          status: 301
          url:    http://example.net/bar/
EOT

    my $doit = sub {
        my ($url, $expected_status, $expected_location) = @_;
        subtest $url => sub {
            my ($stderr, $stdout) = run_prog("$curl --silent --show-error --max-redirs 0 --dump-header /dev/stderr $url");
            like $stderr, qr{^HTTP/[^ ]+ $expected_status\s}s, "status";
            like $stderr, qr{^location: ?$expected_location\r$}im, "location";
        };
    };

    $doit->("http://127.0.0.1:$server->{port}/foo", 302, "https://example.com/foo");
    $doit->("https://127.0.0.1:$server->{tls_port}/foo", 302, "https://example.com/foo");
    $doit->("http://127.0.0.1:$server->{port}/abc/foo/baz", 301, "http://example.net/bar/foo/baz");
    $doit->("http://127.0.0.1:$server->{port}/abc/foo:baz", 301, "http://example.net/bar/foo:baz");
    $doit->("http://127.0.0.1:$server->{port}/foo?abc=def", 302, qr{https://example.com/foo\?abc=def});
    $doit->("http://127.0.0.1:$server->{port}/foo%0D%0Aa:1", 302, "https://example\.com/foo\%0d\%0aa:1");
};

subtest "trailing-slash" => sub {
    my $server = spawn_h2o(<< "EOT");
hosts:
  default:
    paths:
      /p1:
        redirect: /dest
      /p2:
        redirect: /dest/
      /p3/:
        redirect: /dest
      /p4/:
        redirect: /dest/
EOT

    run_with_curl($server, sub {
        my ($proto, $port, $cmd) = @_;
        my $fetch = sub {
            my $path = shift;
            my ($stderr, $stdout) = run_prog("$cmd --silent --show-error --max-redirs 0 --dump-header /dev/stderr $proto://127.0.0.1:$port$path");
            $stderr;
        };
        subtest "p1" => sub {
            like $fetch->("/p1"), qr{^location:\s*/dest\r$}im;
            like $fetch->("/p12"), qr{^HTTP/\S*\s+404}is;
            like $fetch->("/p1?abc"), qr{^location:\s*/dest\?abc\r$}im;
            like $fetch->("/p1/"), qr{^location:\s*/dest/\r$}im;
            like $fetch->("/p1/abc"), qr{^location:\s*/dest/abc\r$}im;
        };
        subtest "p2" => sub {
            like $fetch->("/p2"), qr{^location:\s*/dest/\r$}im;
            like $fetch->("/p12"), qr{^HTTP/\S*\s+404}is;
            like $fetch->("/p2?abc"), qr{^location:\s*/dest/\?abc\r$}im;
            like $fetch->("/p2/"), qr{^location:\s*/dest/\r$}im;
            like $fetch->("/p2/abc"), qr{^location:\s*/dest/abc\r$}im;
        };
        subtest "p3" => sub {
            like $fetch->("/p3"), qr{^HTTP/\S*\s+404}is;
            like $fetch->("/p3/"), qr{^location:\s*/dest/\r$}im;
            like $fetch->("/p3/abc"), qr{^location:\s*/dest/abc\r$}im;
        };
        subtest "p4" => sub {
            like $fetch->("/p4"), qr{^HTTP/\S*\s+404}is;
            like $fetch->("/p4/"), qr{^location:\s*/dest/\r$}im;
            like $fetch->("/p4/abc"), qr{^location:\s*/dest/abc\r$}im;
        };
    });
};

done_testing;