Mercurial > hg > nginx-tests
comparison js_internal_redirect.t @ 1343:2ff483941037
Tests: added njs http internalRedirect() method tests.
author | Dmitry Volyntsev <xeioex@nginx.com> |
---|---|
date | Wed, 13 Jun 2018 14:53:43 +0300 |
parents | |
children | 9fbe84b7ddc6 |
comparison
equal
deleted
inserted
replaced
1342:5833f3b7a884 | 1343:2ff483941037 |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Dmitry Volyntsev | |
4 # (C) Nginx, Inc. | |
5 | |
6 # Tests for http njs module, internalRedirect method. | |
7 | |
8 ############################################################################### | |
9 | |
10 use warnings; | |
11 use strict; | |
12 | |
13 use Test::More; | |
14 | |
15 use Config; | |
16 | |
17 BEGIN { use FindBin; chdir($FindBin::Bin); } | |
18 | |
19 use lib 'lib'; | |
20 use Test::Nginx; | |
21 | |
22 ############################################################################### | |
23 | |
24 select STDERR; $| = 1; | |
25 select STDOUT; $| = 1; | |
26 | |
27 my $t = Test::Nginx->new()->has(qw/http/) | |
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 js_include test.js; | |
41 | |
42 server { | |
43 listen 127.0.0.1:8080; | |
44 server_name localhost; | |
45 | |
46 location /test { | |
47 js_content test_redirect; | |
48 } | |
49 | |
50 location /redirect { | |
51 internal; | |
52 return 200 redirect$arg_b; | |
53 } | |
54 | |
55 location @named { | |
56 return 200 named; | |
57 } | |
58 } | |
59 } | |
60 | |
61 EOF | |
62 | |
63 $t->write_file('test.js', <<EOF); | |
64 function test_redirect(req, res) { | |
65 if (req.variables.arg_dest.startsWith('named')) { | |
66 req.internalRedirect('\@named'); | |
67 | |
68 } else { | |
69 if (req.variables.arg_a) { | |
70 req.internalRedirect('/redirect?b=' + req.variables.arg_a); | |
71 | |
72 } else { | |
73 req.internalRedirect('/redirect'); | |
74 } | |
75 } | |
76 } | |
77 | |
78 EOF | |
79 | |
80 $t->try_run('no njs internalRedirect')->plan(3); | |
81 | |
82 ############################################################################### | |
83 | |
84 like(http_get('/test'), qr/redirect/s, 'redirect'); | |
85 like(http_get('/test?a=A'), qr/redirectA/s, 'redirect with args'); | |
86 like(http_get('/test?dest=named'), qr/named/s, 'redirect to named location'); | |
87 | |
88 ############################################################################### |