comparison js_import2.t @ 1782:390cbd46c46b

Tests: added js tests for directives in additional contexts.
author Dmitry Volyntsev <xeioex@nginx.com>
date Tue, 02 Aug 2022 20:35:06 -0700
parents
children
comparison
equal deleted inserted replaced
1781:386748f328b1 1782:390cbd46c46b
1 #!/usr/bin/perl
2
3 # (C) Dmitry Volyntsev
4 # (c) Nginx, Inc.
5
6 # Tests for http njs module, js_import directive in server | location contexts.
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 proxy rewrite/)
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 server {
39 listen 127.0.0.1:8080;
40 server_name localhost;
41
42 js_set $test foo.bar.p;
43
44 js_import foo from main.js;
45
46 location /njs {
47 js_content foo.version;
48 }
49
50 location /test_foo {
51 js_content foo.test;
52 }
53
54 location /test_lib {
55 js_import lib.js;
56 js_content lib.test;
57 }
58
59 location /test_fun {
60 js_import fun.js;
61 js_content fun;
62 }
63
64 location /test_var {
65 return 200 $test;
66 }
67
68 location /proxy {
69 proxy_pass http://127.0.0.1:8081/;
70 }
71 }
72
73 server {
74 listen 127.0.0.1:8081;
75 server_name localhost;
76
77 location /test_fun {
78 js_import fun.js;
79 js_content fun;
80 }
81 }
82 }
83
84 EOF
85
86 $t->write_file('lib.js', <<EOF);
87 function test(r) {
88 r.return(200, "LIB-TEST");
89 }
90
91 function p(r) {
92 return "LIB-P";
93 }
94
95 export default {test, p};
96
97 EOF
98
99 $t->write_file('fun.js', <<EOF);
100 export default function (r) {r.return(200, "FUN-TEST")};
101
102 EOF
103
104 $t->write_file('main.js', <<EOF);
105 function version(r) {
106 r.return(200, njs.version);
107 }
108
109 function test(r) {
110 r.return(200, "MAIN-TEST");
111 }
112
113 export default {version, test, bar: {p(r) {return "P-TEST"}}};
114
115 EOF
116
117 $t->try_run('no njs available')->plan(5);
118
119 ###############################################################################
120
121 like(http_get('/test_foo'), qr/MAIN-TEST/s, 'foo.test');
122 like(http_get('/test_lib'), qr/LIB-TEST/s, 'lib.test');
123 like(http_get('/test_fun'), qr/FUN-TEST/s, 'fun');
124 like(http_get('/proxy/test_fun'), qr/FUN-TEST/s, 'proxy fun');
125 like(http_get('/test_var'), qr/P-TEST/s, 'foo.bar.p');
126
127 ###############################################################################