Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions nginx.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ def as_strings(self):
if self.value == '' or self.value is None:
return '{0};\n'.format(self.name)
if ';' in self.value or '#' in self.value:
return '{0} "{1}";\n'.format(self.name, self.value)
return '{0} {1};\n'.format(self.name, self.value)
return '{0} {1};\n'.format(self.name, self.value)


Expand Down Expand Up @@ -419,7 +419,8 @@ def loads(data, conf=True):
index += m.end()
continue

m = re.compile(r'^\s*location\s*(.*?\S+)\s*{', re.S).search(data[index:])
m = re.compile(
r'^\s*location\s*(.*?\S+)\s*{', re.S).search(data[index:])
if m:
l = Location(m.group(1))
lopen.insert(0, l)
Expand All @@ -433,7 +434,8 @@ def loads(data, conf=True):
index += m.end()
continue

m = re.compile(r'^\s*upstream\s*(.*?\S+)\s*{', re.S).search(data[index:])
m = re.compile(
r'^\s*upstream\s*(.*?\S+)\s*{', re.S).search(data[index:])
if m:
u = Upstream(m.group(1))
lopen.insert(0, u)
Expand Down Expand Up @@ -476,7 +478,7 @@ def loads(data, conf=True):
index += m.end()
continue

key_with_quoted = r'^\s*(\S*?)\s*"([^"]+)";?|\'([^\']+)\';?|\\S+;?'
key_with_quoted = r'^\s*(\S*?)\s*(\"[^"]+\");?|(\'[^\']+\');?|\\S+;?'
key_wo_quoted = r'^\s*([a-zA-Z0-9-_]+?)\s+(.+?);'
m1 = re.compile(key_with_quoted, re.S).search(data[index:])
m2 = re.compile(key_wo_quoted, re.S).search(data[index:])
Expand Down
32 changes: 29 additions & 3 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
}
"""

TESTBLOCK_CASE_3="""
TESTBLOCK_CASE_3 = """
upstream test0 {
ip_hash;
server 127.0.0.1:8080;
Expand Down Expand Up @@ -100,6 +100,26 @@
location /{ test_key test_value; }}
"""

TESTBLOCK_CASE_5 = """
upstream test0 {
server 1.1.1.1:8080;
send "some request";
}

upstream test1 {
server 1.1.1.1:8080;
send 'some request';
}

server {
server_name "www.example.com";

location / {
root html;
}
}
"""


class TestPythonNginx(unittest.TestCase):
def test_basic_load(self):
Expand All @@ -124,7 +144,7 @@ def test_key_parse(self):
self.assertEqual(firstKey.name, 'listen')
self.assertEqual(firstKey.value, '80')
self.assertEqual(thirdKey.name, 'mykey')
self.assertEqual(thirdKey.value, 'myvalue; #notme myothervalue')
self.assertEqual(thirdKey.value, '"myvalue; #notme myothervalue"')

def test_key_parse_complex(self):
data = nginx.loads(TESTBLOCK_CASE_2)
Expand All @@ -134,7 +154,8 @@ def test_key_parse_complex(self):
self.assertEqual(firstKey.name, 'listen')
self.assertEqual(firstKey.value, '80')
self.assertEqual(thirdKey.name, 'mykey')
self.assertEqual(thirdKey.value, 'myvalue; #notme myothervalue')

self.assertEqual(thirdKey.value, '"myvalue; #notme myothervalue"')
self.assertEqual(
data.server.locations[-1].keys[0].value,
"301 $scheme://$host:$server_port${request_uri}bitbucket/"
Expand Down Expand Up @@ -167,6 +188,11 @@ def test_filtering(self):
self.assertEqual(len(data.server.filter('Key', 'mykey')), 1)
self.assertEqual(data.server.filter('Key', 'nothere'), [])

def test_quoted_key_value(self):
data = nginx.loads(TESTBLOCK_CASE_5)
out_data = '\n' + nginx.dumps(data)
self.assertEqual(out_data, TESTBLOCK_CASE_5)


if __name__ == '__main__':
unittest.main()