Ticket #543: http_keepalive_proxy-2.2.t

File http_keepalive_proxy-2.2.t, 3.9 KB (added by Markus Linnala, 12 years ago)
Line 
1#!/usr/bin/perl
2
3# (C) Maxim Dounin
4# (C) Markus Linnala
5
6# Tests keepalive is kept regardless proxy type.
7
8###############################################################################
9
10use warnings;
11use strict;
12
13use Test::More;
14
15use IO::Socket::INET;
16use Socket qw/ CRLF /;
17
18BEGIN { use FindBin; chdir($FindBin::Bin); }
19
20use lib 'lib';
21use Test::Nginx;
22
23###############################################################################
24
25select STDERR; $| = 1;
26select STDOUT; $| = 1;
27
28plan(skip_all => 'win32') if $^O eq 'MSWin32';
29
30my $t = Test::Nginx->new()->has(qw/http proxy upstream_keepalive sub/)->plan(2)
31 ->write_file_expand('nginx.conf', <<'EOF');
32
33%%TEST_GLOBALS%%
34
35daemon off;
36
37events {
38}
39
40http {
41 %%TEST_GLOBALS_HTTP%%
42
43 upstream backend {
44 server 127.0.0.1:8081;
45 }
46
47 server {
48 listen 127.0.0.1:8080;
49 server_name localhost;
50
51 location / {
52 proxy_pass http://backend;
53 proxy_http_version 1.1;
54 proxy_set_header Connection "";
55 }
56 }
57}
58
59EOF
60
61$t->run_daemon(\&http_daemon);
62$t->run()->waitforsocket('127.0.0.1:8081')
63 or die "Can't start test backend";
64
65###############################################################################
66
67like(http_ka('/','1.0'), qr/Connection: keep-alive/, 'keepalive/1.0 proxy chunked');
68like(http_cl('/','1.0'), qr/Connection: close/, 'close/1.0 proxy chunked');
69
70like(http_ka('/','1.1'), qr/Connection: keep-alive/, 'keepalive/1.1 proxy chunked');
71like(http_cl('/','1.1'), qr/Connection: close/, 'close/1.1 proxy chunked');
72
73###############################################################################
74
75sub http_ka {
76 my ($q, $p) = @_;
77 my_http(<<EOF, 'sleep' => 0.1);
78GET $q HTTP/$p
79Host: localhost
80Connection: Keep-Alive
81
82EOF
83}
84
85sub http_cl {
86 my ($q, $p) = @_;
87 my_http(<<EOF, 'sleep' => 0.1);
88GET $q HTTP/$p
89Host: localhost
90Connection: close
91
92EOF
93}
94
95###############################################################################
96
97sub my_http {
98 my ($request, %extra) = @_;
99 my $reply;
100
101 eval {
102 local $SIG{ALRM} = sub { die "timeout\n" };
103 local $SIG{PIPE} = sub { die "sigpipe\n" };
104 alarm(5);
105
106 my $s = $extra{socket} || IO::Socket::INET->new(
107 Proto => 'tcp',
108 PeerAddr => '127.0.0.1:8080',
109 )
110 or die "Can't connect to nginx: $!\n";
111
112 log_out($request);
113 $s->print($request);
114
115 select undef, undef, undef, $extra{sleep} if $extra{sleep};
116 return '' if $extra{aborted};
117
118 if ($extra{body}) {
119 log_out($extra{body});
120 $s->print($extra{body});
121 }
122
123 # Done writing
124 $s->shutdown(1);
125
126 local $/;
127 $reply = $s->getline();
128
129 alarm(0);
130 };
131 alarm(0);
132 if ($@) {
133 log_in("died: $@");
134 return undef;
135 }
136
137 log_in($reply);
138 return $reply;
139}
140
141sub http_daemon {
142 my $server = IO::Socket::INET->new(
143 Proto => 'tcp',
144 LocalHost => '127.0.0.1:8081',
145 Listen => 5,
146 Reuse => 1
147 )
148 or die "Can't create listening socket: $!\n";
149
150 # dumb server which is able to keep connections alive
151
152 while (my $client = $server->accept()) {
153 $client->autoflush(1);
154
155 while (1) {
156 my $headers = '';
157 my $uri = '';
158
159 while (<$client>) {
160 $headers .= $_;
161 last if (/^\x0d?\x0a?$/);
162 }
163
164 last if $headers eq '';
165
166 $uri = $1 if $headers =~ /^\S+\s+([^ ]+)\s+HTTP/i;
167
168 print { $client }
169 "HTTP/1.1 200 OK" . CRLF .
170 "Transfer-Encoding: chunked" . CRLF .
171 CRLF .
172 "a" . CRLF .
173 "finished" . CRLF .
174 CRLF . "0" . CRLF . CRLF;
175
176 last;
177 }
178
179 close $client;
180 }
181}
182
183###############################################################################