summaryrefslogtreecommitdiffstats
path: root/web/server/h2o/libh2o/t/50errordoc.t
blob: 6f847dae49837a274339be074501db50b2f97684 (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
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
use strict;
use warnings;
use Test::More;
use Test::Exception;
use t::Util;

subtest 'basic' => sub {
    my $server = spawn_h2o(<< "EOT");
hosts:
  default:
    paths:
      /:
        file.dir: @{[DOC_ROOT]}
error-doc:
  status: 404
  url: /404.html
EOT

    my $expected = do {
        open my $fh, '<', "@{[DOC_ROOT]}/404.html"
            or die "failed to read file:@{[DOC_ROOT]}/404.html:$!";
        local $/;
        <$fh>;
    };
    run_with_curl($server, sub {
        my ($proto, $port, $curl) = @_;
        my $resp = `$curl --silent $proto://127.0.0.1:$port/nonexist`;
        is $resp, $expected, "content";
        $resp = `$curl --silent --dump-header /dev/stderr $proto://127.0.0.1:$port/nonexist 2>&1 > /dev/null`;
        like $resp, qr{^HTTP/[^ ]+ 404\s}s, "status";
        like $resp, qr{\r\ncontent-type:\s*text/html.*\r\n}is, "content-type";
        like $resp, qr{\r\ncontent-length:\s*@{[length $expected]}\r\n}is, "content-length";
        unlike $resp, qr{\r\nlast-modified:}is, "no last-modified";
        unlike $resp, qr{\r\etag:}is, "no etag";
    });
};

subtest 'double-error' => sub {
    my $server = spawn_h2o(<< "EOT");
hosts:
  default:
    paths:
      /:
        file.dir: @{[DOC_ROOT]}
error-doc:
  status: 404
  url: /nonexist
EOT

    run_with_curl($server, sub {
        my ($proto, $port, $curl) = @_;
        my $resp = `$curl --silent $proto://127.0.0.1:$port/nonexist`;
        is $resp, "not found", "content";
        $resp = `$curl --silent --dump-header /dev/stderr $proto://127.0.0.1:$port/nonexist 2>&1 > /dev/null`;
        like $resp, qr{^HTTP/[^ ]+ 404\s}s, "status";
        like $resp, qr{\r\ncontent-type:\s*text/plain.*\r\n}is, "content-type";
        like $resp, qr{\r\ncontent-length:\s*@{[length "not found"]}\r\n}is, "content-length";
        unlike $resp, qr{\r\nlast-modified:}is, "no last-modified";
        unlike $resp, qr{\r\etag:}is, "no etag";
    });
};

subtest 'redirect' => sub {
    my $server = spawn_h2o(<< "EOT");
hosts:
  default:
    paths:
      /:
        file.dir: @{[DOC_ROOT]}
error-doc:
  status: 404
  url: /subdir
EOT

    my $expected = do {
        open my $fh, '<', "@{[DOC_ROOT]}/subdir/index.txt"
            or die "failed to read file:@{[DOC_ROOT]}/subdir/index.txt:$!";
        local $/;
        <$fh>;
    };
    run_with_curl($server, sub {
        my ($proto, $port, $curl) = @_;
        my $resp = `$curl --silent $proto://127.0.0.1:$port/nonexist`;
        is $resp, $expected, "content";
        $resp = `$curl --silent --dump-header /dev/stderr $proto://127.0.0.1:$port/nonexist 2>&1 > /dev/null`;
        like $resp, qr{^HTTP/[^ ]+ 404\s}s, "status";
        like $resp, qr{\r\ncontent-type:\s*text/plain.*\r\n}is, "content-type";
        like $resp, qr{\r\ncontent-length:\s*@{[length $expected]}\r\n}is, "content-length";
        unlike $resp, qr{\r\nlast-modified:}is, "no last-modified";
        unlike $resp, qr{\r\etag:}is, "no etag";
    });
};

subtest 'multi-error' => sub {
    my $server = spawn_h2o(<< "EOT");
hosts:
  default:
    paths:
      /:
        file.dir: @{[DOC_ROOT]}
error-doc:
  - status: 404
    url: /404.html
  - status: 500
    url: /500.html
EOT

    run_with_curl($server, sub {
        my ($proto, $port, $curl) = @_;
        my $resp = `$curl --silent --dump-header /dev/stderr $proto://127.0.0.1:$port/nonexist 2>&1 > /dev/null`;
        like $resp, qr{^HTTP/[^ ]+ 404\s}s, "status";
    });
};

subtest 'multi-status-error' => sub {
    my $server = spawn_h2o(<< "EOT");
hosts:
  default:
    paths:
      /:
        file.dir: @{[DOC_ROOT]}
error-doc:
  - status: [404, 405]
    url: /404.html
EOT

    my $expected = do {
        open my $fh, '<', "@{[DOC_ROOT]}/404.html"
            or die "failed to read file:@{[DOC_ROOT]}/404.html:$!";
        local $/;
        <$fh>;
    };
    run_with_curl($server, sub {
        my ($proto, $port, $curl) = @_;
        my $resp = `$curl --silent $proto://127.0.0.1:$port/nonexist`;
        is $resp, $expected, "content";
        $resp = `$curl --silent --dump-header /dev/stderr $proto://127.0.0.1:$port/nonexist 2>&1 > /dev/null`;
        like $resp, qr{^HTTP/[^ ]+ 404\s}s, "status";
    });
    run_with_curl($server, sub {
        my ($proto, $port, $curl) = @_;
        my $resp = `$curl --silent -X POST $proto://127.0.0.1:$port/index.txt`;
        is $resp, $expected, "content";
        $resp = `$curl --silent --dump-header /dev/stderr -X POST $proto://127.0.0.1:$port/index.txt 2>&1 > /dev/null`;
        like $resp, qr{^HTTP/[^ ]+ 405\s}s, "status";
    });
};

subtest "empty status" => sub {
    throws_ok sub {
        spawn_h2o(<< "EOT");
hosts:
  default:
    paths:
      /:
        file.dir: @{[DOC_ROOT]}
error-doc:
  - status: []
    url: /404.html
EOT
    }, qr/server failed to start/, 'status must not be empty';
};

subtest "duplicated statuses" => sub {
    throws_ok sub {
        spawn_h2o(<< "EOT");
hosts:
  default:
    paths:
      /:
        file.dir: @{[DOC_ROOT]}
error-doc:
  - status: [@{[ join(', ', (400 .. 599), 599) ]}]
    url: /error.html
EOT
    }, qr/server failed to start/, 'duplicated statuses';
};

done_testing;