Mercurial > hg > nginx-tests
comparison js_paths.t @ 1456:f4ae08adc23f
Tests: added njs modules tests.
author | Dmitry Volyntsev <xeioex@nginx.com> |
---|---|
date | Tue, 26 Mar 2019 15:43:16 +0300 |
parents | |
children | bc0990ea2e5b |
comparison
equal
deleted
inserted
replaced
1455:558d3d9a000c | 1456:f4ae08adc23f |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Dmitry Volyntsev | |
4 # (C) Nginx, Inc. | |
5 | |
6 # Tests for http njs module, js_path directive. | |
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/) | |
26 ->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 js_path "%%TESTDIR%%/lib1"; | |
39 js_path "lib2"; | |
40 | |
41 js_include test.js; | |
42 | |
43 server { | |
44 listen 127.0.0.1:8080; | |
45 server_name localhost; | |
46 | |
47 location /njs { | |
48 js_content test_njs; | |
49 } | |
50 | |
51 location /test { | |
52 js_content test; | |
53 } | |
54 | |
55 location /test2 { | |
56 js_content test2; | |
57 } | |
58 } | |
59 } | |
60 | |
61 EOF | |
62 | |
63 $t->write_file('test.js', <<EOF); | |
64 import m1 from 'module1.js'; | |
65 import m2 from 'module2.js'; | |
66 import m3 from 'lib1/module1.js'; | |
67 | |
68 function test_njs(r) { | |
69 r.return(200, njs.version); | |
70 } | |
71 | |
72 function test(r) { | |
73 r.return(200, m1[r.args.fun](r.args.a, r.args.b)); | |
74 } | |
75 | |
76 function test2(r) { | |
77 r.return(200, m2.sum(r.args.a, r.args.b)); | |
78 } | |
79 | |
80 function test3(r) { | |
81 r.return(200, m3.sum(r.args.a, r.args.b)); | |
82 } | |
83 | |
84 EOF | |
85 | |
86 my $d = $t->testdir(); | |
87 | |
88 mkdir("$d/lib1"); | |
89 mkdir("$d/lib2"); | |
90 | |
91 $t->write_file('lib1/module1.js', <<EOF); | |
92 function sum(a, b) { return Number(a) + Number(b); } | |
93 function prod(a, b) { return Number(a) * Number(b); } | |
94 | |
95 export default {sum, prod}; | |
96 | |
97 EOF | |
98 | |
99 $t->write_file('lib2/module2.js', <<EOF); | |
100 function sum(a, b) { return a + b; } | |
101 | |
102 export default {sum}; | |
103 | |
104 EOF | |
105 | |
106 | |
107 $t->try_run('no njs available')->plan(4); | |
108 | |
109 ############################################################################### | |
110 | |
111 like(http_get('/test?fun=sum&a=3&b=4'), qr/7/s, 'test sum'); | |
112 like(http_get('/test?fun=prod&a=3&b=4'), qr/12/s, 'test prod'); | |
113 like(http_get('/test2?a=3&b=4'), qr/34/s, 'test2'); | |
114 like(http_get('/test2?a=A&b=B'), qr/AB/s, 'test2 relative'); | |
115 | |
116 ############################################################################### |