comparison body_chunked.t @ 240:462d89f5732a

Tests: request body and chunked transfer encoding tests.
author Maxim Dounin <mdounin@mdounin.ru>
date Fri, 09 Nov 2012 07:46:37 +0400
parents
children 8f280348d76f
comparison
equal deleted inserted replaced
239:5d178e27037c 240:462d89f5732a
1 #!/usr/bin/perl
2
3 # (C) Maxim Dounin
4
5 # Tests for nginx request body reading, with chunked transfer-coding.
6
7 ###############################################################################
8
9 use warnings;
10 use strict;
11
12 use Test::More;
13 use Socket qw/ CRLF /;
14
15 BEGIN { use FindBin; chdir($FindBin::Bin); }
16
17 use lib 'lib';
18 use Test::Nginx;
19
20 ###############################################################################
21
22 select STDERR; $| = 1;
23 select STDOUT; $| = 1;
24
25 my $t = Test::Nginx->new()->has(qw/http proxy rewrite/)->plan(9);
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 client_header_buffer_size 1k;
44
45 location / {
46 client_body_buffer_size 2k;
47 add_header X-Body "$request_body";
48 add_header X-Body-File "$request_body_file";
49 proxy_pass http://127.0.0.1:8081;
50 }
51 location /b {
52 client_body_buffer_size 2k;
53 client_body_in_file_only on;
54 add_header X-Body "$request_body";
55 add_header X-Body-File "$request_body_file";
56 proxy_pass http://127.0.0.1:8081;
57 }
58 location /single {
59 client_body_in_single_buffer on;
60 add_header X-Body "$request_body";
61 add_header X-Body-File "$request_body_file";
62 proxy_pass http://127.0.0.1:8081;
63 }
64 location /discard {
65 return 200 "TEST\n";
66 }
67 }
68
69 server {
70 listen 127.0.0.1:8081;
71 server_name localhost;
72
73 location / {
74 return 200 "TEST\n";
75 }
76 }
77 }
78
79 EOF
80
81 $t->run();
82
83 ###############################################################################
84
85 TODO: {
86 local $TODO = 'not yet';
87
88 like(http_get_body('/', '0123456789'),
89 qr/X-Body: 0123456789\x0d?$/ms, 'body');
90
91 like(http_get_body('/', '0123456789' x 128),
92 qr/X-Body: (0123456789){128}\x0d?$/ms, 'body in two buffers');
93
94 like(http_get_body('/', '0123456789' x 512),
95 qr/X-Body-File/ms, 'body in file');
96
97 like(read_body_file(http_get_body('/b', '0123456789' x 512)),
98 qr/^(0123456789){512}$/s, 'body in file only');
99
100 like(http_get_body('/single', '0123456789' x 128),
101 qr/X-Body: (0123456789){128}\x0d?$/ms, 'body in single buffer');
102
103 # pipelined requests
104
105 like(http_get_body('/', '0123456789', '0123456789' x 128, '0123456789' x 512,
106 'foobar'), qr/X-Body: foobar\x0d?$/ms, 'chunked body pipelined');
107 like(http_get_body('/', '0123456789' x 128, '0123456789' x 512, '0123456789',
108 'foobar'), qr/X-Body: foobar\x0d?$/ms, 'chunked body pipelined 2');
109
110 like(http_get_body('/discard', '0123456789', '0123456789' x 128,
111 '0123456789' x 512, 'foobar'), qr/(TEST.*){4}/ms,
112 'chunked body discard');
113 like(http_get_body('/discard', '0123456789' x 128, '0123456789' x 512,
114 '0123456789', 'foobar'), qr/(TEST.*){4}/ms,
115 'chunked body discard 2');
116
117 }
118
119 ###############################################################################
120
121 sub read_body_file {
122 my ($r) = @_;
123 return '' unless $r =~ m/X-Body-File: (.*)/;
124 open FILE, $1
125 or return "$!";
126 local $/;
127 my $content = <FILE>;
128 close FILE;
129 return $content;
130 }
131
132 sub http_get_body {
133 my $uri = shift;
134 my $last = pop;
135 return http( join '', (map {
136 my $body = $_;
137 "GET $uri HTTP/1.1" . CRLF
138 . "Host: localhost" . CRLF
139 . "Transfer-Encoding: chunked" . CRLF . CRLF
140 . sprintf("%x", length $body) . CRLF
141 . $body . CRLF
142 . "0" . CRLF . CRLF
143 } @_),
144 "GET $uri HTTP/1.1" . CRLF
145 . "Host: localhost" . CRLF
146 . "Connection: close" . CRLF
147 . "Transfer-Encoding: chunked" . CRLF . CRLF
148 . sprintf("%x", length $last) . CRLF
149 . $last . CRLF
150 . "0" . CRLF . CRLF
151 );
152 }
153
154 ###############################################################################