Mercurial > hg > nginx-tests
comparison proxy_unix.t @ 798:faf2e460b951
Tests: basic proxy tests with unix socket.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Wed, 09 Dec 2015 14:44:34 +0300 |
parents | |
children | bf87c406f81d |
comparison
equal
deleted
inserted
replaced
797:55ca38e1a3d9 | 798:faf2e460b951 |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Maxim Dounin | |
4 # (C) Sergey Kandaurov | |
5 # (C) Nginx, Inc. | |
6 | |
7 # Tests for http proxy module with unix socket. | |
8 | |
9 ############################################################################### | |
10 | |
11 use warnings; | |
12 use strict; | |
13 | |
14 use Test::More; | |
15 | |
16 BEGIN { use FindBin; chdir($FindBin::Bin); } | |
17 | |
18 use lib 'lib'; | |
19 use Test::Nginx; | |
20 | |
21 ############################################################################### | |
22 | |
23 select STDERR; $| = 1; | |
24 select STDOUT; $| = 1; | |
25 | |
26 eval { require IO::Socket::UNIX; }; | |
27 plan(skip_all => 'IO::Socket::UNIX not installed') if $@; | |
28 | |
29 my $t = Test::Nginx->new()->has(qw/http proxy/)->plan(4); | |
30 | |
31 $t->write_file_expand('nginx.conf', <<'EOF'); | |
32 | |
33 %%TEST_GLOBALS%% | |
34 | |
35 daemon off; | |
36 | |
37 events { | |
38 } | |
39 | |
40 http { | |
41 %%TEST_GLOBALS_HTTP%% | |
42 | |
43 server { | |
44 listen 127.0.0.1:8080; | |
45 server_name localhost; | |
46 | |
47 location / { | |
48 proxy_pass http://unix:/%%TESTDIR%%/unix.sock; | |
49 proxy_read_timeout 1s; | |
50 proxy_connect_timeout 2s; | |
51 } | |
52 | |
53 location /var { | |
54 proxy_pass http://$arg_b; | |
55 proxy_read_timeout 1s; | |
56 } | |
57 } | |
58 } | |
59 | |
60 EOF | |
61 | |
62 my $d = $t->testdir(); | |
63 | |
64 $t->run_daemon(\&http_daemon, $d); | |
65 $t->run(); | |
66 | |
67 # wait for unix socket to appear | |
68 | |
69 for (1 .. 50) { | |
70 last if -S "$d/unix.sock"; | |
71 select undef, undef, undef, 0.1; | |
72 } | |
73 | |
74 ############################################################################### | |
75 | |
76 like(http_get('/'), qr/SEE-THIS/, 'proxy request'); | |
77 like(http_get('/multi'), qr/AND-THIS/, 'proxy request with multiple packets'); | |
78 | |
79 unlike(http_head('/'), qr/SEE-THIS/, 'proxy head request'); | |
80 | |
81 TODO: { | |
82 local $TODO = 'not yet'; | |
83 | |
84 like(http_get("/var?b=unix:/$d/unix.sock:/"), qr/SEE-THIS/, 'proxy variables'); | |
85 | |
86 } | |
87 | |
88 ############################################################################### | |
89 | |
90 sub http_daemon { | |
91 my ($d) = @_; | |
92 | |
93 my $server = IO::Socket::UNIX->new( | |
94 Proto => 'tcp', | |
95 Local => "$d/unix.sock", | |
96 Listen => 5, | |
97 Reuse => 1 | |
98 ) | |
99 or die "Can't create listening socket: $!\n"; | |
100 | |
101 local $SIG{PIPE} = 'IGNORE'; | |
102 | |
103 while (my $client = $server->accept()) { | |
104 $client->autoflush(1); | |
105 | |
106 my $headers = ''; | |
107 my $uri = ''; | |
108 | |
109 while (<$client>) { | |
110 $headers .= $_; | |
111 last if (/^\x0d?\x0a?$/); | |
112 } | |
113 | |
114 $uri = $1 if $headers =~ /^\S+\s+([^ ]+)\s+HTTP/i; | |
115 | |
116 if ($uri eq '/') { | |
117 print $client <<'EOF'; | |
118 HTTP/1.1 200 OK | |
119 Connection: close | |
120 | |
121 EOF | |
122 print $client "TEST-OK-IF-YOU-SEE-THIS" | |
123 unless $headers =~ /^HEAD/i; | |
124 | |
125 } elsif ($uri eq '/multi') { | |
126 | |
127 print $client <<"EOF"; | |
128 HTTP/1.1 200 OK | |
129 Connection: close | |
130 | |
131 TEST-OK-IF-YOU-SEE-THIS | |
132 EOF | |
133 | |
134 select undef, undef, undef, 0.1; | |
135 print $client 'AND-THIS'; | |
136 | |
137 } else { | |
138 | |
139 print $client <<"EOF"; | |
140 HTTP/1.1 404 Not Found | |
141 Connection: close | |
142 | |
143 Oops, '$uri' not found | |
144 EOF | |
145 } | |
146 | |
147 close $client; | |
148 } | |
149 } | |
150 | |
151 ############################################################################### |