Mercurial > hg > nginx-tests
annotate js_fetch_resolver.t @ 1826:1f125771f1a1
Tests: adapted session reuse tests to work with TLSv1.3.
In TLSv1.3, session tickets are sent after the handshake, and saving session
right after the handshake is not going to work. To properly test session
resumption, sessions are now saved after some data exchange.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Tue, 21 Mar 2023 02:58:02 +0300 |
parents | f89770df737a |
children |
rev | line source |
---|---|
1640
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
1 #!/usr/bin/perl |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
2 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
3 # (C) Dmitry Volyntsev |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
4 # (C) Nginx, Inc. |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
5 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
6 # Tests for http njs module, fetch method, dns support. |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
7 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
8 ############################################################################### |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
9 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
10 use warnings; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
11 use strict; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
12 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
13 use Test::More; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
14 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
15 use IO::Select; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
16 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
17 BEGIN { use FindBin; chdir($FindBin::Bin); } |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
18 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
19 use lib 'lib'; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
20 use Test::Nginx; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
21 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
22 ############################################################################### |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
23 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
24 select STDERR; $| = 1; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
25 select STDOUT; $| = 1; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
26 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
27 plan(skip_all => '127.0.0.2 local address required') |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
28 unless defined IO::Socket::INET->new( LocalAddr => '127.0.0.2' ); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
29 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
30 my $t = Test::Nginx->new()->has(qw/http/) |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
31 ->write_file_expand('nginx.conf', <<'EOF'); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
32 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
33 %%TEST_GLOBALS%% |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
34 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
35 daemon off; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
36 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
37 events { |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
38 } |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
39 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
40 http { |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
41 %%TEST_GLOBALS_HTTP%% |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
42 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
43 js_import test.js; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
44 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
45 server { |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
46 listen 127.0.0.1:8080; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
47 server_name localhost; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
48 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
49 location /njs { |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
50 js_content test.njs; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
51 } |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
52 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
53 location /dns { |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
54 js_content test.dns; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
55 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
56 resolver 127.0.0.1:%%PORT_8981_UDP%%; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
57 resolver_timeout 1s; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
58 } |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
59 } |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
60 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
61 server { |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
62 listen 127.0.0.1:8080; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
63 server_name aaa; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
64 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
65 location /loc { |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
66 js_content test.loc; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
67 } |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
68 } |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
69 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
70 server { |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
71 listen 127.0.0.1:8080; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
72 server_name many; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
73 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
74 location /loc { |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
75 js_content test.loc; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
76 } |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
77 } |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
78 } |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
79 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
80 EOF |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
81 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
82 my $p0 = port(8080); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
83 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
84 $t->write_file('test.js', <<EOF); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
85 function test_njs(r) { |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
86 r.return(200, njs.version); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
87 } |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
88 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
89 function dns(r) { |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
90 var url = `http://\${r.args.domain}:$p0/loc`; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
91 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
92 ngx.fetch(url) |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
93 .then(reply => reply.text()) |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
94 .then(body => r.return(200, body)) |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
95 .catch(e => r.return(501, e.message)) |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
96 } |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
97 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
98 function str(v) { return v ? v : ''}; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
99 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
100 function loc(r) { |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
101 var v = r.variables; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
102 var body = str(r.requestText); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
103 var foo = str(r.headersIn.foo); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
104 var bar = str(r.headersIn.bar); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
105 var c = r.headersIn.code ? Number(r.headersIn.code) : 200; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
106 r.return(c, `\${v.host}:\${v.request_method}:\${foo}:\${bar}:\${body}`); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
107 } |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
108 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
109 export default {njs: test_njs, dns, loc}; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
110 EOF |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
111 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
112 $t->try_run('no njs.fetch')->plan(3); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
113 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
114 $t->run_daemon(\&dns_daemon, port(8981), $t); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
115 $t->waitforfile($t->testdir . '/' . port(8981)); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
116 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
117 ############################################################################### |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
118 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
119 like(http_get('/dns?domain=aaa'), qr/aaa:GET:::$/s, 'fetch dns aaa'); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
120 like(http_get('/dns?domain=many'), qr/many:GET:::$/s, 'fetch dns many'); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
121 like(http_get('/dns?domain=unknown'), qr/"unknown" could not be resolved/s, |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
122 'fetch dns unknown'); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
123 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
124 ############################################################################### |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
125 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
126 sub reply_handler { |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
127 my ($recv_data, $port, %extra) = @_; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
128 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
129 my (@name, @rdata); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
130 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
131 use constant NOERROR => 0; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
132 use constant FORMERR => 1; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
133 use constant SERVFAIL => 2; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
134 use constant NXDOMAIN => 3; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
135 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
136 use constant A => 1; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
137 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
138 use constant IN => 1; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
139 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
140 # default values |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
141 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
142 my ($hdr, $rcode, $ttl) = (0x8180, NOERROR, 3600); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
143 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
144 # decode name |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
145 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
146 my ($len, $offset) = (undef, 12); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
147 while (1) { |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
148 $len = unpack("\@$offset C", $recv_data); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
149 last if $len == 0; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
150 $offset++; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
151 push @name, unpack("\@$offset A$len", $recv_data); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
152 $offset += $len; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
153 } |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
154 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
155 $offset -= 1; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
156 my ($id, $type, $class) = unpack("n x$offset n2", $recv_data); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
157 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
158 my $name = join('.', @name); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
159 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
160 if ($name eq 'aaa' && $type == A) { |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
161 push @rdata, rd_addr($ttl, '127.0.0.1'); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
162 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
163 } elsif ($name eq 'many' && $type == A) { |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
164 push @rdata, rd_addr($ttl, '127.0.0.2'); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
165 push @rdata, rd_addr($ttl, '127.0.0.1'); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
166 } |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
167 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
168 $len = @name; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
169 pack("n6 (C/a*)$len x n2", $id, $hdr | $rcode, 1, scalar @rdata, |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
170 0, 0, @name, $type, $class) . join('', @rdata); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
171 } |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
172 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
173 sub rd_addr { |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
174 my ($ttl, $addr) = @_; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
175 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
176 my $code = 'split(/\./, $addr)'; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
177 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
178 return pack 'n3N', 0xc00c, A, IN, $ttl if $addr eq ''; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
179 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
180 pack 'n3N nC4', 0xc00c, A, IN, $ttl, eval "scalar $code", eval($code); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
181 } |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
182 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
183 sub dns_daemon { |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
184 my ($port, $t, %extra) = @_; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
185 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
186 my ($data, $recv_data); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
187 my $socket = IO::Socket::INET->new( |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
188 LocalAddr => '127.0.0.1', |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
189 LocalPort => $port, |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
190 Proto => 'udp', |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
191 ) |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
192 or die "Can't create listening socket: $!\n"; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
193 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
194 my $sel = IO::Select->new($socket); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
195 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
196 local $SIG{PIPE} = 'IGNORE'; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
197 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
198 # signal we are ready |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
199 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
200 open my $fh, '>', $t->testdir() . '/' . $port; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
201 close $fh; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
202 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
203 while (my @ready = $sel->can_read) { |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
204 foreach my $fh (@ready) { |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
205 if ($socket == $fh) { |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
206 $fh->recv($recv_data, 65536); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
207 $data = reply_handler($recv_data, $port); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
208 $fh->send($data); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
209 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
210 } else { |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
211 $fh->recv($recv_data, 65536); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
212 unless (length $recv_data) { |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
213 $sel->remove($fh); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
214 $fh->close; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
215 next; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
216 } |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
217 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
218 again: |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
219 my $len = unpack("n", $recv_data); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
220 $data = substr $recv_data, 2, $len; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
221 $data = reply_handler($data, $port, tcp => 1); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
222 $data = pack("n", length $data) . $data; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
223 $fh->send($data); |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
224 $recv_data = substr $recv_data, 2 + $len; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
225 goto again if length $recv_data; |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
226 } |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
227 } |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
228 } |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
229 } |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
230 |
67adc5fd0548
Tests: added js tests for ngx.fetch() method.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
diff
changeset
|
231 ############################################################################### |