comparison grpc_pass.t @ 1547:2d747d967fc0

Tests: grpc_pass with variables.
author Sergey Kandaurov <pluknet@nginx.com>
date Mon, 20 Jan 2020 16:30:56 +0300
parents
children 5ac6efbe5552
comparison
equal deleted inserted replaced
1546:0902a4539c99 1547:2d747d967fc0
1 #!/usr/bin/perl
2
3 # (C) Sergey Kandaurov
4 # (C) Nginx, Inc.
5
6 # Tests for the grpc_pass directive with variables.
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 http_ssl http_v2 grpc rewrite/)
26 ->has_daemon('openssl')->write_file_expand('nginx.conf', <<'EOF');
27
28 %%TEST_GLOBALS%%
29
30 daemon off;
31
32 events {
33 }
34
35 http {
36 %%TEST_GLOBALS_HTTP%%
37
38 upstream u {
39 server 127.0.0.1:8081;
40 }
41
42 resolver 127.0.0.1:%%PORT_8982_UDP%%;
43
44 server {
45 listen 127.0.0.1:8080;
46 server_name localhost;
47
48 location / {
49 grpc_pass $host:%%PORT_8081%%;
50 }
51
52 location /grpc {
53 grpc_pass grpc://$host:%%PORT_8081%%;
54 }
55
56 location /grpcs {
57 grpc_pass grpcs://$host:%%PORT_8082%%;
58 }
59
60 location /arg {
61 grpc_pass $arg_b;
62 }
63 }
64
65 server {
66 listen 127.0.0.1:8081 http2;
67 listen 127.0.0.1:8082 http2 ssl;
68 server_name localhost;
69
70 ssl_certificate_key localhost.key;
71 ssl_certificate localhost.crt;
72
73 location / {
74 return 200 $http_host;
75 }
76 }
77 }
78
79 EOF
80
81 $t->write_file('openssl.conf', <<EOF);
82 [ req ]
83 default_bits = 2048
84 encrypt_key = no
85 distinguished_name = req_distinguished_name
86 [ req_distinguished_name ]
87 EOF
88
89 my $d = $t->testdir();
90
91 foreach my $name ('localhost') {
92 system('openssl req -x509 -new '
93 . "-config $d/openssl.conf -subj /CN=$name/ "
94 . "-out $d/$name.crt -keyout $d/$name.key "
95 . ">>$d/openssl.out 2>&1") == 0
96 or die "Can't create certificate for $name: $!\n";
97 }
98
99 $t->run_daemon(\&dns_daemon, port(8982), $t);
100 $t->try_run('no grpc_pass variables')->plan(5);
101
102 $t->waitforfile($t->testdir . '/' . port(8982));
103
104 ###############################################################################
105
106 like(http_get('/basic'), qr/200 OK/, 'no scheme');
107 like(http_get('/grpc'), qr/200 OK/, 'grpc scheme');
108
109 SKIP: {
110 $t->{_configure_args} =~ /OpenSSL ([\d\.]+)/;
111 skip 'OpenSSL too old', 1 unless defined $1 and $1 ge '1.0.2';
112
113 like(http_get('/grpcs'), qr/200 OK/, 'grpcs scheme');
114
115 }
116
117 like(http_get('/arg?b=grpc://127.0.0.1:' . port(8081)), qr/200 OK/, 'addrs');
118 like(http_get('/arg?b=grpc://u'), qr/200 OK/, 'no_port');
119
120 ###############################################################################
121
122 sub reply_handler {
123 my ($recv_data) = @_;
124
125 my (@name, @rdata);
126
127 use constant NOERROR => 0;
128 use constant A => 1;
129 use constant IN => 1;
130
131 # default values
132
133 my ($hdr, $rcode, $ttl) = (0x8180, NOERROR, 3600);
134
135 # decode name
136
137 my ($len, $offset) = (undef, 12);
138 while (1) {
139 $len = unpack("\@$offset C", $recv_data);
140 last if $len == 0;
141 $offset++;
142 push @name, unpack("\@$offset A$len", $recv_data);
143 $offset += $len;
144 }
145
146 $offset -= 1;
147 my ($id, $type, $class) = unpack("n x$offset n2", $recv_data);
148
149 my $name = join('.', @name);
150 if ($name eq 'localhost' && $type == A) {
151 push @rdata, rd_addr($ttl, '127.0.0.1');
152 }
153
154 $len = @name;
155 pack("n6 (C/a*)$len x n2", $id, $hdr | $rcode, 1, scalar @rdata,
156 0, 0, @name, $type, $class) . join('', @rdata);
157 }
158
159 sub rd_addr {
160 my ($ttl, $addr) = @_;
161
162 my $code = 'split(/\./, $addr)';
163
164 return pack 'n3N', 0xc00c, A, IN, $ttl if $addr eq '';
165
166 pack 'n3N nC4', 0xc00c, A, IN, $ttl, eval "scalar $code", eval($code);
167 }
168
169 sub dns_daemon {
170 my ($port, $t) = @_;
171
172 my ($data, $recv_data);
173 my $socket = IO::Socket::INET->new(
174 LocalAddr => '127.0.0.1',
175 LocalPort => $port,
176 Proto => 'udp',
177 )
178 or die "Can't create listening socket: $!\n";
179
180 # signal we are ready
181
182 open my $fh, '>', $t->testdir() . '/' . $port;
183 close $fh;
184
185 while (1) {
186 $socket->recv($recv_data, 65536);
187 $data = reply_handler($recv_data);
188 $socket->send($data);
189 }
190 }
191
192 ###############################################################################