-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathtests.py
executable file
·135 lines (112 loc) · 3.59 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"""
Testing module for python-nginx.
python-nginx
(c) 2016 Jacob Cook
Licensed under GPLv3, see LICENSE.md
"""
# flake8: noqa
import nginx
import unittest
TESTBLOCK = """
upstream php {
server unix:/tmp/php-fcgi.socket;
}
server {
listen 80; # This comment should be present;
# And this one
server_name localhost 127.0.0.1;
root /srv/http; # And also this one
mykey "myvalue; #notme myothervalue";
# This one too
index index.php;
location ~ \.php(?:$|/) {
fastcgi_pass php;
}
}
"""
SECONDTESTBLOCK = """
upstream php
{
server unix:/tmp/php-fcgi.socket;
}
server
{
listen 80; # This comment should be present;
# And this one
server_name localhost 127.0.0.1;
root /srv/http; # And also this one
mykey "myvalue; #notme myothervalue";
# This one too
index index.php;
if (!-e $request_filename)
{
rewrite ^(.+)$ /index.php?q=$1 last;
}
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php?q=$1 last;
}
location ~ \.php(?:$|/) {
fastcgi_pass php;
}
# location from the issue #10
location / {
return 301 $scheme://$host:$server_port${request_uri}bitbucket/;
}
}
"""
MESSYBLOCK = """
# This is an example of a messy config
upstream php { server unix:/tmp/php-cgi.socket; }
server { server_name localhost; #this is the server server_name
location /{ test_key test_value; }}
"""
class TestPythonNginx(unittest.TestCase):
def test_basic_load(self):
self.assertTrue(nginx.loads(TESTBLOCK) is not None)
def test_messy_load(self):
data = nginx.loads(MESSYBLOCK)
self.assertTrue(data is not None)
self.assertTrue(len(data.server.comments), 1)
self.assertTrue(len(data.server.locations), 1)
def test_comment_parse(self):
data = nginx.loads(TESTBLOCK)
self.assertEqual(len(data.server.comments), 4)
self.assertEqual(data.server.comments[2].comment, 'And also this one')
def test_key_parse(self):
data = nginx.loads(TESTBLOCK)
self.assertEqual(len(data.server.keys), 5)
firstKey = data.server.keys[0]
thirdKey = data.server.keys[3]
self.assertEqual(firstKey.name, 'listen')
self.assertEqual(firstKey.value, '80')
self.assertEqual(thirdKey.name, 'mykey')
self.assertEqual(thirdKey.value, '"myvalue; #notme myothervalue"')
def test_key_parse_complex(self):
data = nginx.loads(SECONDTESTBLOCK)
self.assertEqual(len(data.server.keys), 5)
firstKey = data.server.keys[0]
thirdKey = data.server.keys[3]
self.assertEqual(firstKey.name, 'listen')
self.assertEqual(firstKey.value, '80')
self.assertEqual(thirdKey.name, 'mykey')
self.assertEqual(thirdKey.value, '"myvalue; #notme myothervalue"')
self.assertEqual(
data.server.locations[-1].keys[0].value,
"301 $scheme://$host:$server_port${request_uri}bitbucket/"
)
def test_location_parse(self):
data = nginx.loads(TESTBLOCK)
self.assertEqual(len(data.server.locations), 1)
firstLoc = data.server.locations[0]
self.assertEqual(firstLoc.value, '~ \.php(?:$|/)')
self.assertEqual(len(firstLoc.keys), 1)
def test_reflection(self):
inp_data = nginx.loads(TESTBLOCK)
out_data = '\n' + nginx.dumps(inp_data)
self.assertEqual(TESTBLOCK, out_data)
def test_filtering(self):
data = nginx.loads(TESTBLOCK)
self.assertEqual(len(data.server.filter('Key', 'mykey')), 1)
self.assertEqual(data.server.filter('Key', 'nothere'), [])
if __name__ == '__main__':
unittest.main()