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