From 7a47672f8da765e8c98ddad4eb06b46ad4b3fdb3 Mon Sep 17 00:00:00 2001 From: kfang Date: Thu, 8 Mar 2018 19:17:50 +0800 Subject: [PATCH 1/2] fix quote in value --- nginx.py | 10 ++++++---- tests.py | 34 +++++++++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 7 deletions(-) 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..a99a4f5 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,27 @@ 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; + proxy_ignore_headers "Set-Cookie" "Cache-Control" "Expires"; + } +} +""" + class TestPythonNginx(unittest.TestCase): def test_basic_load(self): @@ -124,7 +145,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 +155,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 +189,12 @@ 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) + print out_data + self.assertEqual(out_data, TESTBLOCK_CASE_5) + if __name__ == '__main__': unittest.main() From 28b1dffe3f1ebaccfd01a0dd73c0043dd9c21711 Mon Sep 17 00:00:00 2001 From: kfang Date: Fri, 9 Mar 2018 10:56:40 +0800 Subject: [PATCH 2/2] delete dirty code in test part --- tests.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests.py b/tests.py index a99a4f5..9254109 100755 --- a/tests.py +++ b/tests.py @@ -116,7 +116,6 @@ location / { root html; - proxy_ignore_headers "Set-Cookie" "Cache-Control" "Expires"; } } """ @@ -192,7 +191,6 @@ def test_filtering(self): def test_quoted_key_value(self): data = nginx.loads(TESTBLOCK_CASE_5) out_data = '\n' + nginx.dumps(data) - print out_data self.assertEqual(out_data, TESTBLOCK_CASE_5)