Ticket #543: http_keepalive_proxy-2.t

File http_keepalive_proxy-2.t, 3.7 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 }
54 }
55}
56
57EOF
58
59$t->run_daemon(\&http_daemon);
60$t->run()->waitforsocket('127.0.0.1:8081')
61 or die "Can't start test backend";
62
63###############################################################################
64
65like(http_ka('/','1.0'), qr/Connection: keep-alive/, 'keepalive/1.0 proxy chunked');
66like(http_cl('/','1.0'), qr/Connection: close/, 'close/1.0 proxy chunked');
67
68###############################################################################
69
70sub http_ka {
71 my ($q, $p) = @_;
72 my_http(<<EOF, 'sleep' => 0.1);
73GET $q HTTP/$p
74Host: localhost
75Connection: Keep-Alive
76
77EOF
78}
79
80sub http_cl {
81 my ($q, $p) = @_;
82 my_http(<<EOF, 'sleep' => 0.1);
83GET $q HTTP/$p
84Host: localhost
85Connection: close
86
87EOF
88}
89
90###############################################################################
91
92sub my_http {
93 my ($request, %extra) = @_;
94 my $reply;
95
96 eval {
97 local $SIG{ALRM} = sub { die "timeout\n" };
98 local $SIG{PIPE} = sub { die "sigpipe\n" };
99 alarm(5);
100
101 my $s = $extra{socket} || IO::Socket::INET->new(
102 Proto => 'tcp',
103 PeerAddr => '127.0.0.1:8080',
104 )
105 or die "Can't connect to nginx: $!\n";
106
107 log_out($request);
108 $s->print($request);
109
110 select undef, undef, undef, $extra{sleep} if $extra{sleep};
111 return '' if $extra{aborted};
112
113 if ($extra{body}) {
114 log_out($extra{body});
115 $s->print($extra{body});
116 }
117
118 # Done writing
119 $s->shutdown(1);
120
121 local $/;
122 $reply = $s->getline();
123
124 alarm(0);
125 };
126 alarm(0);
127 if ($@) {
128 log_in("died: $@");
129 return undef;
130 }
131
132 log_in($reply);
133 return $reply;
134}
135
136sub http_daemon {
137 my $server = IO::Socket::INET->new(
138 Proto => 'tcp',
139 LocalHost => '127.0.0.1:8081',
140 Listen => 5,
141 Reuse => 1
142 )
143 or die "Can't create listening socket: $!\n";
144
145 # dumb server which is able to keep connections alive
146
147 while (my $client = $server->accept()) {
148 $client->autoflush(1);
149
150 while (1) {
151 my $headers = '';
152 my $uri = '';
153
154 while (<$client>) {
155 $headers .= $_;
156 last if (/^\x0d?\x0a?$/);
157 }
158
159 last if $headers eq '';
160
161 $uri = $1 if $headers =~ /^\S+\s+([^ ]+)\s+HTTP/i;
162
163 print { $client }
164 "HTTP/1.1 200 OK" . CRLF .
165 "Transfer-Encoding: chunked" . CRLF .
166 CRLF .
167 "a" . CRLF .
168 "finished" . CRLF .
169 CRLF . "0" . CRLF . CRLF;
170
171 last;
172 }
173
174 close $client;
175 }
176}
177
178###############################################################################