Beispiel #1
0
def test_lex_lua_block_tricky():
    dirname = os.path.join(tests_dir, 'configs', 'lua-block-tricky')
    config = os.path.join(dirname, 'nginx.conf')
    tokens = crossplane.lex(config)
    assert list((token, line) for token, line, quoted in tokens) == [
        ('http', 1), ('{', 1), ('server', 2), ('{', 2), ('listen', 3),
        ('127.0.0.1:8080', 3), (';', 3), ('server_name', 4),
        ('content_by_lua_block', 4), (';', 4),
        ("# make sure this doesn't trip up lexers", 4),
        ('set_by_lua_block', 5), ('$res', 5),
        (' -- irregular lua block directive'
         '\n            local a = 32'
         '\n            local b = 56'
         '\n'
         '\n            ngx.var.diff = a - b;  -- write to $diff directly'
         '\n            return a + b;          -- return the $sum value normally'
         '\n        ', 11), (';', 11), ('rewrite_by_lua_block', 12),
        (' -- have valid braces in Lua code and quotes around directive'
         '\n            do_something("hello, world!\\nhiya\\n")'
         '\n            a = { 1, 2, 3 }'
         '\n            btn = iup.button({title="ok"})'
         '\n        ', 16), (';', 16), ('}', 17), ('upstream', 18),
        ('content_by_lua_block', 18), ('{', 18), ('# stuff', 19), ('}', 20),
        ('}', 21)
    ]
Beispiel #2
0
def test_messy_config():
    dirname = os.path.join(here, 'configs', 'messy')
    config = os.path.join(dirname, 'nginx.conf')
    tokens = crossplane.lex(config)
    assert list(tokens) == [
        ('user', 1), ('nobody', 1), (';', 1), ('events', 3), ('{', 3),
        ('worker_connections', 3), ('2048', 3), (';', 3), ('}', 3),
        ('http', 5), ('{', 5), ('access_log', 7), ('off', 7), (';', 7),
        ('default_type', 7), ('text/plain', 7), (';', 7), ('error_log', 7),
        ('off', 7), (';', 7), ('server', 8), ('{', 8), ('listen', 9),
        ('8083', 9), (';', 9), ('return', 10), ('200', 10),
        ('Ser" \' \' ver\\\\ \\ $server_addr:\\$server_port\\n\\nTime: $time_local\\n\\n',
         10), (';', 10), ('}', 11), ('server', 12), ('{', 12), ('listen', 12),
        ('8080', 12), (';', 12), ('root', 13), ('/usr/share/nginx/html', 13),
        (';', 13), ('location', 14), ('~', 14),
        ('/hello/world;', 14), ('{', 14), ('return', 14), ('301', 14),
        ('/status.html', 14), (';', 14), ('}', 14), ('location', 15),
        ('/foo', 15), ('{', 15), ('}', 15), ('location', 15), ('/bar', 15),
        ('{', 15), ('}', 15), ('location', 16), ('/\\{\\;\\}\\ #\\ ab', 16),
        ('{', 16), ('}', 16), ('if', 17), ('($request_method', 17), ('=', 17),
        ('P\\{O\\)\\###\\;ST', 17), (')', 17), ('{', 17), ('}', 17),
        ('location', 18), ('/status.html', 18), ('{', 18), ('try_files', 19),
        ('/abc/${uri} /abc/${uri}.html', 19), ('=404', 19),
        (';', 19), ('}', 20), ('location', 21),
        ('/sta;\n                    tus', 21), ('{', 22), ('return', 22),
        ('302', 22), ('/status.html', 22), (';', 22), ('}', 22),
        ('location', 23), ('/upstream_conf', 23), ('{', 23), ('return', 23),
        ('200', 23), ('/status.html', 23), (';', 23), ('}', 23), ('}', 23),
        ('server', 24), ('{', 25), ('}', 25), ('}', 25)
    ]
Beispiel #3
0
def test_lex_lua_block_simple():
    dirname = os.path.join(tests_dir, 'configs', 'lua-block-simple')
    config = os.path.join(dirname, 'nginx.conf')
    tokens = crossplane.lex(config)
    assert list((token, line) for token, line, quoted in tokens) == [
        ('http', 1), ('{', 1), ('init_by_lua_block', 2),
        ('\n        print("Lua block code with curly brace str {")\n    ', 4),
        (';', 4), ('init_worker_by_lua_block', 5),
        ('\n        print("Work that every worker")\n    ', 7), (';', 7),
        ('body_filter_by_lua_block', 8),
        ('\n        local data, eof = ngx.arg[1], ngx.arg[2]\n    ', 10),
        (';', 10), ('header_filter_by_lua_block', 11),
        ('\n        ngx.header["content-length"] = nil\n    ', 13), (';', 13),
        ('server', 14), ('{', 14), ('listen', 15), ('127.0.0.1:8080', 15),
        (';', 15), ('location', 16), ('/', 16), ('{', 16),
        ('content_by_lua_block', 17),
        ('\n                ngx.say("I need no extra escaping here, for example: \\r\\nblah")\n            ',
         19), (';', 19), ('return', 20), ('200', 20), ('foo bar baz', 20),
        (';', 20), ('}', 21), ('ssl_certificate_by_lua_block', 22),
        ('\n            print("About to initiate a new SSL handshake!")\n        ',
         24), (';', 24), ('location', 25), ('/a', 25), ('{', 25),
        ('client_max_body_size', 26), ('100k', 26), (';', 26),
        ('client_body_buffer_size', 27), ('100k', 27), (';', 27), ('}', 28),
        ('}', 29), ('upstream', 31), ('foo', 31), ('{', 31), ('server', 32),
        ('127.0.0.1', 32), (';', 32), ('balancer_by_lua_block', 33),
        ('\n            -- use Lua to do something interesting here\n        ',
         35), (';', 35), ('log_by_lua_block', 36),
        ('\n            print("I need no extra escaping here, for example: \\r\\nblah")\n        ',
         38), (';', 38), ('}', 39), ('}', 40)
    ]
Beispiel #4
0
def test_simple_config():
    dirname = os.path.join(here, 'configs', 'simple')
    config = os.path.join(dirname, 'nginx.conf')
    tokens = crossplane.lex(config)
    assert list(tokens) == [('events', 1), ('{', 1), ('worker_connections', 2),
                            ('1024', 2), (';', 2), ('}', 3), ('http', 5),
                            ('{', 5), ('server', 6), ('{', 6), ('listen', 7),
                            ('127.0.0.1:8080', 7), (';', 7),
                            ('server_name', 8), ('default_server', 8),
                            (';', 8), ('location', 9), ('/', 9), ('{', 9),
                            ('return', 10), ('200', 10), ('foo bar baz', 10),
                            (';', 10), ('}', 11), ('}', 12), ('}', 13)]
Beispiel #5
0
def test_quoted_right_brace():
    dirname = os.path.join(here, 'configs', 'quoted-right-brace')
    config = os.path.join(dirname, 'nginx.conf')
    tokens = crossplane.lex(config)
    assert list(token for token, line, quoted in tokens) == [
        'events', '{', '}', 'http', '{', 'log_format', 'main', 'escape=json',
        '{ "@timestamp": "$time_iso8601", ', '"server_name": "$server_name", ',
        '"host": "$host", ', '"status": "$status", ',
        '"request": "$request", ', '"uri": "$uri", ', '"args": "$args", ',
        '"https": "$https", ', '"request_method": "$request_method", ',
        '"referer": "$http_referer", ', '"agent": "$http_user_agent"', '}',
        ';', '}'
    ]
Beispiel #6
0
def test_with_config_comments():
    dirname = os.path.join(here, 'configs', 'with-comments')
    config = os.path.join(dirname, 'nginx.conf')
    tokens = crossplane.lex(config)
    assert list((token, line) for token, line, quoted in tokens) == [
        ('events', 1), ('{', 1), ('worker_connections', 2), ('1024', 2),
        (';', 2), ('}', 3), ('#comment', 4), ('http', 5), ('{', 5),
        ('server', 6), ('{', 6), ('listen', 7), ('127.0.0.1:8080', 7),
        (';', 7), ('#listen', 7), ('server_name', 8), ('default_server', 8),
        (';', 8), ('location', 9), ('/', 9), ('{', 9), ('## this is brace', 9),
        ('# location /', 10), ('return', 11), ('200', 11), ('foo bar baz', 11),
        (';', 11), ('}', 12), ('}', 13), ('}', 14)
    ]
Beispiel #7
0
def test_lex_lua_block_larger():
    dirname = os.path.join(tests_dir, 'configs', 'lua-block-larger')
    config = os.path.join(dirname, 'nginx.conf')
    tokens = crossplane.lex(config)
    assert list((token, line) for token, line, quoted in tokens) == [
        ('http', 1),
        ('{', 1),
        ('content_by_lua_block', 2),
        (
            '\n        ngx.req.read_body()  -- explicitly read the req body'
            '\n        local data = ngx.req.get_body_data()'
            '\n        if data then'
            '\n            ngx.say("body data:")'
            '\n            ngx.print(data)'
            '\n            return'
            '\n        end'
            '\n'
            '\n        -- body may get buffered in a temp file:'
            '\n        local file = ngx.req.get_body_file()'
            '\n        if file then'
            '\n            ngx.say("body is in file ", file)'
            '\n        else'
            '\n            ngx.say("no body found")'
            '\n        end'
            '\n    ',
            18
        ),
        (';', 18),
        ('access_by_lua_block', 19),
        (
            '\n        -- check the client IP address is in our black list'
            '\n        if ngx.var.remote_addr == "132.5.72.3" then'
            '\n            ngx.exit(ngx.HTTP_FORBIDDEN)'
            '\n        end'
            '\n'
            '\n        -- check if the URI contains bad words'
            '\n        if ngx.var.uri and'
            '\n               string.match(ngx.var.request_body, "evil")'
            '\n        then'
            '\n            return ngx.redirect("/terms_of_use.html")'
            '\n        end'
            '\n'
            '\n        -- tests passed'
            '\n    ',
            33
        ),
        (';', 33),
        ('}', 34)
    ]
Beispiel #8
0
def test_quote_behavior():
    dirname = os.path.join(here, 'configs', 'quote-behavior')
    config = os.path.join(dirname, 'nginx.conf')
    tokens = crossplane.lex(config)
    assert list(token for token, line, quoted in tokens) == [
        'outer-quote',
        'left',
        '-quote',
        'right-"quote"',
        'inner"-"quote',
        ';',
        '',
        '',
        'left-empty',
        'right-empty""',
        'inner""empty',
        'right-empty-single"',
        ';',
    ]