annotate mirror_proxy.t @ 1728:6d5ecf445e57

Tests: added HTTP/2 test with big request body. Notably, it is useful with body buffering in filters, in which case the stream window is flow controlled based on the preread buffer.
author Sergey Kandaurov <pluknet@nginx.com>
date Sat, 04 Sep 2021 14:50:02 +0300
parents 8b7ab9245916
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1208
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1 #!/usr/bin/perl
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
2
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
3 # (C) Sergey Kandaurov
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
4 # (C) Nginx, Inc.
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
5
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
6 # Tests for http mirror module and it's interaction with proxy.
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
7
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
8 ###############################################################################
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
9
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
10 use warnings;
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
11 use strict;
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
12
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
13 use Test::More;
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
14
1703
8b7ab9245916 Tests: mirror_proxy.t speedup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1381
diff changeset
15 use IO::Select;
8b7ab9245916 Tests: mirror_proxy.t speedup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1381
diff changeset
16
1208
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
17 BEGIN { use FindBin; chdir($FindBin::Bin); }
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
18
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
19 use lib 'lib';
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
20 use Test::Nginx;
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
21
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
22 ###############################################################################
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
23
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
24 select STDERR; $| = 1;
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
25 select STDOUT; $| = 1;
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
26
1366
e16eecc84b49 Tests: added mirror test with subrequest delayed in limit_req.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1208
diff changeset
27 my $t = Test::Nginx->new()->has(qw/http proxy mirror rewrite limit_req/);
1208
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
28
1381
97c8280de681 Tests: removed TODO and try_run() checks for legacy versions.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1368
diff changeset
29 $t->write_file_expand('nginx.conf', <<'EOF')->plan(7);
1208
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
30
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
31 %%TEST_GLOBALS%%
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
32
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
33 daemon off;
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
34
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
35 events {
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
36 }
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
37
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
38 http {
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
39 %%TEST_GLOBALS_HTTP%%
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
40
1366
e16eecc84b49 Tests: added mirror test with subrequest delayed in limit_req.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1208
diff changeset
41 limit_req_zone $uri zone=slow:1m rate=30r/m;
e16eecc84b49 Tests: added mirror test with subrequest delayed in limit_req.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1208
diff changeset
42 log_format test $request_uri:$request_body;
1208
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
43
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
44 server {
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
45 listen 127.0.0.1:8080;
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
46 server_name localhost;
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
47
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
48 location / {
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
49 mirror /mirror;
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
50 proxy_pass http://127.0.0.1:8081;
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
51 }
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
52
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
53 location /off {
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
54 mirror /mirror/off;
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
55 mirror_request_body off;
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
56 proxy_pass http://127.0.0.1:8081;
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
57 }
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
58
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
59 location /mirror {
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
60 internal;
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
61 proxy_pass http://127.0.0.1:8082;
1366
e16eecc84b49 Tests: added mirror test with subrequest delayed in limit_req.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1208
diff changeset
62 limit_req zone=slow burst=1;
1208
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
63 }
1703
8b7ab9245916 Tests: mirror_proxy.t speedup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1381
diff changeset
64
8b7ab9245916 Tests: mirror_proxy.t speedup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1381
diff changeset
65 location /mirror/off {
8b7ab9245916 Tests: mirror_proxy.t speedup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1381
diff changeset
66 internal;
8b7ab9245916 Tests: mirror_proxy.t speedup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1381
diff changeset
67 proxy_pass http://127.0.0.1:8082;
8b7ab9245916 Tests: mirror_proxy.t speedup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1381
diff changeset
68 proxy_set_header Content-Length "";
8b7ab9245916 Tests: mirror_proxy.t speedup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1381
diff changeset
69 }
1208
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
70 }
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
71
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
72 server {
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
73 listen 127.0.0.1:8081;
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
74 listen 127.0.0.1:8082;
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
75 server_name localhost;
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
76
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
77 location / {
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
78 proxy_pass http://127.0.0.1:$server_port/return204;
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
79 access_log %%TESTDIR%%/test.log test;
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
80 add_header X-Body $request_body;
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
81 }
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
82
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
83 location /return204 {
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
84 return 204;
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
85 }
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
86 }
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
87 }
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
88
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
89 EOF
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
90
1381
97c8280de681 Tests: removed TODO and try_run() checks for legacy versions.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1368
diff changeset
91 $t->run();
1208
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
92
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
93 ###############################################################################
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
94
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
95 like(http_post('/'), qr/X-Body: 1234567890\x0d?$/m, 'mirror proxy');
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
96 like(http_post('/off'), qr/X-Body: 1234567890\x0d?$/m, 'mirror_request_body');
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
97
1366
e16eecc84b49 Tests: added mirror test with subrequest delayed in limit_req.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1208
diff changeset
98 # delayed subrequest should not affect main request processing nor stuck itself
e16eecc84b49 Tests: added mirror test with subrequest delayed in limit_req.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1208
diff changeset
99
1703
8b7ab9245916 Tests: mirror_proxy.t speedup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1381
diff changeset
100 my $s = http_post('/delay?1', start => 1);
8b7ab9245916 Tests: mirror_proxy.t speedup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1381
diff changeset
101 like(read_keepalive($s), qr/X-Body: 1234567890\x0d?$/m, 'mirror delay');
1366
e16eecc84b49 Tests: added mirror test with subrequest delayed in limit_req.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1208
diff changeset
102
1703
8b7ab9245916 Tests: mirror_proxy.t speedup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1381
diff changeset
103 $t->todo_alerts();
1208
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
104 $t->stop();
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
105
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
106 my $log = $t->read_file('test.log');
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
107 like($log, qr!^/:1234567890$!m, 'log - request body');
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
108 like($log, qr!^/mirror:1234567890$!m, 'log - request body in mirror');
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
109 like($log, qr!^/off:1234567890$!m, 'log - mirror_request_body off');
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
110 like($log, qr!^/mirror/off:-$!m,, 'log - mirror_request_body off in mirror');
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
111
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
112 ###############################################################################
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
113
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
114 sub http_post {
1703
8b7ab9245916 Tests: mirror_proxy.t speedup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1381
diff changeset
115 my ($url, %extra) = @_;
1208
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
116
1703
8b7ab9245916 Tests: mirror_proxy.t speedup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1381
diff changeset
117 http(<<EOF, %extra);
1208
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
118 POST $url HTTP/1.0
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
119 Host: localhost
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
120 Content-Length: 10
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
121
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
122 1234567890
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
123 EOF
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
124 }
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
125
1703
8b7ab9245916 Tests: mirror_proxy.t speedup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1381
diff changeset
126 sub read_keepalive {
8b7ab9245916 Tests: mirror_proxy.t speedup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1381
diff changeset
127 my ($s) = @_;
8b7ab9245916 Tests: mirror_proxy.t speedup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1381
diff changeset
128 my $data = '';
8b7ab9245916 Tests: mirror_proxy.t speedup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1381
diff changeset
129
8b7ab9245916 Tests: mirror_proxy.t speedup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1381
diff changeset
130 while (IO::Select->new($s)->can_read(3)) {
8b7ab9245916 Tests: mirror_proxy.t speedup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1381
diff changeset
131 sysread($s, my $buffer, 4096) or last;
8b7ab9245916 Tests: mirror_proxy.t speedup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1381
diff changeset
132 $data .= $buffer;
8b7ab9245916 Tests: mirror_proxy.t speedup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1381
diff changeset
133 last if $data =~ /^\x0d\x0a/ms;
8b7ab9245916 Tests: mirror_proxy.t speedup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1381
diff changeset
134 }
8b7ab9245916 Tests: mirror_proxy.t speedup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1381
diff changeset
135
8b7ab9245916 Tests: mirror_proxy.t speedup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1381
diff changeset
136 log_in($data);
8b7ab9245916 Tests: mirror_proxy.t speedup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1381
diff changeset
137 return $data;
8b7ab9245916 Tests: mirror_proxy.t speedup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1381
diff changeset
138 }
8b7ab9245916 Tests: mirror_proxy.t speedup.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1381
diff changeset
139
1208
a6453cf5786a Tests: http mirror module tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
140 ###############################################################################