Mercurial > hg > nginx-tests
annotate uwsgi_body.t @ 1943:1786f49cca17
Tests: fixed spurious http_resolver_ipv4.t failures.
Previously, to get resolved addresses, the test relied on cached random
rotation in resolver, such that a chosen address is eventually changed,
which may not happen after several client requests. For this reason, the
test is rewritten to get all addresses by switching to the next upstream.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Mon, 22 Jan 2024 14:11:12 +0400 |
parents | b27bcded6449 |
children |
rev | line source |
---|---|
1737
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
1 #!/usr/bin/perl |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
2 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
3 # (C) Sergey Kandaurov |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
4 # (C) Nginx, Inc. |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
5 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
6 # Test for uwsgi backend with request body. |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
7 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
8 ############################################################################### |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
9 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
10 use warnings; |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
11 use strict; |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
12 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
13 use Test::More; |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
14 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
15 BEGIN { use FindBin; chdir($FindBin::Bin); } |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
16 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
17 use lib 'lib'; |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
18 use Test::Nginx; |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
19 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
20 ############################################################################### |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
21 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
22 select STDERR; $| = 1; |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
23 select STDOUT; $| = 1; |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
24 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
25 my $t = Test::Nginx->new()->has(qw/http rewrite uwsgi/) |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
26 ->has_daemon('uwsgi')->plan(5) |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
27 ->write_file_expand('nginx.conf', <<'EOF'); |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
28 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
29 %%TEST_GLOBALS%% |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
30 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
31 daemon off; |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
32 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
33 events { |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
34 } |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
35 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
36 http { |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
37 %%TEST_GLOBALS_HTTP%% |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
38 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
39 server { |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
40 listen 127.0.0.1:8080; |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
41 server_name localhost; |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
42 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
43 set $variable $content_length; |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
44 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
45 location / { |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
46 uwsgi_pass 127.0.0.1:8081; |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
47 uwsgi_param CONTENT_LENGTH $content_length if_not_empty; |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
48 } |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
49 } |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
50 } |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
51 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
52 EOF |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
53 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
54 $t->write_file('uwsgi_test_app.py', <<END); |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
55 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
56 def application(env, start_response): |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
57 start_response('200 OK', [('Content-Type','text/plain')]) |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
58 if "CONTENT_LENGTH" not in env: |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
59 return b"SEE-THIS" |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
60 cl = int(env.get('CONTENT_LENGTH')) |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
61 rb = env.get('wsgi.input').read(cl) |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
62 return b"cl=%d '%s'" % (cl, rb) |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
63 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
64 END |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
65 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
66 my $uwsgihelp = `uwsgi -h`; |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
67 my @uwsgiopts = (); |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
68 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
69 if ($uwsgihelp !~ /--wsgi-file/) { |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
70 # uwsgi has no python support, maybe plugin load is necessary |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
71 push @uwsgiopts, '--plugin', 'python'; |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
72 push @uwsgiopts, '--plugin', 'python3'; |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
73 } |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
74 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
75 open OLDERR, ">&", \*STDERR; close STDERR; |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
76 $t->run_daemon('uwsgi', '--socket', '127.0.0.1:' . port(8081), @uwsgiopts, |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
77 '--wsgi-file', $t->testdir() . '/uwsgi_test_app.py', |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
78 '--logto', $t->testdir() . '/uwsgi_log'); |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
79 open STDERR, ">&", \*OLDERR; |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
80 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
81 $t->run(); |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
82 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
83 $t->waitforsocket('127.0.0.1:' . port(8081)) |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
84 or die "Can't start uwsgi"; |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
85 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
86 ############################################################################### |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
87 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
88 like(http_get('/'), qr/SEE-THIS/, 'uwsgi no body'); |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
89 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
90 like(http_get_length('/', 'foobar'), qr/cl=6 'foobar'/, 'uwsgi body'); |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
91 like(http_get_length('/', ''), qr/cl=0 ''/, 'uwsgi empty body'); |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
92 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
93 # rewrite set is used to cache $content_length early |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
94 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
95 like(http_get_chunked('/', 'foobar'), qr/cl=6 'foobar'/, 'uwsgi chunked'); |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
96 like(http_get_chunked('/', ''), qr/cl=0 ''/, 'uwsgi empty chunked'); |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
97 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
98 ############################################################################### |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
99 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
100 sub http_get_length { |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
101 my ($url, $body) = @_; |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
102 my $length = length $body; |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
103 return http(<<EOF); |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
104 GET $url HTTP/1.1 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
105 Host: localhost |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
106 Connection: close |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
107 Content-Length: $length |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
108 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
109 $body |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
110 EOF |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
111 } |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
112 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
113 sub http_get_chunked { |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
114 my ($url, $body) = @_; |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
115 my $length = sprintf("%x", length $body); |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
116 return http(<<EOF); |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
117 GET $url HTTP/1.1 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
118 Host: localhost |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
119 Connection: close |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
120 Transfer-Encoding: chunked |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
121 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
122 $length |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
123 $body |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
124 0 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
125 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
126 EOF |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
127 } |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
128 |
9fc3b428b18a
Tests: uwsgi request body tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
129 ############################################################################### |