2
2
3
3
from unit .applications .proto import TestApplicationProto
4
4
from unit .option import option
5
+ from unit .utils import waitforfiles
5
6
6
7
7
8
class TestNJS (TestApplicationProto ):
8
9
prerequisites = {'modules' : {'njs' : 'any' }}
9
10
10
11
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
-
19
12
assert 'success' in self .conf (
20
13
{
21
14
"listeners" : {"*:7080" : {"pass" : "routes" }},
@@ -25,52 +18,55 @@ def setup_method(self):
25
18
}
26
19
)
27
20
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
+
28
28
def set_share (self , share ):
29
29
assert 'success' in self .conf (share , 'routes/0/action/share' )
30
30
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
34
34
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' )
37
37
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' )
40
41
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
43
44
44
45
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' )
48
47
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}`}' )
53
52
54
53
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' )
60
55
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}' )
63
59
64
60
self .set_share ('"`' + temp_dir + '/assets/${cookies.foo}`"' )
65
61
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
+ ]
69
65
== 200
70
66
), 'cookies'
71
67
72
68
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'
74
70
75
71
def test_njs_invalid (self , temp_dir , skip_alert ):
76
72
skip_alert (r'js exception:' )
0 commit comments