|
| 1 | +""" |
| 2 | +Testing module for python-nginx. |
| 3 | +
|
| 4 | +python-nginx |
| 5 | +(c) 2016 Jacob Cook |
| 6 | +Licensed under GPLv3, see LICENSE.md |
| 7 | +""" |
| 8 | + |
| 9 | +import nginx |
| 10 | +import unittest |
| 11 | + |
| 12 | + |
| 13 | +TESTBLOCK = """ |
| 14 | +upstream php { |
| 15 | + server unix:/tmp/php-fcgi.socket; |
| 16 | +} |
| 17 | +server { |
| 18 | + listen 80; # This comment should be present; |
| 19 | + # And this one |
| 20 | + server_name localhost 127.0.0.1; |
| 21 | + root /srv/http; # And also this one |
| 22 | + mykey "myvalue; #notme myothervalue"; |
| 23 | + # This one too |
| 24 | + index index.php; |
| 25 | +
|
| 26 | + location ~ \.php(?:$|/) { |
| 27 | + fastcgi_pass php; |
| 28 | + } |
| 29 | +} |
| 30 | +""" |
| 31 | + |
| 32 | +MESSYBLOCK = """ |
| 33 | +# This is an example of a messy config |
| 34 | +upstream php { server unix:/tmp/php-cgi.socket; } |
| 35 | +server { server_name localhost; #this is the server server_name |
| 36 | +location /{ test_key test_value; }} |
| 37 | +""" |
| 38 | + |
| 39 | + |
| 40 | +class TestPythonNginx(unittest.TestCase): |
| 41 | + def test_basic_load(self): |
| 42 | + self.assertTrue(nginx.loads(TESTBLOCK) is not None) |
| 43 | + |
| 44 | + def test_messy_load(self): |
| 45 | + data = nginx.loads(MESSYBLOCK) |
| 46 | + self.assertTrue(data is not None) |
| 47 | + self.assertTrue(len(data.server.comments), 1) |
| 48 | + self.assertTrue(len(data.server.locations), 1) |
| 49 | + |
| 50 | + def test_comment_parse(self): |
| 51 | + data = nginx.loads(TESTBLOCK) |
| 52 | + self.assertEquals(len(data.server.comments), 4) |
| 53 | + self.assertEquals(data.server.comments[2].comment, 'And also this one') |
| 54 | + |
| 55 | + def test_key_parse(self): |
| 56 | + data = nginx.loads(TESTBLOCK) |
| 57 | + self.assertEquals(len(data.server.keys), 5) |
| 58 | + firstKey = data.server.keys[0] |
| 59 | + thirdKey = data.server.keys[3] |
| 60 | + self.assertEquals(firstKey.name, 'listen') |
| 61 | + self.assertEquals(firstKey.value, '80') |
| 62 | + self.assertEquals(thirdKey.name, 'mykey') |
| 63 | + self.assertEquals(thirdKey.value, '"myvalue; #notme myothervalue"') |
| 64 | + |
| 65 | + def test_location_parse(self): |
| 66 | + data = nginx.loads(TESTBLOCK) |
| 67 | + self.assertEquals(len(data.server.locations), 1) |
| 68 | + firstLoc = data.server.locations[0] |
| 69 | + self.assertEquals(firstLoc.value, '~ \.php(?:$|/)') |
| 70 | + self.assertEquals(len(firstLoc.keys), 1) |
| 71 | + |
| 72 | + def test_reflection(self): |
| 73 | + inp_data = nginx.loads(TESTBLOCK) |
| 74 | + out_data = '\n' + nginx.dumps(inp_data) |
| 75 | + self.assertEqual(TESTBLOCK, out_data) |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == '__main__': |
| 79 | + unittest.main() |
0 commit comments