comparison js_import.t @ 1562:b4f528987146

Tests: added js_import tests.
author Dmitry Volyntsev <xeioex@nginx.com>
date Tue, 14 Apr 2020 12:14:47 +0000
parents
children
comparison
equal deleted inserted replaced
1561:75fb32094392 1562:b4f528987146
1 #!/usr/bin/perl
2
3 # (C) Dmitry Volyntsev
4 # (c) Nginx, Inc.
5
6 # Tests for http njs module, js_import 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_set $test foo.bar.p;
39
40 js_import lib.js;
41 js_import fun.js;
42 js_import foo from ./main.js;
43
44 server {
45 listen 127.0.0.1:8080;
46 server_name localhost;
47
48 location /njs {
49 js_content foo.version;
50 }
51
52 location /test_foo {
53 js_content foo.test;
54 }
55
56 location /test_lib {
57 js_content lib.test;
58 }
59
60 location /test_fun {
61 js_content fun;
62 }
63
64 location /test_var {
65 return 200 $test;
66 }
67 }
68 }
69
70 EOF
71
72 $t->write_file('lib.js', <<EOF);
73 function test(r) {
74 r.return(200, "LIB-TEST");
75 }
76
77 export default {test};
78
79 EOF
80
81 $t->write_file('fun.js', <<EOF);
82 export default function (r) {r.return(200, "FUN-TEST")};
83
84 EOF
85
86 $t->write_file('main.js', <<EOF);
87 function version(r) {
88 r.return(200, njs.version);
89 }
90
91 function test(r) {
92 r.return(200, "MAIN-TEST");
93 }
94
95 export default {version, test, bar: {p(r) {return "P-TEST"}}};
96
97 EOF
98
99 $t->try_run('no njs available')->plan(4);
100
101 ###############################################################################
102
103 like(http_get('/test_foo'), qr/MAIN-TEST/s, 'foo.test');
104 like(http_get('/test_lib'), qr/LIB-TEST/s, 'lib.test');
105 like(http_get('/test_fun'), qr/FUN-TEST/s, 'fun');
106 like(http_get('/test_var'), qr/P-TEST/s, 'foo.bar.p');
107
108 ###############################################################################