test_directive.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. from nose.tools import assert_equals, assert_false, assert_true
  2. from tests.asserts import assert_is_instance
  3. from gixy.parser.nginx_parser import NginxParser
  4. from gixy.directives.directive import *
  5. def _get_parsed(config):
  6. root = NginxParser(cwd='', allow_includes=False).parse(config)
  7. return root.children[0]
  8. def test_directive():
  9. config = 'some "foo" "bar";'
  10. directive = _get_parsed(config)
  11. assert_is_instance(directive, Directive)
  12. assert_equals(directive.name, 'some')
  13. assert_equals(directive.args, ['foo', 'bar'])
  14. assert_equals(str(directive), 'some foo bar;')
  15. def test_add_header():
  16. config = 'add_header "X-Foo" "bar";'
  17. directive = _get_parsed(config)
  18. assert_is_instance(directive, AddHeaderDirective)
  19. assert_equals(directive.name, 'add_header')
  20. assert_equals(directive.args, ['X-Foo', 'bar'])
  21. assert_equals(directive.header, 'x-foo')
  22. assert_equals(directive.value, 'bar')
  23. assert_false(directive.always)
  24. assert_equals(str(directive), 'add_header X-Foo bar;')
  25. def test_add_header_always():
  26. config = 'add_header "X-Foo" "bar" always;'
  27. directive = _get_parsed(config)
  28. assert_is_instance(directive, AddHeaderDirective)
  29. assert_equals(directive.name, 'add_header')
  30. assert_equals(directive.args, ['X-Foo', 'bar', 'always'])
  31. assert_equals(directive.header, 'x-foo')
  32. assert_equals(directive.value, 'bar')
  33. assert_true(directive.always)
  34. assert_equals(str(directive), 'add_header X-Foo bar always;')
  35. def test_set():
  36. config = 'set $foo bar;'
  37. directive = _get_parsed(config)
  38. assert_is_instance(directive, SetDirective)
  39. assert_equals(directive.name, 'set')
  40. assert_equals(directive.args, ['$foo', 'bar'])
  41. assert_equals(directive.variable, 'foo')
  42. assert_equals(directive.value, 'bar')
  43. assert_equals(str(directive), 'set $foo bar;')
  44. assert_true(directive.provide_variables)
  45. def test_rewrite():
  46. config = 'rewrite ^ http://some;'
  47. directive = _get_parsed(config)
  48. assert_is_instance(directive, RewriteDirective)
  49. assert_equals(directive.name, 'rewrite')
  50. assert_equals(directive.args, ['^', 'http://some'])
  51. assert_equals(str(directive), 'rewrite ^ http://some;')
  52. assert_true(directive.provide_variables)
  53. assert_equals(directive.pattern, '^')
  54. assert_equals(directive.replace, 'http://some')
  55. assert_equals(directive.flag, None)
  56. def test_rewrite_flags():
  57. config = 'rewrite ^/(.*)$ http://some/$1 redirect;'
  58. directive = _get_parsed(config)
  59. assert_is_instance(directive, RewriteDirective)
  60. assert_equals(directive.name, 'rewrite')
  61. assert_equals(directive.args, ['^/(.*)$', 'http://some/$1', 'redirect'])
  62. assert_equals(str(directive), 'rewrite ^/(.*)$ http://some/$1 redirect;')
  63. assert_true(directive.provide_variables)
  64. assert_equals(directive.pattern, '^/(.*)$')
  65. assert_equals(directive.replace, 'http://some/$1')
  66. assert_equals(directive.flag, 'redirect')
  67. def test_root():
  68. config = 'root /var/www/html;'
  69. directive = _get_parsed(config)
  70. assert_is_instance(directive, RootDirective)
  71. assert_equals(directive.name, 'root')
  72. assert_equals(directive.args, ['/var/www/html'])
  73. assert_equals(str(directive), 'root /var/www/html;')
  74. assert_true(directive.provide_variables)
  75. assert_equals(directive.path, '/var/www/html')