Ticket #541: proxy_no_proxy_temp2.t

File proxy_no_proxy_temp2.t, 2.8 KB (added by Markus Linnala, 12 years ago)
Line 
1#!/usr/bin/perl
2
3# (C) Maxim Dounin
4
5# Tests for http proxy and prematurely closed connections. Incomplete
6# responses shouldn't loose information about their incompleteness.
7
8# In particular, incomplete responses:
9#
10# - shouldn't be cached
11#
12# - if a response is sent using chunked transfer encoding,
13# final chunk shouldn't be sent
14
15###############################################################################
16
17use warnings;
18use strict;
19
20use Test::More;
21
22use Socket qw/ CRLF /;
23
24BEGIN { use FindBin; chdir($FindBin::Bin); }
25
26use lib 'lib';
27use Test::Nginx;
28
29###############################################################################
30
31select STDERR; $| = 1;
32select STDOUT; $| = 1;
33
34plan(skip_all => 'win32') if $^O eq 'MSWin32';
35
36my $t = Test::Nginx->new()->has(qw/http proxy cache/)->plan(1)
37 ->write_file_expand('nginx.conf', <<'EOF');
38
39%%TEST_GLOBALS%%
40
41daemon off;
42
43events {
44}
45
46http {
47 %%TEST_GLOBALS_HTTP%%
48
49 proxy_cache_path %%TESTDIR%%/cache levels=1:2
50 keys_zone=one:1m;
51
52 server {
53 listen 127.0.0.1:8080;
54 server_name localhost;
55
56 location / {
57 proxy_pass http://127.0.0.1:8081;
58 proxy_cache one;
59 proxy_buffer_size 1k;
60 proxy_buffers 4 1k;
61 }
62 }
63}
64
65EOF
66
67$t->run_daemon(\&http_daemon);
68$t->run()->waitforsocket('127.0.0.1:8081');
69
70###############################################################################
71
72TODO: {
73local $TODO = 'not yet' unless $t->has_version('1.5.3');
74
75# if disk buffering fails for some reason, there should be
76# no final chunk
77
78chmod(0000, $t->testdir() . '/proxy_temp');
79like(http_get_11('/proxy/big.html', sleep => 0.5),
80 qr/X(?!.*\x0d\x0a?0\x0d\x0a?)/s, 'no proxy temp');
81
82}
83
84###############################################################################
85
86sub http_get_11 {
87 my ($uri, %extra) = @_;
88
89 return http(
90 "GET $uri HTTP/1.1" . CRLF .
91 "Connection: close" . CRLF .
92 "Host: localhost" . CRLF . CRLF,
93 %extra
94 );
95}
96
97###############################################################################
98
99sub http_daemon {
100 my $server = IO::Socket::INET->new(
101 Proto => 'tcp',
102 LocalAddr => '127.0.0.1:8081',
103 Listen => 5,
104 Reuse => 1
105 )
106 or die "Can't create listening socket: $!\n";
107
108 local $SIG{PIPE} = 'IGNORE';
109
110 while (my $client = $server->accept()) {
111 $client->autoflush(1);
112
113 my $headers = '';
114 my $uri = '';
115
116 while (<$client>) {
117 $headers .= $_;
118 last if (/^\x0d?\x0a?$/);
119 }
120
121 $uri = $1 if $headers =~ /^\S+\s+([^ ]+)\s+HTTP/i;
122
123 print $client
124 "HTTP/1.1 200 OK" . CRLF .
125 "Content-Length: 1000010" . CRLF .
126 "Cache-Control: max-age=300" . CRLF .
127 "Connection: close" . CRLF .
128 CRLF .
129 ("X" x (1000*1000)) . "finished" . CRLF;
130 }
131}
132
133###############################################################################