Mercurial > hg > nginx-tests
annotate stream_udp_proxy.t @ 1606:e4e0695552ed
Tests: fixed stream_proxy_ssl_conf_command.t.
The stream_proxy_ssl_conf_command.t test used stream return module
to return the response. Since this ignores actual request, but the
perl test code used http_get(). This might result in the request being
sent after the response is returned and the connection closed by the server,
resulting in RST being generated and no response seen by the client at all.
Fix is to use "stream(...)->read()" instead of http_get(), so
no request is sent at all, eliminating possibility of RST being
generated.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Tue, 10 Nov 2020 05:03:29 +0300 |
parents | 42e37e0434d5 |
children | f3ba4c74de31 |
rev | line source |
---|---|
868 | 1 #!/usr/bin/perl |
2 | |
3 # (C) Nginx, Inc. | |
4 | |
5 # Tests for stream proxy module with datagrams. | |
6 | |
7 ############################################################################### | |
8 | |
9 use warnings; | |
10 use strict; | |
11 | |
12 use Test::More; | |
13 | |
14 BEGIN { use FindBin; chdir($FindBin::Bin); } | |
15 | |
16 use lib 'lib'; | |
17 use Test::Nginx; | |
18 use Test::Nginx::Stream qw/ dgram /; | |
19 | |
20 ############################################################################### | |
21 | |
22 select STDERR; $| = 1; | |
23 select STDOUT; $| = 1; | |
24 | |
1574
42e37e0434d5
Tests: stream udp tests with zero-length payload (ticket #1982).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
25 my $t = Test::Nginx->new()->has(qw/stream udp/)->plan(8) |
868 | 26 ->write_file_expand('nginx.conf', <<'EOF'); |
27 | |
28 %%TEST_GLOBALS%% | |
29 | |
30 daemon off; | |
31 | |
32 events { | |
33 } | |
34 | |
35 stream { | |
36 proxy_timeout 1s; | |
37 | |
38 server { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
39 listen 127.0.0.1:%%PORT_8980_UDP%% udp; |
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
40 proxy_pass 127.0.0.1:%%PORT_8981_UDP%%; |
868 | 41 |
42 proxy_responses 0; | |
43 } | |
44 | |
45 server { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
46 listen 127.0.0.1:%%PORT_8982_UDP%% udp; |
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
47 proxy_pass 127.0.0.1:%%PORT_8981_UDP%%; |
868 | 48 |
49 proxy_responses 2; | |
50 } | |
51 | |
52 server { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
53 listen 127.0.0.1:%%PORT_8983_UDP%% udp; |
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
54 proxy_pass 127.0.0.1:%%PORT_8981_UDP%%; |
868 | 55 } |
56 } | |
57 | |
58 EOF | |
59 | |
60 | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
61 $t->run_daemon(\&udp_daemon, port(8981), $t); |
1020
196d33c2bb45
Tests: removed TODO and try_run() checks for legacy versions.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
974
diff
changeset
|
62 $t->run(); |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
63 $t->waitforfile($t->testdir . '/' . port(8981)); |
868 | 64 |
65 ############################################################################### | |
66 | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
67 my $s = dgram('127.0.0.1:' . port(8980)); |
935
25a4b2fdd3fb
Tests: I/O timeout options introduced in Stream.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
868
diff
changeset
|
68 is($s->io('1', read => 1, read_timeout => 0.5), '', 'proxy responses 0'); |
868 | 69 |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
70 $s = dgram('127.0.0.1:' . port(8982)); |
868 | 71 is($s->io('1'), '1', 'proxy responses 1'); |
1334
c4f58dfe8207
Tests: stream udp tests adjusted for upcoming udp streams.
Roman Arutyunyan <arut@nginx.com>
parents:
1237
diff
changeset
|
72 $s = dgram('127.0.0.1:' . port(8982)); |
868 | 73 is($s->io('2', read => 2), '12', 'proxy responses 2'); |
74 | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
75 $s = dgram('127.0.0.1:' . port(8983)); |
868 | 76 is($s->io('3', read => 3), '123', 'proxy responses default'); |
77 | |
1574
42e37e0434d5
Tests: stream udp tests with zero-length payload (ticket #1982).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
78 # zero-length payload |
42e37e0434d5
Tests: stream udp tests with zero-length payload (ticket #1982).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
79 |
42e37e0434d5
Tests: stream udp tests with zero-length payload (ticket #1982).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
80 TODO: { |
42e37e0434d5
Tests: stream udp tests with zero-length payload (ticket #1982).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
81 local $TODO = 'not yet' unless $t->has_version('1.19.1'); |
42e37e0434d5
Tests: stream udp tests with zero-length payload (ticket #1982).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
82 |
42e37e0434d5
Tests: stream udp tests with zero-length payload (ticket #1982).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
83 $s = dgram('127.0.0.1:' . port(8982)); |
42e37e0434d5
Tests: stream udp tests with zero-length payload (ticket #1982).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
84 $s->write(''); |
42e37e0434d5
Tests: stream udp tests with zero-length payload (ticket #1982).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
85 is($s->read(), 'zero', 'upstream read zero bytes'); |
42e37e0434d5
Tests: stream udp tests with zero-length payload (ticket #1982).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
86 is($s->read(), '', 'upstream sent zero bytes'); |
42e37e0434d5
Tests: stream udp tests with zero-length payload (ticket #1982).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
87 |
42e37e0434d5
Tests: stream udp tests with zero-length payload (ticket #1982).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
88 $s->write(''); |
42e37e0434d5
Tests: stream udp tests with zero-length payload (ticket #1982).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
89 is($s->read(), 'zero', 'upstream read zero bytes again'); |
42e37e0434d5
Tests: stream udp tests with zero-length payload (ticket #1982).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
90 is($s->read(), '', 'upstream sent zero bytes again'); |
42e37e0434d5
Tests: stream udp tests with zero-length payload (ticket #1982).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
91 |
42e37e0434d5
Tests: stream udp tests with zero-length payload (ticket #1982).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
92 } |
42e37e0434d5
Tests: stream udp tests with zero-length payload (ticket #1982).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
93 |
868 | 94 ############################################################################### |
95 | |
96 sub udp_daemon { | |
97 my ($port, $t) = @_; | |
98 | |
99 my $server = IO::Socket::INET->new( | |
100 Proto => 'udp', | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
101 LocalAddr => '127.0.0.1:' . port(8981), |
868 | 102 Reuse => 1, |
103 ) | |
104 or die "Can't create listening socket: $!\n"; | |
105 | |
106 # signal we are ready | |
107 | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
108 open my $fh, '>', $t->testdir() . '/' . port(8981); |
868 | 109 close $fh; |
110 | |
111 while (1) { | |
112 $server->recv(my $buffer, 65536); | |
1574
42e37e0434d5
Tests: stream udp tests with zero-length payload (ticket #1982).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
113 |
42e37e0434d5
Tests: stream udp tests with zero-length payload (ticket #1982).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
114 if (length($buffer) > 0) { |
42e37e0434d5
Tests: stream udp tests with zero-length payload (ticket #1982).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
115 $server->send($_) for (1 .. $buffer); |
42e37e0434d5
Tests: stream udp tests with zero-length payload (ticket #1982).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
116 |
42e37e0434d5
Tests: stream udp tests with zero-length payload (ticket #1982).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
117 } else { |
42e37e0434d5
Tests: stream udp tests with zero-length payload (ticket #1982).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
118 $server->send('zero'); |
42e37e0434d5
Tests: stream udp tests with zero-length payload (ticket #1982).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
119 select undef, undef, undef, 0.2; |
42e37e0434d5
Tests: stream udp tests with zero-length payload (ticket #1982).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
120 $server->send(''); |
42e37e0434d5
Tests: stream udp tests with zero-length payload (ticket #1982).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
121 } |
868 | 122 } |
123 } | |
124 | |
125 ############################################################################### |