Mercurial > hg > nginx-tests
comparison limit_req.t @ 148:b714d6df958c
Tests: rename some tests for better sorting.
Use underscore instead of dash. Addtionally, rename some tests to better
match "module" + "details" scheme used: use "http_" prefix for http core
module tests, use "mail_" prefix for mail module tests.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Fri, 04 Mar 2011 16:07:15 +0300 |
parents | limit-req.t@59021f2b2fd3 |
children | c0ae29632905 |
comparison
equal
deleted
inserted
replaced
147:fd865ada95c8 | 148:b714d6df958c |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Maxim Dounin | |
4 | |
5 # Tests for nginx limit_req module. | |
6 | |
7 ############################################################################### | |
8 | |
9 use warnings; | |
10 use strict; | |
11 | |
12 use Test::More; | |
13 | |
14 BEGIN { use FindBin; chdir($FindBin::Bin); } | |
15 | |
16 use lib 'lib'; | |
17 use Test::Nginx; | |
18 | |
19 ############################################################################### | |
20 | |
21 select STDERR; $| = 1; | |
22 select STDOUT; $| = 1; | |
23 | |
24 my $t = Test::Nginx->new()->has(qw/http limit_req/)->plan(5); | |
25 | |
26 $t->write_file_expand('nginx.conf', <<'EOF'); | |
27 | |
28 %%TEST_GLOBALS%% | |
29 | |
30 master_process off; | |
31 daemon off; | |
32 | |
33 events { | |
34 } | |
35 | |
36 http { | |
37 %%TEST_GLOBALS_HTTP%% | |
38 | |
39 limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; | |
40 limit_req_zone $binary_remote_addr zone=long:10m rate=1r/s; | |
41 limit_req_zone $binary_remote_addr zone=fast:10m rate=1000r/s; | |
42 | |
43 server { | |
44 listen 127.0.0.1:8080; | |
45 server_name localhost; | |
46 location / { | |
47 limit_req zone=one burst=1 nodelay; | |
48 } | |
49 location /long { | |
50 limit_req zone=long burst=5; | |
51 } | |
52 location /fast { | |
53 limit_req zone=fast burst=1; | |
54 } | |
55 } | |
56 } | |
57 | |
58 EOF | |
59 | |
60 $t->write_file('test1.html', 'XtestX'); | |
61 $t->write_file('long.html', "1234567890\n" x (1 << 16)); | |
62 $t->write_file('fast.html', 'XtestX'); | |
63 $t->run(); | |
64 | |
65 ############################################################################### | |
66 | |
67 like(http_get('/test1.html'), qr/^HTTP\/1.. 200 /m, 'request'); | |
68 http_get('/test1.html'); | |
69 like(http_get('/test1.html'), qr/^HTTP\/1.. 503 /m, 'request rejected'); | |
70 http_get('/test1.html'); | |
71 http_get('/test1.html'); | |
72 | |
73 # Second request will be delayed by limit_req, make sure it isn't truncated. | |
74 # The bug only manifests itself if buffer will be filled, so sleep for a while | |
75 # before reading response. | |
76 | |
77 my $l1 = length(http_get('/long.html')); | |
78 my $l2 = length(http_get('/long.html', sleep => 1.1)); | |
79 is($l2, $l1, 'delayed big request not truncated'); | |
80 | |
81 # make sure rejected requests are not counted, and access is again allowed | |
82 # after 1/rate seconds | |
83 | |
84 like(http_get('/test1.html'), qr/^HTTP\/1.. 200 /m, 'rejects not counted'); | |
85 | |
86 # make sure negative excess values are handled properly | |
87 | |
88 http_get('/fast.html'); | |
89 select undef, undef, undef, 0.1; | |
90 like(http_get('/fast.html'), qr/^HTTP\/1.. 200 /m, 'negative excess'); | |
91 | |
92 ############################################################################### |