Mercurial > hg > nginx-tests
comparison scgi_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 | 8f280348d76f |
comparison
equal
deleted
inserted
replaced
239:5d178e27037c | 240:462d89f5732a |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Maxim Dounin | |
4 | |
5 # Test for scgi backend with chunked request body. | |
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 eval { require SCGI; }; | |
25 plan(skip_all => 'SCGI not installed') if $@; | |
26 | |
27 my $t = Test::Nginx->new()->has(qw/http scgi/)->plan(5) | |
28 ->write_file_expand('nginx.conf', <<'EOF'); | |
29 | |
30 %%TEST_GLOBALS%% | |
31 | |
32 daemon off; | |
33 | |
34 events { | |
35 } | |
36 | |
37 http { | |
38 %%TEST_GLOBALS_HTTP%% | |
39 | |
40 server { | |
41 listen 127.0.0.1:8080; | |
42 server_name localhost; | |
43 | |
44 location / { | |
45 scgi_pass 127.0.0.1:8081; | |
46 scgi_param SCGI 1; | |
47 scgi_param REQUEST_URI $request_uri; | |
48 } | |
49 } | |
50 } | |
51 | |
52 EOF | |
53 | |
54 $t->run_daemon(\&scgi_daemon); | |
55 $t->run(); | |
56 | |
57 ############################################################################### | |
58 | |
59 | |
60 like(http_get('/'), qr/X-Body: /, 'scgi no body'); | |
61 | |
62 like(http_get_length('/', ''), qr/X-Body: /, 'scgi empty body'); | |
63 like(http_get_length('/', 'foobar'), qr/X-Body: foobar/, 'scgi body'); | |
64 | |
65 TODO: { | |
66 local $TODO = 'not yet'; | |
67 | |
68 like(http(<<EOF), qr/X-Body: foobar/, 'scgi chunked'); | |
69 GET / HTTP/1.1 | |
70 Host: localhost | |
71 Connection: close | |
72 Transfer-Encoding: chunked | |
73 | |
74 6 | |
75 foobar | |
76 0 | |
77 | |
78 EOF | |
79 | |
80 like(http(<<EOF), qr/X-Body: /, 'scgi empty chunked'); | |
81 GET / HTTP/1.1 | |
82 Host: localhost | |
83 Connection: close | |
84 Transfer-Encoding: chunked | |
85 | |
86 0 | |
87 | |
88 EOF | |
89 | |
90 } | |
91 | |
92 ############################################################################### | |
93 | |
94 sub http_get_length { | |
95 my ($url, $body) = @_; | |
96 my $length = length $body; | |
97 return http(<<EOF); | |
98 GET $url HTTP/1.1 | |
99 Host: localhost | |
100 Connection: close | |
101 Content-Length: $length | |
102 | |
103 $body | |
104 EOF | |
105 } | |
106 | |
107 ############################################################################### | |
108 | |
109 sub scgi_daemon { | |
110 my $server = IO::Socket::INET->new( | |
111 Proto => 'tcp', | |
112 LocalHost => '127.0.0.1:8081', | |
113 Listen => 5, | |
114 Reuse => 1 | |
115 ) | |
116 or die "Can't create listening socket: $!\n"; | |
117 | |
118 my $scgi = SCGI->new($server, blocking => 1); | |
119 my $body; | |
120 | |
121 while (my $request = $scgi->accept()) { | |
122 $request->read_env(); | |
123 read($request->connection, $body, | |
124 $request->env->{CONTENT_LENGTH}); | |
125 | |
126 $request->connection()->print(<<EOF); | |
127 Location: http://127.0.0.1:8080/redirect | |
128 Content-Type: text/html | |
129 X-Body: $body | |
130 | |
131 SEE-THIS | |
132 EOF | |
133 } | |
134 } | |
135 | |
136 ############################################################################### |