comparison uwsgi_body.t @ 1737:9fc3b428b18a

Tests: uwsgi request body tests.
author Sergey Kandaurov <pluknet@nginx.com>
date Thu, 07 Oct 2021 20:29:02 +0300
parents
children b27bcded6449
comparison
equal deleted inserted replaced
1736:f7a8997c46c7 1737:9fc3b428b18a
1 #!/usr/bin/perl
2
3 # (C) Sergey Kandaurov
4 # (C) Nginx, Inc.
5
6 # Test for uwsgi backend with request body.
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;
19
20 ###############################################################################
21
22 select STDERR; $| = 1;
23 select STDOUT; $| = 1;
24
25 my $t = Test::Nginx->new()->has(qw/http rewrite uwsgi/)
26 ->has_daemon('uwsgi')->plan(5)
27 ->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 set $variable $content_length;
44
45 location / {
46 uwsgi_pass 127.0.0.1:8081;
47 uwsgi_param CONTENT_LENGTH $content_length if_not_empty;
48 }
49 }
50 }
51
52 EOF
53
54 $t->write_file('uwsgi_test_app.py', <<END);
55
56 def application(env, start_response):
57 start_response('200 OK', [('Content-Type','text/plain')])
58 if "CONTENT_LENGTH" not in env:
59 return b"SEE-THIS"
60 cl = int(env.get('CONTENT_LENGTH'))
61 rb = env.get('wsgi.input').read(cl)
62 return b"cl=%d '%s'" % (cl, rb)
63
64 END
65
66 my $uwsgihelp = `uwsgi -h`;
67 my @uwsgiopts = ();
68
69 if ($uwsgihelp !~ /--wsgi-file/) {
70 # uwsgi has no python support, maybe plugin load is necessary
71 push @uwsgiopts, '--plugin', 'python';
72 push @uwsgiopts, '--plugin', 'python3';
73 }
74
75 open OLDERR, ">&", \*STDERR; close STDERR;
76 $t->run_daemon('uwsgi', '--socket', '127.0.0.1:' . port(8081), @uwsgiopts,
77 '--wsgi-file', $t->testdir() . '/uwsgi_test_app.py',
78 '--logto', $t->testdir() . '/uwsgi_log');
79 open STDERR, ">&", \*OLDERR;
80
81 $t->run();
82
83 $t->waitforsocket('127.0.0.1:' . port(8081))
84 or die "Can't start uwsgi";
85
86 ###############################################################################
87
88 like(http_get('/'), qr/SEE-THIS/, 'uwsgi no body');
89
90 like(http_get_length('/', 'foobar'), qr/cl=6 'foobar'/, 'uwsgi body');
91 like(http_get_length('/', ''), qr/cl=0 ''/, 'uwsgi empty body');
92
93 # rewrite set is used to cache $content_length early
94
95 TODO: {
96 local $TODO = 'not yet' unless $t->has_version('1.21.4');
97
98 like(http_get_chunked('/', 'foobar'), qr/cl=6 'foobar'/, 'uwsgi chunked');
99 like(http_get_chunked('/', ''), qr/cl=0 ''/, 'uwsgi empty chunked');
100
101 }
102
103 ###############################################################################
104
105 sub http_get_length {
106 my ($url, $body) = @_;
107 my $length = length $body;
108 return http(<<EOF);
109 GET $url HTTP/1.1
110 Host: localhost
111 Connection: close
112 Content-Length: $length
113
114 $body
115 EOF
116 }
117
118 sub http_get_chunked {
119 my ($url, $body) = @_;
120 my $length = sprintf("%x", length $body);
121 return http(<<EOF);
122 GET $url HTTP/1.1
123 Host: localhost
124 Connection: close
125 Transfer-Encoding: chunked
126
127 $length
128 $body
129 0
130
131 EOF
132 }
133
134 ###############################################################################