Mercurial > hg > ngx_http_delay_body_filter_module
annotate t/delay_body.t @ 1:7c2d64d9c656
Working delay body and tests.
Depends on rb->filter_need_buffering and rb->buffered, as well as
corresponding changes in the request body reading code to work.
Might not be the best solution, as current body reading code relies
on rb->buffered to be properly set at various stages. Notably,
rb->buffered must be cleared when calling the next filter, since
the save body filter relies on it. Possible future improvements:
implement last buffer checking in the save body filter instead of
checking for (rb->rest == 0 && !rb->buffered).
The code uses its own event to implement delay timer. To remove the timer
in case of abnormal request termination a cleanup handler is added. While
in theory it is possible to use a timer on c->read instead, this implies
additional changes to the request body reading code. Custom event was chosen
to reduce complexity of changes needed.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Fri, 20 Aug 2021 22:35:05 +0300 |
parents | a386f95c5ae9 |
children |
rev | line source |
---|---|
0 | 1 #!/usr/bin/perl |
2 | |
3 # (C) Maxim Dounin | |
4 | |
5 # Tests for delay body filter module. | |
6 | |
7 ############################################################################### | |
8 | |
9 use warnings; | |
10 use strict; | |
11 | |
12 use Test::More; | |
13 use Test::Nginx; | |
14 | |
15 use Socket qw/ CRLF /; | |
16 | |
17 ############################################################################### | |
18 | |
19 select STDERR; $| = 1; | |
20 select STDOUT; $| = 1; | |
21 | |
22 my $t = Test::Nginx->new()->has(qw/http proxy rewrite/) | |
23 ->write_file_expand('nginx.conf', <<'EOF'); | |
24 | |
25 %%TEST_GLOBALS%% | |
26 | |
27 daemon off; | |
28 | |
29 events { | |
30 } | |
31 | |
32 http { | |
33 %%TEST_GLOBALS_HTTP%% | |
34 | |
35 server { | |
36 listen 127.0.0.1:8080; | |
37 server_name localhost; | |
1
7c2d64d9c656
Working delay body and tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
38 |
0 | 39 location / { |
40 delay_body 1s; | |
41 add_header X-Time $request_time; | |
42 proxy_pass http://127.0.0.1:8080/empty; | |
43 } | |
1
7c2d64d9c656
Working delay body and tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
44 |
7c2d64d9c656
Working delay body and tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
45 location /no { |
7c2d64d9c656
Working delay body and tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
46 delay_body 0s; |
7c2d64d9c656
Working delay body and tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
47 add_header X-Time $request_time; |
7c2d64d9c656
Working delay body and tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
48 proxy_pass http://127.0.0.1:8080/empty; |
7c2d64d9c656
Working delay body and tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
49 } |
7c2d64d9c656
Working delay body and tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
50 |
0 | 51 location /empty { |
52 return 200 "test response body\n"; | |
53 } | |
54 } | |
55 } | |
56 | |
57 EOF | |
58 | |
59 $t->try_run('no delay_body')->plan(4); | |
60 | |
61 ############################################################################### | |
62 | |
63 like(get_body('/', '123456'), qr/200 OK.*X-Time: 1/ms, 'delay'); | |
1
7c2d64d9c656
Working delay body and tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
64 like(get_body('/no', '123456'), qr/200 OK.*X-Time: 0/ms, 'no delay'); |
0 | 65 |
66 # pipelining | |
67 | |
68 like(get_body('/', '123456', '12345X'), | |
69 qr/200 OK.*X-Time: 1.*200 OK.*X-Time: 1/ms, | |
70 'pipelining delay'); | |
71 | |
72 # pipelining with chunked | |
73 | |
74 like(get_chunked('/', '123456', '12345X'), | |
75 qr/200 OK.*X-Time: 1.*200 OK.*X-Time: 1/ms, | |
76 'pipelining chunked delay'); | |
77 | |
78 ############################################################################### | |
79 | |
80 sub get_body { | |
81 my $uri = shift; | |
82 my $last = pop; | |
83 return http( join '', (map { | |
84 my $body = $_; | |
85 "GET $uri HTTP/1.1" . CRLF | |
86 . "Host: localhost" . CRLF | |
87 . "Content-Length: " . (length $body) . CRLF . CRLF | |
88 . $body | |
89 } @_), | |
90 "GET $uri HTTP/1.1" . CRLF | |
91 . "Host: localhost" . CRLF | |
92 . "Connection: close" . CRLF | |
93 . "Content-Length: " . (length $last) . CRLF . CRLF | |
94 . $last | |
95 ); | |
96 } | |
97 | |
98 sub get_chunked { | |
99 my $uri = shift; | |
100 my $last = pop; | |
101 return http( join '', (map { | |
102 my $body = $_; | |
103 "GET $uri HTTP/1.1" . CRLF | |
104 . "Host: localhost" . CRLF | |
105 . "Transfer-Encoding: chunked" . CRLF . CRLF | |
106 . sprintf("%x", length $body) . CRLF | |
107 . $body . CRLF | |
108 . "0" . CRLF . CRLF | |
109 } @_), | |
110 "GET $uri HTTP/1.1" . CRLF | |
111 . "Host: localhost" . CRLF | |
112 . "Connection: close" . CRLF | |
113 . "Transfer-Encoding: chunked" . CRLF . CRLF | |
114 . sprintf("%x", length $last) . CRLF | |
115 . $last . CRLF | |
116 . "0" . CRLF . CRLF | |
117 ); | |
118 } | |
119 | |
120 ############################################################################### |