Mercurial > hg > nginx-tests
annotate stream_proxy.t @ 1248:70192b1baf01
Tests: added exception test to stream_js.t using 'require'.
The stream js tests introduced in edf5a3c9e36a fail on njs 0.1.14. It doesn't
currently provide an easy way to check its version, whilst we are obligated to
gracefully handle such cases somehow. With such an addition of 'require', now
the tests are skipped instead on the previous versions.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Tue, 21 Nov 2017 13:16:39 +0300 |
parents | 3fc6817cd84a |
children | f3ba4c74de31 |
rev | line source |
---|---|
555
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
1 #!/usr/bin/perl |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
2 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
3 # (C) Sergey Kandaurov |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
4 # (C) Nginx, Inc. |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
5 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
6 # Tests for stream proxy module. |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
7 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
8 ############################################################################### |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
9 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
10 use warnings; |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
11 use strict; |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
12 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
13 use Test::More; |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
14 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
15 use IO::Select; |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
16 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
17 BEGIN { use FindBin; chdir($FindBin::Bin); } |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
18 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
19 use lib 'lib'; |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
20 use Test::Nginx; |
816
77359b849cd5
Tests: stream package.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
795
diff
changeset
|
21 use Test::Nginx::Stream qw/ stream /; |
555
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
22 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
23 ############################################################################### |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
24 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
25 select STDERR; $| = 1; |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
26 select STDOUT; $| = 1; |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
27 |
732
984bfe661cce
Tests: stream and http proxy_connect_timeout tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
711
diff
changeset
|
28 my $t = Test::Nginx->new()->has(qw/stream/)->plan(5) |
555
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
29 ->write_file_expand('nginx.conf', <<'EOF'); |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
30 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
31 %%TEST_GLOBALS%% |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
32 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
33 daemon off; |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
34 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
35 events { |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
36 } |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
37 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
38 stream { |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
39 server { |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
40 listen 127.0.0.1:8080; |
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
41 proxy_pass 127.0.0.1:8081; |
795
122cd3a82367
Tests: fixed 'proxy connect timeout' test timeouts on slow hosts.
Sergey Kandaurov <pluknet@nginx.com>
parents:
732
diff
changeset
|
42 proxy_connect_timeout 2s; |
555
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
43 } |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
44 } |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
45 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
46 EOF |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
47 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
48 $t->run_daemon(\&stream_daemon); |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
49 $t->run()->waitforsocket('127.0.0.1:' . port(8081)); |
555
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
50 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
51 ############################################################################### |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
52 |
1235
3fc6817cd84a
Tests: explicit peer port in stream tests now required.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
974
diff
changeset
|
53 my $s = stream('127.0.0.1:' . port(8080)); |
555
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
54 |
816
77359b849cd5
Tests: stream package.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
795
diff
changeset
|
55 is($s->io('foo1', length => 4), 'bar1', 'proxy connection'); |
77359b849cd5
Tests: stream package.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
795
diff
changeset
|
56 is($s->io('foo3', length => 4), 'bar3', 'proxy connection again'); |
77359b849cd5
Tests: stream package.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
795
diff
changeset
|
57 is($s->io('close'), 'close', 'proxy connection close'); |
77359b849cd5
Tests: stream package.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
795
diff
changeset
|
58 is($s->io('test'), '', 'proxy connection closed'); |
555
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
59 |
1235
3fc6817cd84a
Tests: explicit peer port in stream tests now required.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
974
diff
changeset
|
60 $s = stream('127.0.0.1:' . port(8080)); |
732
984bfe661cce
Tests: stream and http proxy_connect_timeout tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
711
diff
changeset
|
61 |
795
122cd3a82367
Tests: fixed 'proxy connect timeout' test timeouts on slow hosts.
Sergey Kandaurov <pluknet@nginx.com>
parents:
732
diff
changeset
|
62 sleep 3; |
732
984bfe661cce
Tests: stream and http proxy_connect_timeout tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
711
diff
changeset
|
63 |
816
77359b849cd5
Tests: stream package.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
795
diff
changeset
|
64 is($s->io('foo', length => 3), 'bar', 'proxy connect timeout'); |
555
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
65 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
66 ############################################################################### |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
67 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
68 sub stream_daemon { |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
69 my $server = IO::Socket::INET->new( |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
70 Proto => 'tcp', |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
71 LocalAddr => '127.0.0.1:' . port(8081), |
555
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
72 Listen => 5, |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
73 Reuse => 1 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
74 ) |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
75 or die "Can't create listening socket: $!\n"; |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
76 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
77 my $sel = IO::Select->new($server); |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
78 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
79 local $SIG{PIPE} = 'IGNORE'; |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
80 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
81 while (my @ready = $sel->can_read) { |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
82 foreach my $fh (@ready) { |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
83 if ($server == $fh) { |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
84 my $new = $fh->accept; |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
85 $new->autoflush(1); |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
86 $sel->add($new); |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
87 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
88 } elsif (stream_handle_client($fh)) { |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
89 $sel->remove($fh); |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
90 $fh->close; |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
91 } |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
92 } |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
93 } |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
94 } |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
95 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
96 sub stream_handle_client { |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
97 my ($client) = @_; |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
98 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
99 log2c("(new connection $client)"); |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
100 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
101 $client->sysread(my $buffer, 65536) or return 1; |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
102 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
103 log2i("$client $buffer"); |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
104 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
105 $buffer =~ s/foo/bar/g; |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
106 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
107 log2o("$client $buffer"); |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
108 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
109 $client->syswrite($buffer); |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
110 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
111 return $buffer =~ /close/; |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
112 } |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
113 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
114 sub log2i { Test::Nginx::log_core('|| <<', @_); } |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
115 sub log2o { Test::Nginx::log_core('|| >>', @_); } |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
116 sub log2c { Test::Nginx::log_core('||', @_); } |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
117 |
ba6cc90e3d67
Tests: basic stream proxy tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
118 ############################################################################### |