Mercurial > hg > nginx-tests
comparison js_modules.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, ES6 import, export. | |
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_include test.js; | |
39 | |
40 server { | |
41 listen 127.0.0.1:8080; | |
42 server_name localhost; | |
43 | |
44 location /njs { | |
45 js_content test_njs; | |
46 } | |
47 | |
48 location /test { | |
49 js_content test; | |
50 } | |
51 } | |
52 } | |
53 | |
54 EOF | |
55 | |
56 $t->write_file('test.js', <<EOF); | |
57 import m from 'module.js'; | |
58 | |
59 function test_njs(r) { | |
60 r.return(200, njs.version); | |
61 } | |
62 | |
63 function test(r) { | |
64 r.return(200, m[r.args.fun](r.args.a, r.args.b)); | |
65 } | |
66 | |
67 EOF | |
68 | |
69 $t->write_file('module.js', <<EOF); | |
70 function sum(a, b) { | |
71 return Number(a) + Number(b); | |
72 } | |
73 | |
74 function prod(a, b) { | |
75 return Number(a) * Number(b); | |
76 } | |
77 | |
78 export default {sum, prod}; | |
79 | |
80 EOF | |
81 | |
82 | |
83 $t->try_run('no njs modules')->plan(2); | |
84 | |
85 ############################################################################### | |
86 | |
87 like(http_get('/test?fun=sum&a=3&b=4'), qr/7/s, 'test sum'); | |
88 like(http_get('/test?fun=prod&a=3&b=4'), qr/12/s, 'test prod'); | |
89 | |
90 ############################################################################### |