15
|
1 #!/usr/bin/perl
|
|
2
|
|
3 # (C) Maxim Dounin
|
|
4
|
|
5 # Tests for bytes filter module.
|
|
6
|
|
7 ###############################################################################
|
|
8
|
|
9 use warnings;
|
|
10 use strict;
|
|
11
|
|
12 use Test::More;
|
|
13 use Test::Nginx;
|
|
14
|
|
15 ###############################################################################
|
|
16
|
|
17 select STDERR; $| = 1;
|
|
18 select STDOUT; $| = 1;
|
|
19
|
|
20 my $t = Test::Nginx->new()->plan(24);
|
|
21
|
|
22 $t->write_file_expand('nginx.conf', <<'EOF');
|
|
23
|
|
24 master_process off;
|
|
25 daemon off;
|
|
26
|
|
27 events {
|
|
28 }
|
|
29
|
|
30 http {
|
|
31 access_log off;
|
|
32 root %%TESTDIR%%;
|
|
33
|
|
34 client_body_temp_path %%TESTDIR%%/client_body_temp;
|
|
35 fastcgi_temp_path %%TESTDIR%%/fastcgi_temp;
|
|
36 proxy_temp_path %%TESTDIR%%/proxy_temp;
|
|
37
|
|
38 server {
|
|
39 listen 127.0.0.1:8080;
|
|
40 server_name localhost;
|
|
41 location / {
|
|
42 bytes on;
|
|
43 }
|
|
44 }
|
|
45 }
|
|
46
|
|
47 EOF
|
|
48
|
|
49 $t->write_file('t1',
|
|
50 join('', map { sprintf "X%03dXXXXXX", $_ } (0 .. 99)));
|
|
51 $t->run();
|
|
52
|
|
53 ###############################################################################
|
|
54
|
|
55 my $t1;
|
|
56
|
|
57 # normal requests
|
|
58
|
|
59 $t1 = http_get('/t1');
|
|
60 like($t1, qr/200/, 'full reply');
|
|
61 like($t1, qr/Content-Length: 1000/, 'full reply length');
|
|
62 like($t1, qr/^(X[0-9]{3}XXXXXX){100}$/m, 'full reply content');
|
|
63
|
|
64 $t1 = http_get('/t1?bytes=0-');
|
|
65 like($t1, qr/200/, 'full reply filtered');
|
|
66 like($t1, qr/Content-Length: 1000/, 'full reply filtered length');
|
|
67 like($t1, qr/^(X[0-9]{3}XXXXXX){100}$/m, 'full reply filtered content');
|
|
68
|
|
69 $t1 = http_get('/t1?bytes=0-9,100-109,990-');
|
|
70 like($t1, qr/200/, 'complex reply');
|
|
71 like($t1, qr/Content-Length: 30/, 'complex reply length');
|
|
72 like($t1, qr/^(X[0-9]{3}XXXXXX){3}$/m, 'complex reply content');
|
|
73
|
|
74 $t1 = http_get('/t1?bytes=-10');
|
|
75 like($t1, qr/200/, 'final bytes');
|
|
76 like($t1, qr/Content-Length: 10/, 'final bytes length');
|
|
77 like($t1, qr/^X099XXXXXX$/m, 'final bytes content');
|
|
78
|
|
79 # various range requests
|
|
80
|
|
81 $t1 = http_get_range('/t1?bytes=100-', 'Range: bytes=0-9');
|
|
82 like($t1, qr/206/, 'first bytes - 206 partial reply');
|
|
83 like($t1, qr/Content-Length: 10/, 'first bytes - correct length');
|
|
84 like($t1, qr/Content-Range: bytes 0-9\/900/, 'first bytes - content range');
|
|
85 like($t1, qr/^X010X{6}$/m, 'first bytes - correct content');
|
|
86
|
|
87 $t1 = http_get_range('/t1?bytes=100-', 'Range: bytes=-10');
|
|
88 like($t1, qr/206/, 'final bytes - 206 partial reply');
|
|
89 like($t1, qr/Content-Length: 10/, 'final bytes - content length');
|
|
90 like($t1, qr/Content-Range: bytes 890-899\/900/,
|
|
91 'final bytes - content range');
|
|
92 like($t1, qr/^X099XXXXXX$/m, 'final bytes - correct content');
|
|
93
|
|
94 $t1 = http_get_range('/t1?bytes=0-12,100-', 'Range: bytes=0-99');
|
|
95 like($t1, qr/206/, 'multi buffers - 206 partial reply');
|
|
96 like($t1, qr/Content-Length: 100/, 'multi buffers - content length');
|
|
97 like($t1, qr/Content-Range: bytes 0-99\/913/, 'multi buffers - content range');
|
|
98 like($t1, qr/^X000X{6}X00X010XXXXXX(X01[1-7]XXXXXX){7}X018XXX$/m,
|
|
99 'multi buffers - correct content');
|
|
100
|
|
101 ###############################################################################
|
|
102
|
|
103 sub http_get_range {
|
|
104 my ($url, $extra) = @_;
|
|
105 return http(<<EOF);
|
|
106 GET $url HTTP/1.1
|
|
107 Host: localhost
|
|
108 Connection: close
|
|
109 $extra
|
|
110
|
|
111 EOF
|
|
112 }
|
|
113
|
|
114 ###############################################################################
|