|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +# (C) Sergey Kandaurov |
| 4 | +# (C) Nginx, Inc. |
| 5 | + |
| 6 | +# Tests for the proxy_limit_rate directive, variables support. |
| 7 | + |
| 8 | +############################################################################### |
| 9 | + |
| 10 | +use warnings; |
| 11 | +use strict; |
| 12 | + |
| 13 | +use Test::More; |
| 14 | + |
| 15 | +BEGIN { use FindBin; chdir($FindBin::Bin); } |
| 16 | + |
| 17 | +use lib 'lib'; |
| 18 | +use Test::Nginx qw/ :DEFAULT http_content /; |
| 19 | + |
| 20 | +############################################################################### |
| 21 | + |
| 22 | +select STDERR; $| = 1; |
| 23 | +select STDOUT; $| = 1; |
| 24 | + |
| 25 | +my $t = Test::Nginx->new()->has(qw/http proxy/); |
| 26 | + |
| 27 | +$t->write_file_expand('nginx.conf', <<'EOF'); |
| 28 | +
|
| 29 | +%%TEST_GLOBALS%% |
| 30 | +
|
| 31 | +daemon off; |
| 32 | +
|
| 33 | +events { |
| 34 | +} |
| 35 | +
|
| 36 | +http { |
| 37 | + %%TEST_GLOBALS_HTTP%% |
| 38 | +
|
| 39 | + server { |
| 40 | + listen 127.0.0.1:8080; |
| 41 | + server_name localhost; |
| 42 | +
|
| 43 | + proxy_limit_rate $upstream_http_x_rate; |
| 44 | + proxy_buffer_size 4k; |
| 45 | +
|
| 46 | + location / { |
| 47 | + proxy_pass http://127.0.0.1:8080/data; |
| 48 | + add_header X-Msec $msec; |
| 49 | + add_trailer X-Msec $msec; |
| 50 | + } |
| 51 | +
|
| 52 | + location /data { |
| 53 | + add_header X-Rate $arg_e; |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | +
|
| 58 | +EOF |
| 59 | + |
| 60 | +$t->write_file('data', 'X' x 40000); |
| 61 | +$t->try_run('no proxy_limit_rate variables')->plan(4); |
| 62 | + |
| 63 | +############################################################################### |
| 64 | + |
| 65 | +my ($body, $t1, $t2) = get('/?e=20000'); |
| 66 | + |
| 67 | +cmp_ok($t2 - $t1, '>=', 1, 'proxy_limit_rate'); |
| 68 | +is($body, 'X' x 40000, 'response body'); |
| 69 | + |
| 70 | +# unlimited |
| 71 | + |
| 72 | +($body, $t1, $t2) = get('/?e=0'); |
| 73 | + |
| 74 | +is($t2 - $t1, 0, 'proxy_limit_rate unlimited'); |
| 75 | +is($body, 'X' x 40000, 'response body unlimited'); |
| 76 | + |
| 77 | +############################################################################### |
| 78 | + |
| 79 | +sub get { |
| 80 | + my ($uri) = @_; |
| 81 | + my $r = http(<<EOF); |
| 82 | +GET $uri HTTP/1.1 |
| 83 | +Host: localhost |
| 84 | +Connection: close |
| 85 | +
|
| 86 | +EOF |
| 87 | + |
| 88 | + http_content($r), $r =~ /X-Msec: (\d+)/g; |
| 89 | +} |
| 90 | + |
| 91 | +############################################################################### |
0 commit comments