22
33from unit .applications .proto import TestApplicationProto
44from unit .option import option
5+ from unit .utils import waitforfiles
56
67
78class TestNJS (TestApplicationProto ):
89 prerequisites = {'modules' : {'njs' : 'any' }}
910
1011 def setup_method (self ):
11- os .makedirs (option .temp_dir + '/assets' )
12- open (option .temp_dir + '/assets/index.html' , 'a' )
13- open (option .temp_dir + '/assets/localhost' , 'a' )
14- open (option .temp_dir + '/assets/`string`' , 'a' )
15- open (option .temp_dir + '/assets/`backtick' , 'a' )
16- open (option .temp_dir + '/assets/l1\n l2' , 'a' )
17- open (option .temp_dir + '/assets/127.0.0.1' , 'a' )
18-
1912 assert 'success' in self .conf (
2013 {
2114 "listeners" : {"*:7080" : {"pass" : "routes" }},
@@ -25,52 +18,55 @@ def setup_method(self):
2518 }
2619 )
2720
21+ def create_files (self , * files ):
22+ assets_dir = option .temp_dir + '/assets/'
23+ os .makedirs (assets_dir )
24+
25+ [open (assets_dir + f , 'a' ) for f in files ]
26+ waitforfiles (* [assets_dir + f for f in files ])
27+
2828 def set_share (self , share ):
2929 assert 'success' in self .conf (share , 'routes/0/action/share' )
3030
31- def test_njs_template_string (self , temp_dir ):
32- self .set_share ('"`' + temp_dir + '/assets/index.html `"' )
33- assert self .get ()['status' ] == 200 , 'string'
31+ def check_expression (self , expression , url = '/' ):
32+ self .set_share ('"`' + option . temp_dir + '/assets' + expression + ' `"' )
33+ assert self .get (url = url )['status' ] == 200
3434
35- self . set_share ( '"' + temp_dir + '/assets/`string`"' )
36- assert self .get ()[ 'status' ] == 200 , 'string 2'
35+ def test_njs_template_string ( self , temp_dir ):
36+ self .create_files ( 'str' , '` string`' , '`backtick' , 'l1 \n l2' )
3737
38- self .set_share ('"`' + temp_dir + '/assets/\\ \\ `backtick`"' )
39- assert self .get ()['status' ] == 200 , 'escape'
38+ self .check_expression ('/str' )
39+ self .check_expression ('/\\ \\ `backtick' )
40+ self .check_expression ('/l1\\ nl2' )
4041
41- self .set_share ('"` ' + temp_dir + '/assets/l1 \\ nl2 `"' )
42- assert self .get ()['status' ] == 200 , 'multiline'
42+ self .set_share ('"' + temp_dir + '/assets/`string `"' )
43+ assert self .get ()['status' ] == 200
4344
4445 def test_njs_template_expression (self , temp_dir ):
45- def check_expression (expression ):
46- self .set_share (expression )
47- assert self .get ()['status' ] == 200
46+ self .create_files ('str' , 'localhost' )
4847
49- check_expression ('"`' + temp_dir + '/assets ${uri}`" ' )
50- check_expression ('"`' + temp_dir + '/assets ${uri}${host}`" ' )
51- check_expression ('"`' + temp_dir + '/assets ${uri + host}`" ' )
52- check_expression ('"`' + temp_dir + '/assets ${uri + `${host}`}`" ' )
48+ self . check_expression ('${uri}' , '/str ' )
49+ self . check_expression ('${uri}${host}' )
50+ self . check_expression ('${uri + host}' )
51+ self . check_expression ('${uri + `${host}`}' )
5352
5453 def test_njs_variables (self , temp_dir ):
55- self .set_share ('"`' + temp_dir + '/assets/${host}`"' )
56- assert self .get ()['status' ] == 200 , 'host'
57-
58- self .set_share ('"`' + temp_dir + '/assets/${remoteAddr}`"' )
59- assert self .get ()['status' ] == 200 , 'remoteAddr'
54+ self .create_files ('str' , 'localhost' , '127.0.0.1' )
6055
61- self .set_share ('"`' + temp_dir + '/assets/${headers.Host}`"' )
62- assert self .get ()['status' ] == 200 , 'headers'
56+ self .check_expression ('/${host}' )
57+ self .check_expression ('/${remoteAddr}' )
58+ self .check_expression ('/${headers.Host}' )
6359
6460 self .set_share ('"`' + temp_dir + '/assets/${cookies.foo}`"' )
6561 assert (
66- self .get (
67- headers = { 'Cookie' : 'foo=localhost' , 'Connection' : 'close' }
68- )[ 'status' ]
62+ self .get (headers = { 'Cookie' : 'foo=str' , 'Connection' : 'close' })[
63+ 'status'
64+ ]
6965 == 200
7066 ), 'cookies'
7167
7268 self .set_share ('"`' + temp_dir + '/assets/${args.foo}`"' )
73- assert self .get (url = '/?foo=localhost ' )['status' ] == 200 , 'args'
69+ assert self .get (url = '/?foo=str ' )['status' ] == 200 , 'args'
7470
7571 def test_njs_invalid (self , temp_dir , skip_alert ):
7672 skip_alert (r'js exception:' )
0 commit comments