- next is 1.4.56
[lighttpd.git] / tests / cachable.t
blob35b9ea90895c8d6097705aef6065768c3ad33f4f
1 #!/usr/bin/env perl
2 BEGIN {
3 # add current source dir to the include-path
4 # we need this for make distcheck
5 (my $srcdir = $0) =~ s,/[^/]+$,/,;
6 unshift @INC, $srcdir;
9 use strict;
10 use IO::Socket;
11 use Test::More tests => 25;
12 use LightyTest;
14 my $tf = LightyTest->new();
15 my $t;
17 ok($tf->start_proc == 0, "Starting lighttpd") or die();
19 ## check if If-Modified-Since, If-None-Match works
21 $t->{REQUEST} = ( <<EOF
22 GET / HTTP/1.0
23 If-Modified-Since: Sun, 01 Jan 1970 00:00:01 GMT
24 EOF
26 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
27 ok($tf->handle_http($t) == 0, 'Conditional GET - old If-Modified-Since');
29 $t->{REQUEST} = ( <<EOF
30 GET / HTTP/1.0
31 If-Modified-Since: Sun, 01 Jan 1970 00:00:01 GMT; foo
32 EOF
34 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, '+Last-Modified' => ''} ];
35 ok($tf->handle_http($t) == 0, 'Conditional GET - old If-Modified-Since, comment');
37 my $now = $t->{date};
39 $t->{REQUEST} = ( <<EOF
40 GET / HTTP/1.0
41 If-Modified-Since: $now
42 EOF
44 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 304 } ];
45 ok($tf->handle_http($t) == 0, 'Conditional GET - new If-Modified-Since');
47 $t->{REQUEST} = ( <<EOF
48 GET / HTTP/1.0
49 If-Modified-Since: $now; foo
50 EOF
52 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 304 } ];
53 ok($tf->handle_http($t) == 0, 'Conditional GET - new If-Modified-Since, comment');
55 $t->{REQUEST} = ( <<EOF
56 GET / HTTP/1.0
57 If-None-Match: foo
58 EOF
60 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, '+ETag' => ''} ];
61 ok($tf->handle_http($t) == 0, 'Conditional GET - old If-None-Match');
63 my $etag = $t->{etag};
65 $t->{REQUEST} = ( <<EOF
66 GET / HTTP/1.0
67 If-None-Match: $etag
68 EOF
70 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 304 } ];
71 ok($tf->handle_http($t) == 0, 'Conditional GET - old If-None-Match');
73 $t->{REQUEST} = ( <<EOF
74 GET / HTTP/1.0
75 If-None-Match: $etag
76 If-Modified-Since: Sun, 01 Jan 1970 00:00:01 GMT; foo
77 EOF
79 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 304 } ];
80 ok($tf->handle_http($t) == 0, 'Conditional GET - ETag + old Last-Modified (which should be ignored)');
82 $t->{REQUEST} = ( <<EOF
83 GET / HTTP/1.0
84 If-None-Match: $etag
85 If-Modified-Since: $now; foo
86 EOF
88 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 304 } ];
89 ok($tf->handle_http($t) == 0, 'Conditional GET - ETag, Last-Modified + comment (which should be ignored)');
91 $t->{REQUEST} = ( <<EOF
92 GET / HTTP/1.0
93 If-None-Match: Foo
94 If-Modified-Since: Sun, 01 Jan 1970 00:00:01 GMT; foo
95 EOF
97 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
98 ok($tf->handle_http($t) == 0, 'Conditional GET - old ETAG + old Last-Modified');
100 $t->{REQUEST} = ( <<EOF
101 GET / HTTP/1.0
102 If-None-Match: $etag
103 If-Modified-Since: $now foo
106 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 304 } ];
107 ok($tf->handle_http($t) == 0, 'Conditional GET - ETag + Last-Modified + overlong timestamp (which should be ignored)');
109 $t->{REQUEST} = ( <<EOF
110 GET / HTTP/1.0
111 If-None-Match: $etag
112 Host: etag.example.org
115 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
116 ok($tf->handle_http($t) == 0, 'Conditional GET - ETag + disabled etags on server side');
118 ###############
120 ok($etag =~ /^\"(.*)\"$/, "The server must quote ETags");
122 $t->{REQUEST} = ( <<EOF
123 GET / HTTP/1.0
124 If-None-Match: $1
127 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
128 ok($tf->handle_http($t) == 0, 'The client must send a quoted ETag');
130 $etag =~ /^(\".*)\"$/;
131 $t->{REQUEST} = ( <<EOF
132 GET / HTTP/1.0
133 If-None-Match: $1
136 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
137 ok($tf->handle_http($t) == 0, 'The ETag must be surrounded by quotes');
139 $t->{REQUEST} = ( <<EOF
140 GET / HTTP/1.0
141 If-None-Match: *
144 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 304 } ];
145 ok($tf->handle_http($t) == 0, 'An unquoted star matches any ETag');
147 $t->{REQUEST} = ( <<EOF
148 GET / HTTP/1.0
149 If-None-Match: "*"
152 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
153 ok($tf->handle_http($t) == 0, 'A quoted star is just a regular ETag');
156 $t->{REQUEST} = ( <<EOF
157 GET / HTTP/1.0
158 If-None-Match: W/$etag
161 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 304 } ];
162 ok($tf->handle_http($t) == 0, 'A weak etag matches like a regular ETag for HEAD and GET');
165 $t->{REQUEST} = ( <<EOF
166 GET / HTTP/1.0
167 If-None-Match: W/$etag
168 Range: bytes=0-0
171 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 206, 'HTTP-Content' => '<' } ];
172 ok($tf->handle_http($t) == 0, 'A weak etag does not match for ranged requests');
174 $t->{REQUEST} = ( <<EOF
175 GET / HTTP/1.0
176 If-None-Match: W/"12345"
179 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
180 ok($tf->handle_http($t) == 0, 'However, a weak ETag is not *');
182 $t->{REQUEST} = ( <<EOF
183 GET / HTTP/1.0
184 If-None-Match: "12345", $etag
187 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 304 } ];
188 ok($tf->handle_http($t) == 0, 'Client sent a list of ETags, the second matches');
191 $t->{REQUEST} = ( <<EOF
192 GET / HTTP/1.0
193 If-None-Match: "12345", W/$etag
196 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 304 } ];
197 ok($tf->handle_http($t) == 0, 'The second provided ETag matches weakly');
200 $t->{REQUEST} = ( <<EOF
201 GET / HTTP/1.0
202 If-None-Match: "12345",, ,, , $etag
205 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 304 } ];
206 ok($tf->handle_http($t) == 0, 'Broken client did get around to sending good data');
208 $t->{REQUEST} = ( <<EOF
209 GET / HTTP/1.0
210 If-None-Match: "1234", $etag, "brokentrailing
213 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 304 } ];
214 ok($tf->handle_http($t) == 0, 'Bad syntax *after* a matching ETag doesn\'t matter');
216 ok($tf->stop_proc == 0, "Stopping lighttpd");