annotate proxy_unix.t @ 804:6b2512f859a0

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