Beispiel #1
0
def test_render():
    url = 'http://httpbin.org/{pa}'
    s = Session()
    s.cache = {'pa': 'post', 'para': '1000', 'dodo': '2000'}
    data = {'replace': '{{ dodo}}'}
    params = {'replace': '{{ para }}'}
    s.post(url, params=params, json=data)
    assert '/post' == s.response.request.path_url.split('?')[0]
    assert '1000' == s.response.request.path_url.split('=')[1]
    assert '{"replace": "2000"}' == s.response.request.body.decode('utf-8')
Beispiel #2
0
def test_render_vars():
    s = Session()
    s.cache = {'token_holder': 'real_token'}
    body = {'data': 'testdata', 'token': '{{ token_holder }}'}
    s.post('http://httpbin.org/post', json=body)
    s.stash(json_query='headers.Host', key='host')
    body = {'context_vars': '{{ host }}'}
    s.post('http://httpbin.org/post', json=body)
Beispiel #3
0
def test_render_func_with_args():
    def deco_func(a, b):
        def func(x):
            return a < x < b

        return func

    s = Session()
    s.register_render(deco_func)
    body = {'data': 'testdata', 'token': 3}
    s.post('http://httpbin.org/post', json=body)
    scm = {'json': {'token': "{% deco_func(1, 5) %}"}}
    s.validate(schema=scm)
Beispiel #4
0
def test_render_func():
    def get_timestamp():
        return int(time.time())

    s = Session()
    s.register_render(get_timestamp)
    body = {'data': 'testdata', 'token': '{% get_timestamp() %}'}
    s.post('http://httpbin.org/post', json=body)
Beispiel #5
0
def test_validate_response_fail():
    url = 'http://httpbin.org/get'
    s = Session()
    s.get(url)
    scm = {
        "ars": {},
        "origin": "{% str %}",
        "url": "http://httpbin.get"
    }
    s.validate(scm)
Beispiel #6
0
def test_validate_response():
    url = 'http://httpbin.org/get'
    s = Session()
    s.get(url)
    s.stash('headers.Host', 'Host')
    scm = {
        "args": {},
        "headers": {
            "Accept": "{% str %}",
            "Accept-Encoding": "{% str %}",
            "Host": "httpbin.org",
            "User-Agent": "{% str %}",
            "X-Amzn-Trace-Id": "{% str %}"
        },
        "origin": "{% str %}",
        "url": "http://httpbin.org/get"
    }
    s.validate(scm)
Beispiel #7
0
def test_validate_response_custom_function():
    def is_url(s):
        return s.startswith('http')
    url = 'http://httpbin.org/get'
    s = Session()
    s.register_render(is_url)
    s.get(url)
    scm = {
        "args": {},
        "headers": {
            "Accept": "{% str %}",
            "Accept-Encoding": "{% str %}",
            "Host": "httpbin.org",
            "User-Agent": "{% str %}",
            "X-Amzn-Trace-Id": "{% str %}"
        },
        "origin": "{% str %}",
        "url": "{% is_url %}"
    }
    s.validate(scm)
Beispiel #8
0
def test_validate_extract_data():
    data = {
        'data_list': [
            {'type': 'A', 'count': 80},
            {'type': 'B', 'count': 90},
            {'type': 'C', 'count': 70},
            {'type': 'D', 'count': 50},
            ]
        }
    url = 'http://httpbin.org/post'
    s = Session()
    s.post(url, json=data)
    scm = ['A', 'B', 'C', 'D']
    s.validate(schema=scm, json_query='json.data_list[].type')
Beispiel #9
0
def test_stash():
    url = 'http://httpbin.org/get'
    s = Session()
    s.get(url)
    s.stash('headers.Host', 'Host')
    assert s.cache['Host'] == 'httpbin.org'