|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +# (C) Vadim Zhestikov |
| 4 | +# (C) Nginx, Inc. |
| 5 | + |
| 6 | +# Tests for http njs module, js_preload_object 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 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 | + js_preload_object g1 from g.json; |
| 39 | + js_preload_object ga from ga.json; |
| 40 | +
|
| 41 | + server { |
| 42 | + listen 127.0.0.1:8080; |
| 43 | + server_name localhost; |
| 44 | +
|
| 45 | + js_import lib.js; |
| 46 | + js_preload_object lx from l.json; |
| 47 | +
|
| 48 | + location /test { |
| 49 | + js_content lib.test; |
| 50 | + } |
| 51 | +
|
| 52 | + location /test_query { |
| 53 | + js_import lib1.js; |
| 54 | + js_content lib1.query; |
| 55 | + } |
| 56 | +
|
| 57 | + location /test_query_preloaded { |
| 58 | + js_import lib1.js; |
| 59 | + js_preload_object l.json; |
| 60 | + js_content lib1.query; |
| 61 | + } |
| 62 | +
|
| 63 | + location /test_var { |
| 64 | + js_set $test_var lib.test_var; |
| 65 | + return 200 $test_var; |
| 66 | + } |
| 67 | +
|
| 68 | + location /test_mutate { |
| 69 | + js_content lib.mutate; |
| 70 | + } |
| 71 | +
|
| 72 | + location /test_no_suffix { |
| 73 | + js_preload_object gg from no_suffix; |
| 74 | + js_content lib.suffix; |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | +
|
| 79 | +EOF |
| 80 | + |
| 81 | +$t->write_file('lib.js', <<EOF); |
| 82 | + function test(r) { |
| 83 | + r.return(200, ga + ' ' + g1.c.prop[0].a + ' ' + lx); |
| 84 | + } |
| 85 | +
|
| 86 | + function test_var(r) { |
| 87 | + return g1.b[2]; |
| 88 | + } |
| 89 | +
|
| 90 | + function mutate(r) { |
| 91 | + var res = "OK"; |
| 92 | +
|
| 93 | + try { |
| 94 | + switch (r.args.method) { |
| 95 | + case 'set_obj': |
| 96 | + g1.c.prop[0].a = 5; |
| 97 | + break; |
| 98 | + case 'set_arr': |
| 99 | + g1.c.prop[0] = 5; |
| 100 | + break; |
| 101 | + case 'add_obj': |
| 102 | + g1.c.prop[0].xxx = 5; |
| 103 | + break; |
| 104 | + case 'add_arr': |
| 105 | + g1.c.prop[10] = 5; |
| 106 | + break; |
| 107 | + case 'del_obj': |
| 108 | + delete g1.c.prop[0].a; |
| 109 | + break; |
| 110 | + case 'del_arr': |
| 111 | + delete g1.c.prop[0]; |
| 112 | + break; |
| 113 | + } |
| 114 | +
|
| 115 | + } catch (e) { |
| 116 | + res = e.message; |
| 117 | + } |
| 118 | +
|
| 119 | + r.return(200, res); |
| 120 | + } |
| 121 | +
|
| 122 | + function suffix(r) { |
| 123 | + r.return(200, gg); |
| 124 | + } |
| 125 | +
|
| 126 | + export default {test, test_var, mutate, suffix}; |
| 127 | +
|
| 128 | +EOF |
| 129 | + |
| 130 | +$t->write_file('lib1.js', <<EOF); |
| 131 | + function query(r) { |
| 132 | + var res = 'ok'; |
| 133 | +
|
| 134 | + try { |
| 135 | + res = r.args.path.split('.').reduce((a, v) => a[v], globalThis); |
| 136 | +
|
| 137 | + } catch (e) { |
| 138 | + res = e.message; |
| 139 | + } |
| 140 | +
|
| 141 | + r.return(200, njs.dump(res)); |
| 142 | + } |
| 143 | +
|
| 144 | + export default {query}; |
| 145 | +
|
| 146 | +EOF |
| 147 | + |
| 148 | +$t->write_file('g.json', |
| 149 | + '{"a":1, "b":[1,2,"element",4,5], "c":{"prop":[{"a":2}]}}'); |
| 150 | +$t->write_file('ga.json', '"ga loaded"'); |
| 151 | +$t->write_file('l.json', '"l loaded"'); |
| 152 | +$t->write_file('no_suffix', '"no_suffix loaded"'); |
| 153 | + |
| 154 | +$t->try_run('no js_preload_object available')->plan(12); |
| 155 | + |
| 156 | +############################################################################### |
| 157 | + |
| 158 | +like(http_get('/test'), qr/ga loaded 2 l loaded/s, 'direct query'); |
| 159 | +like(http_get('/test_query?path=l'), qr/undefined/s, 'unreferenced'); |
| 160 | +like(http_get('/test_query_preloaded?path=l'), qr/l loaded/s, |
| 161 | + 'reference preload'); |
| 162 | +like(http_get('/test_query?path=g1.b.1'), qr/2/s, 'complex query'); |
| 163 | +like(http_get('/test_var'), qr/element/s, 'var reference'); |
| 164 | + |
| 165 | +like(http_get('/test_mutate?method=set_obj'), qr/Cannot assign to read-only/s, |
| 166 | + 'preload_object props are const (object)'); |
| 167 | +like(http_get('/test_mutate?method=set_arr'), qr/Cannot assign to read-only/s, |
| 168 | + 'preload_object props are const (array)'); |
| 169 | +like(http_get('/test_mutate?method=add_obj'), qr/Cannot add property "xxx"/s, |
| 170 | + 'preload_object props are not extensible (object)'); |
| 171 | +like(http_get('/test_mutate?method=add_arr'), qr/Cannot add property "10"/s, |
| 172 | + 'preload_object props are not extensible (array)'); |
| 173 | +like(http_get('/test_mutate?method=del_obj'), qr/Cannot delete property "a"/s, |
| 174 | + 'preload_object props are not deletable (object)'); |
| 175 | +like(http_get('/test_mutate?method=del_arr'), qr/Cannot delete property "0"/s, |
| 176 | + 'preload_object props are not deletable (array)'); |
| 177 | + |
| 178 | +like(http_get('/test_no_suffix'), qr/no_suffix loaded/s, |
| 179 | + 'load without suffix'); |
| 180 | + |
| 181 | +############################################################################### |
0 commit comments