comparison uwsgi_ssl.t @ 1575:577e72267fec

Tests: uwsgi tests with SSL.
author Maxim Dounin <mdounin@mdounin.ru>
date Tue, 16 Jun 2020 21:25:37 +0300
parents
children f069dd7ba5a7
comparison
equal deleted inserted replaced
1574:42e37e0434d5 1575:577e72267fec
1 #!/usr/bin/perl
2
3 # (C) Maxim Dounin
4 # (C) Nginx, Inc.
5
6 # Test for uwsgi backend with SSL.
7
8 ###############################################################################
9
10 use warnings;
11 use strict;
12
13 use Test::More;
14 use Socket qw/ CRLF /;
15
16 BEGIN { use FindBin; chdir($FindBin::Bin); }
17
18 use lib 'lib';
19 use Test::Nginx;
20
21 ###############################################################################
22
23 select STDERR; $| = 1;
24 select STDOUT; $| = 1;
25
26 my $t = Test::Nginx->new()->has(qw/http uwsgi http_ssl/)
27 ->has_daemon('uwsgi')->has_daemon('openssl')->plan(7)
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 upstream u {
41 server 127.0.0.1:8081;
42 }
43
44 server {
45 listen 127.0.0.1:8080;
46 server_name localhost;
47
48 location / {
49 uwsgi_pass suwsgi://127.0.0.1:8081;
50 uwsgi_param SERVER_PROTOCOL $server_protocol;
51 uwsgi_param HTTP_X_BLAH "blah";
52 uwsgi_pass_request_body off;
53 }
54
55 location /var {
56 uwsgi_pass suwsgi://$arg_b;
57 uwsgi_param SERVER_PROTOCOL $server_protocol;
58 }
59 }
60 }
61
62 EOF
63
64 $t->write_file('openssl.conf', <<EOF);
65 [ req ]
66 default_bits = 2048
67 encrypt_key = no
68 distinguished_name = req_distinguished_name
69 [ req_distinguished_name ]
70 EOF
71
72 my $d = $t->testdir();
73 my $crt = "$d/uwsgi.crt";
74 my $key = "$d/uwsgi.key";
75
76 foreach my $name ('uwsgi') {
77 system('openssl req -x509 -new '
78 . "-config $d/openssl.conf -subj /CN=$name/ "
79 . "-out $d/$name.crt -keyout $d/$name.key "
80 . ">>$d/openssl.out 2>&1") == 0
81 or die "Can't create certificate for $name: $!\n";
82 }
83
84 $t->write_file('uwsgi_test_app.py', <<END);
85
86 def application(env, start_response):
87 start_response('200 OK', [('Content-Type','text/plain')])
88 return b"SEE-THIS"
89
90 END
91
92 my $uwsgihelp = `uwsgi -h`;
93 my @uwsgiopts = ();
94
95 if ($uwsgihelp !~ /--wsgi-file/) {
96 # uwsgi has no python support, maybe plugin load is necessary
97 push @uwsgiopts, '--plugin', 'python';
98 }
99
100 $t->run_daemon('uwsgi', @uwsgiopts,
101 '--ssl-socket', '127.0.0.1:' . port(8081) . ",$crt,$key",
102 '--wsgi-file', $d . '/uwsgi_test_app.py',
103 '--logto', $d . '/uwsgi_log');
104
105 $t->run();
106
107 $t->waitforsocket('127.0.0.1:' . port(8081))
108 or die "Can't start uwsgi";
109
110 ###############################################################################
111
112 TODO: {
113 todo_skip 'not yet', 7 unless $t->has_version('1.19.1');
114
115 like(http_get('/'), qr/SEE-THIS/, 'uwsgi request');
116 like(http_head('/head'), qr/200 OK(?!.*SEE-THIS)/s, 'no data in HEAD');
117
118 like(http_get_headers('/headers'), qr/SEE-THIS/,
119 'uwsgi request with many ignored headers');
120
121 like(http_get('/var?b=127.0.0.1:' . port(8081)), qr/SEE-THIS/,
122 'uwsgi with variables');
123 like(http_get('/var?b=u'), qr/SEE-THIS/, 'uwsgi with variables to upstream');
124
125 like(http_post('/'), qr/SEE-THIS/, 'uwsgi post');
126 like(http_post_big('/'), qr/SEE-THIS/, 'uwsgi big post');
127
128 }
129
130 ###############################################################################
131
132 sub http_get_headers {
133 my ($url, %extra) = @_;
134 return http(<<EOF, %extra);
135 GET $url HTTP/1.0
136 Host: localhost
137 X-Blah: ignored header
138 X-Blah: ignored header
139 X-Blah: ignored header
140 X-Blah: ignored header
141 X-Blah: ignored header
142 X-Blah: ignored header
143 X-Blah: ignored header
144 X-Blah: ignored header
145 X-Blah: ignored header
146 X-Blah: ignored header
147 X-Blah: ignored header
148 X-Blah: ignored header
149 X-Blah: ignored header
150 X-Blah: ignored header
151 X-Blah: ignored header
152 X-Blah: ignored header
153 X-Blah: ignored header
154 X-Blah: ignored header
155 X-Blah: ignored header
156
157 EOF
158 }
159
160 sub http_post {
161 my ($url, %extra) = @_;
162
163 my $p = "POST $url HTTP/1.0" . CRLF .
164 "Host: localhost" . CRLF .
165 "Content-Length: 10" . CRLF .
166 CRLF .
167 "1234567890";
168
169 return http($p, %extra);
170 }
171
172 sub http_post_big {
173 my ($url, %extra) = @_;
174
175 my $p = "POST $url HTTP/1.0" . CRLF .
176 "Host: localhost" . CRLF .
177 "Content-Length: 10240" . CRLF .
178 CRLF .
179 ("1234567890" x 1024);
180
181 return http($p, %extra);
182 }
183
184 ###############################################################################