annotate http_keepalive.t @ 1023:8533222fcfc1

Tests: http keepalive tests. Basic tests for keepalive_requests, keepalive_disable and keepalive_timeout directives.
author Andrey Zelenkov <zelenkov@nginx.com>
date Fri, 02 Sep 2016 14:42:00 +0300
parents
children 2b8523bd4988
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1023
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
1 #!/usr/bin/perl
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
2
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
3 # (C) Andrey Zelenkov
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
4 # (C) Nginx, Inc.
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
5
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
6 # Tests for http keepalive directives.
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
7
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
8 ###############################################################################
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
9
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
10 use warnings;
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
11 use strict;
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
12
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
13 use Test::More;
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
14
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
15 use IO::Select;
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
16
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
17 BEGIN { use FindBin; chdir($FindBin::Bin); }
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
18
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
19 use lib 'lib';
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
20 use Test::Nginx;
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
21
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
22 ###############################################################################
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
23
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
24 select STDERR; $| = 1;
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
25 select STDOUT; $| = 1;
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
26
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
27 my $t = Test::Nginx->new()->has(qw/http/)->plan(13)
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
28 ->write_file_expand('nginx.conf', <<'EOF');
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
29
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
30 %%TEST_GLOBALS%%
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
31
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
32 daemon off;
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
33
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
34 events {
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
35 }
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
36
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
37 http {
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
38 %%TEST_GLOBALS_HTTP%%
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
39
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
40 server {
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
41 listen 127.0.0.1:8080;
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
42 server_name localhost;
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
43
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
44 keepalive_requests 2;
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
45 keepalive_timeout 1 9;
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
46
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
47 location / { }
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
48 location /r {
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
49 keepalive_requests 4;
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
50 }
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
51
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
52 location /safari {
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
53 keepalive_disable safari;
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
54 }
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
55
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
56 location /none {
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
57 keepalive_disable none;
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
58 }
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
59
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
60 location /zero {
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
61 keepalive_timeout 0;
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
62 }
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
63 }
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
64 }
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
65
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
66 EOF
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
67
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
68 $t->write_file('index.html', '');
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
69 $t->write_file('r', '');
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
70 $t->write_file('safari', '');
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
71 $t->write_file('none', '');
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
72 $t->write_file('zero', '');
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
73 $t->run();
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
74
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
75 ###############################################################################
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
76
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
77 # keepalive_requests
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
78
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
79 like(http_keepalive('/'), qr/Connection: keep-alive/, 'keepalive request');
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
80 is(count_keepalive(http_keepalive('/', req => 2)), 1, 'keepalive limit');
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
81 is(count_keepalive(http_keepalive('/r', req => 3)), 3, 'keepalive merge');
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
82 is(count_keepalive(http_keepalive('/r', req => 5)), 3, 'keepalive merge limit');
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
83
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
84 # keepalive_disable
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
85
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
86 like(http_keepalive('/', method => 'POST', ua => "MSIE 5.0"),
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
87 qr/Connection: close/, 'keepalive disable msie6');
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
88 like(http_keepalive('/', ua => "MSIE 5.0"), qr/Connection: keep-alive/,
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
89 'keepalive disable msie6 GET');
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
90 like(http_keepalive('/', method => 'POST', ua => "MSIE 7.0"),
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
91 qr/Connection: keep-alive/, 'keepalive disable msie6 modern');
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
92 like(http_keepalive('/', ua => "Mac OS X Safari/7534.48.3"),
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
93 qr/Connection: keep-alive/, 'keepalive disable msie6 safari');
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
94 like(http_keepalive('/safari', ua => "Mac OS X Safari/7534.48.3"),
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
95 qr/Connection: close/, 'keepalive disable safari');
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
96 like(http_keepalive('/none', method => 'POST', ua => "MSIE 5.0"),
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
97 qr/Connection: keep-alive/, 'keepalive disable none');
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
98
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
99 # keepalive_timeout
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
100
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
101 my $r = http_keepalive('/', req => 2, sleep => 2.1);
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
102 is(count_keepalive($r), 1, 'keepalive timeout request');
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
103 like($r, qr/Keep-Alive: timeout=9/, 'keepalive timeout header');
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
104
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
105 like(http_keepalive('/zero'), qr/Connection: close/, 'keepalive timeout 0');
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
106
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
107 ###############################################################################
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
108
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
109 sub http_keepalive {
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
110 my ($url, %opts) = @_;
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
111 my $data = '';
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
112
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
113 $opts{ua} = $opts{ua} || '';
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
114 $opts{req} = $opts{req} || 1;
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
115 $opts{sleep} = $opts{sleep} || 0;
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
116 $opts{method} = $opts{method} || 'GET';
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
117
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
118 my $s = http('', start => 1);
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
119
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
120 for my $i (1 .. $opts{req}) {
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
121
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
122 my $sleep = ($i == 1 ? $opts{sleep} : 0);
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
123
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
124 http(<<EOF, socket => $s, start => 1, sleep => $sleep);
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
125 $opts{method} $url HTTP/1.1
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
126 Host: localhost
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
127 User-Agent: $opts{ua}
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
128
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
129 EOF
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
130
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
131 while (IO::Select->new($s)->can_read(3)) {
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
132 sysread($s, my $buffer, 4096);
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
133 $data .= $buffer;
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
134 last if $data =~ /^\x0d\x0a/ms;
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
135 }
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
136
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
137 }
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
138
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
139 return $data;
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
140 }
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
141
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
142 sub count_keepalive {
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
143 my ($str) = @_;
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
144 return $str =~ s/Connection: keep-alive//g;
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
145 }
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
146
8533222fcfc1 Tests: http keepalive tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
diff changeset
147 ###############################################################################