comparison body.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 6a0d934950bc
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.
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(10);
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 unlike(http_get('/'), qr/X-Body:/ms, 'no body');
86
87 like(http_get_body('/', '0123456789'),
88 qr/X-Body: 0123456789\x0d?$/ms, 'body');
89
90 like(http_get_body('/', '0123456789' x 128),
91 qr/X-Body: (0123456789){128}\x0d?$/ms, 'body in two buffers');
92
93 like(http_get_body('/', '0123456789' x 512),
94 qr/X-Body-File/ms, 'body in file');
95
96 like(read_body_file(http_get_body('/b', '0123456789' x 512)),
97 qr/^(0123456789){512}$/s, 'body in file only');
98
99 like(http_get_body('/single', '0123456789' x 128),
100 qr/X-Body: (0123456789){128}\x0d?$/ms, 'body in single buffer');
101
102 # pipelined requests
103
104 like(http_get_body('/', '0123456789', '0123456789' x 128, '0123456789' x 512,
105 'foobar'), qr/X-Body: foobar\x0d?$/ms, 'body pipelined');
106 like(http_get_body('/', '0123456789' x 128, '0123456789' x 512, '0123456789',
107 'foobar'), qr/X-Body: foobar\x0d?$/ms, 'body pipelined 2');
108
109 like(http_get_body('/discard', '0123456789', '0123456789' x 128,
110 '0123456789' x 512, 'foobar'), qr/(TEST.*){4}/ms,
111 'body discard');
112 like(http_get_body('/discard', '0123456789' x 128, '0123456789' x 512,
113 '0123456789', 'foobar'), qr/(TEST.*){4}/ms,
114 'body discard 2');
115
116 ###############################################################################
117
118 sub read_body_file {
119 my ($r) = @_;
120 return '' unless $r =~ m/X-Body-File: (.*)/;
121 open FILE, $1
122 or return "$!";
123 local $/;
124 my $content = <FILE>;
125 close FILE;
126 return $content;
127 }
128
129 sub http_get_body {
130 my $uri = shift;
131 my $last = pop;
132 return http( join '', (map {
133 my $body = $_;
134 "GET $uri HTTP/1.1" . CRLF
135 . "Host: localhost" . CRLF
136 . "Content-Length: " . (length $body) . CRLF . CRLF
137 . $body
138 } @_),
139 "GET $uri HTTP/1.1" . CRLF
140 . "Host: localhost" . CRLF
141 . "Connection: close" . CRLF
142 . "Content-Length: " . (length $last) . CRLF . CRLF
143 . $last
144 );
145 }
146
147 ###############################################################################