0
|
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 use Socket qw/ CRLF /;
|
|
16
|
|
17 ###############################################################################
|
|
18
|
|
19 select STDERR; $| = 1;
|
|
20 select STDOUT; $| = 1;
|
|
21
|
|
22 my $t = Test::Nginx->new()->has(qw/http proxy rewrite/)->plan(6)
|
|
23 ->write_file_expand('nginx.conf', <<'EOF');
|
|
24
|
|
25 %%TEST_GLOBALS%%
|
|
26
|
|
27 daemon off;
|
|
28
|
|
29 events {
|
|
30 }
|
|
31
|
|
32 http {
|
|
33 %%TEST_GLOBALS_HTTP%%
|
|
34
|
|
35 server {
|
|
36 listen 127.0.0.1:8080;
|
|
37 server_name localhost;
|
|
38 location / {
|
|
39 catch_body on;
|
|
40 proxy_pass http://127.0.0.1:8080/empty;
|
|
41 }
|
|
42 location /empty {
|
|
43 return 200 "test response body\n";
|
|
44 }
|
|
45 }
|
|
46 }
|
|
47
|
|
48 EOF
|
|
49
|
|
50 $t->write_file('index.html', 'SEE-THIS');
|
|
51 $t->run();
|
|
52
|
|
53 ###############################################################################
|
|
54
|
|
55 like(get_body('/', '123456'), qr/200 OK/, 'normal');
|
|
56 like(get_body('/', '12345X'), qr/403 Forbidden/, 'rejected');
|
|
57
|
|
58 # pipelining
|
|
59
|
|
60 like(get_body('/', '123456', '12345X'),
|
|
61 qr/200 OK.*403 Forbidden/ms,
|
|
62 'second rejected');
|
|
63
|
|
64 like(get_body('/', '123456' x 1024, '12345X6789' x 1024, '123456' x 1024),
|
|
65 qr/200 OK.*403 Forbidden.*200 OK/ms,
|
|
66 'accepted rejected accepted');
|
|
67
|
|
68 # pipelining with chunked
|
|
69
|
|
70 like(get_chunked('/', '123456', '12345X'),
|
|
71 qr/200 OK.*403 Forbidden/ms,
|
|
72 'second rejected');
|
|
73
|
|
74 like(get_chunked('/', '123456', '12345X6789', '123456'),
|
|
75 qr/200 OK.*403 Forbidden.*200 OK/ms,
|
|
76 'accepted rejected accepted');
|
|
77
|
|
78 ###############################################################################
|
|
79
|
|
80 sub get_body {
|
|
81 my $uri = shift;
|
|
82 my $last = pop;
|
|
83 return http( join '', (map {
|
|
84 my $body = $_;
|
|
85 "GET $uri HTTP/1.1" . CRLF
|
|
86 . "Host: localhost" . CRLF
|
|
87 . "Content-Length: " . (length $body) . CRLF . CRLF
|
|
88 . $body
|
|
89 } @_),
|
|
90 "GET $uri HTTP/1.1" . CRLF
|
|
91 . "Host: localhost" . CRLF
|
|
92 . "Connection: close" . CRLF
|
|
93 . "Content-Length: " . (length $last) . CRLF . CRLF
|
|
94 . $last
|
|
95 );
|
|
96 }
|
|
97
|
|
98 sub get_chunked {
|
|
99 my $uri = shift;
|
|
100 my $last = pop;
|
|
101 return http( join '', (map {
|
|
102 my $body = $_;
|
|
103 "GET $uri HTTP/1.1" . CRLF
|
|
104 . "Host: localhost" . CRLF
|
|
105 . "Transfer-Encoding: chunked" . CRLF . CRLF
|
|
106 . sprintf("%x", length $body) . CRLF
|
|
107 . $body . CRLF
|
|
108 . "0" . CRLF . CRLF
|
|
109 } @_),
|
|
110 "GET $uri HTTP/1.1" . CRLF
|
|
111 . "Host: localhost" . CRLF
|
|
112 . "Connection: close" . CRLF
|
|
113 . "Transfer-Encoding: chunked" . CRLF . CRLF
|
|
114 . sprintf("%x", length $last) . CRLF
|
|
115 . $last . CRLF
|
|
116 . "0" . CRLF . CRLF
|
|
117 );
|
|
118 }
|
|
119
|
|
120 ###############################################################################
|