diff --git a/nginx.py b/nginx.py index 866aea3..12852a1 100755 --- a/nginx.py +++ b/nginx.py @@ -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) @@ -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) @@ -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) @@ -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:]) diff --git a/tests.py b/tests.py index 6b14d85..9254109 100755 --- a/tests.py +++ b/tests.py @@ -66,7 +66,7 @@ } """ -TESTBLOCK_CASE_3=""" +TESTBLOCK_CASE_3 = """ upstream test0 { ip_hash; server 127.0.0.1:8080; @@ -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): @@ -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) @@ -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/" @@ -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()