123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- from nose.tools import assert_equals, assert_true, assert_false
- from tests.asserts import assert_is_instance, assert_is_none, assert_is_not_none
- from gixy.parser.nginx_parser import NginxParser
- from gixy.directives.block import *
- # TODO(buglloc): what about include block?
- def _get_parsed(config):
- root = NginxParser(cwd='', allow_includes=False).parse(config)
- return root.children[0]
- def test_block():
- config = 'some {some;}'
- directive = _get_parsed(config)
- assert_is_instance(directive, Block)
- assert_true(directive.is_block)
- assert_true(directive.self_context)
- assert_false(directive.provide_variables)
- def test_http():
- config = '''
- http {
- default_type application/octet-stream;
- sendfile on;
- keepalive_timeout 65;
- }
- '''
- directive = _get_parsed(config)
- assert_is_instance(directive, HttpBlock)
- assert_true(directive.is_block)
- assert_true(directive.self_context)
- assert_false(directive.provide_variables)
- def test_server():
- config = '''
- server {
- listen 80;
- server_name _;
- server_name cool.io;
- }
- '''
- directive = _get_parsed(config)
- assert_is_instance(directive, ServerBlock)
- assert_true(directive.is_block)
- assert_true(directive.self_context)
- assert_equals([d.args[0] for d in directive.get_names()], ['_', 'cool.io'])
- assert_false(directive.provide_variables)
- def test_location():
- config = '''
- location / {
- }
- '''
- directive = _get_parsed(config)
- assert_is_instance(directive, LocationBlock)
- assert_true(directive.is_block)
- assert_true(directive.self_context)
- assert_true(directive.provide_variables)
- assert_is_none(directive.modifier)
- assert_equals(directive.path, '/')
- assert_false(directive.is_internal)
- def test_location_internal():
- config = '''
- location / {
- internal;
- }
- '''
- directive = _get_parsed(config)
- assert_is_instance(directive, LocationBlock)
- assert_true(directive.is_internal)
- def test_location_modifier():
- config = '''
- location = / {
- }
- '''
- directive = _get_parsed(config)
- assert_is_instance(directive, LocationBlock)
- assert_equals(directive.modifier, '=')
- assert_equals(directive.path, '/')
- def test_if():
- config = '''
- if ($some) {
- }
- '''
- directive = _get_parsed(config)
- assert_is_instance(directive, IfBlock)
- assert_true(directive.is_block)
- assert_false(directive.self_context)
- assert_false(directive.provide_variables)
- assert_equals(directive.variable, '$some')
- assert_is_none(directive.operand)
- assert_is_none(directive.value)
- def test_if_modifier():
- config = '''
- if (-f /some) {
- }
- '''
- directive = _get_parsed(config)
- assert_is_instance(directive, IfBlock)
- assert_equals(directive.operand, '-f')
- assert_equals(directive.value, '/some')
- assert_is_none(directive.variable)
- def test_if_variable():
- config = '''
- if ($http_some = '/some') {
- }
- '''
- directive = _get_parsed(config)
- assert_is_instance(directive, IfBlock)
- assert_equals(directive.variable, '$http_some')
- assert_equals(directive.operand, '=')
- assert_equals(directive.value, '/some')
- def test_block_some_flat():
- config = '''
- some {
- default_type application/octet-stream;
- sendfile on;
- if (-f /some/) {
- keepalive_timeout 65;
- }
- }
- '''
- directive = _get_parsed(config)
- for d in ['default_type', 'sendfile', 'keepalive_timeout']:
- c = directive.some(d, flat=True)
- assert_is_not_none(c)
- assert_equals(c.name, d)
- def test_block_some_not_flat():
- config = '''
- some {
- default_type application/octet-stream;
- sendfile on;
- if (-f /some/) {
- keepalive_timeout 65;
- }
- }
- '''
- directive = _get_parsed(config)
- c = directive.some('keepalive_timeout', flat=False)
- assert_is_none(c)
- def test_block_find_flat():
- config = '''
- some {
- directive 1;
- if (-f /some/) {
- directive 2;
- }
- }
- '''
- directive = _get_parsed(config)
- finds = directive.find('directive', flat=True)
- assert_equals(len(finds), 2)
- assert_equals([x.name for x in finds], ['directive', 'directive'])
- assert_equals([x.args[0] for x in finds], ['1', '2'])
- def test_block_find_not_flat():
- config = '''
- some {
- directive 1;
- if (-f /some/) {
- directive 2;
- }
- }
- '''
- directive = _get_parsed(config)
- finds = directive.find('directive', flat=False)
- assert_equals(len(finds), 1)
- assert_equals([x.name for x in finds], ['directive'])
- assert_equals([x.args[0] for x in finds], ['1'])
|