Exemple #1
0
def test_auth():
    user, pwd = 'marc', 'secret'
    basic = base64.b64encode(to_bytes('%s:%s' % (user, pwd)))
    scope = dict(copy.deepcopy(scope1))
    r = Request(scope, None)
    assert r.authorization == None
    scope['headers'].append([b'authorization',  b'basic ' + basic])
    r = Request(scope, None)
    assert r.authorization == (user, pwd)
Exemple #2
0
def test_basic_request2():
    scope = dict(copy.deepcopy(scope1))
    scope['headers'].append([b'x_forwarded_host', b'test.callflow.org'])
    req = Request(scope, None)
    assert req.host == 'test.callflow.org'
    scope = dict(copy.deepcopy(scope1))
    scope['headers'].append([b'x_forwarded_host', b'test.callflow.org, a.proxy.org'])
    req = Request(scope, None)
    assert req.host == 'test.callflow.org'
    scope = dict(copy.deepcopy(scope1))
    req = Request(scope, None)
    assert req.host == 'test.callflow.org'
Exemple #3
0
def test_cookie_dict():
    """ Environ: Cookie dict """
    t = dict()
    t[b'a=a']      = {'a': 'a'}
    t[b'a=a; b=b'] = {'a': 'a', 'b':'b'}
    t[b'a=a; a=b'] = {'a': 'b'}
    for k, v in t.items():
        scope = dict(copy.deepcopy(scope1))
        scope['headers'].append([b'cookie', k])
        req = Request(scope, None)
        for n in v:
            assert v[n] == req.cookies[n]
            assert v[n] == req.get_cookie(n)
Exemple #4
0
def test_basic_request():
    scope = dict(copy.deepcopy(scope1))
    scope['headers'].append([b'content-type', b'text/plain; charset=utf-8'])
    req = Request(scope, None)
    assert req.args == MultiDict({'a':'1', 'b':'2'}.items())
    assert req.path == '/bar'
    assert req.full_path == '/foo/bar'
    assert req.script_root == '/foo'
    assert req.url == 'http://test.callflow.org/foo/bar?a=1&b=2'
    assert req.base_url == 'http://test.callflow.org/foo/bar'
    assert req.root_url == 'http://test.callflow.org/foo/'
    assert req.host_url == 'http://test.callflow.org/'
    assert req.host == 'test.callflow.org'
    assert req.blueprint == None
    assert req.mimetype == 'text/plain'
    assert req.mimetype_params == {'charset': 'utf-8'}
    assert req.content_length == 0
    assert req.authorization == None
    assert req.cookies == FormsDict()
    assert list(req.range) == []
    assert req.is_xhr == False
    assert req.is_secure == False
    assert req.if_modified_since == None
    assert req.if_unmodified_since == None
    assert req.access_route == ['172.29.0.10']
    assert req.remote_addr == '172.29.0.10'
    assert req.chunked == False
    assert req.method == "POST"
    assert req.url_charset == 'utf-8'
    assert "Content-Type" in req.headers
Exemple #5
0
def test_remote_route():
    ips = [b'1.2.3.4', b'2.3.4.5', b'3.4.5.6']
    scope = dict(copy.deepcopy(scope1))
    scope['headers'].append([b'x_forwarded_for', b', '.join(ips)])
    r = Request(scope, None)
    assert r.remote_route == list(map(lambda x: to_unicode(x), ips))

    scope = dict(copy.deepcopy(scope1))
    scope['headers'].append([b'x_forwarded_for', b', '.join(ips)])
    scope['client'] = (to_unicode(ips[1]), 1212)
    r = Request(scope, None)
    assert r.remote_route == list(map(lambda x: to_unicode(x), ips))

    scope = dict(copy.deepcopy(scope1))
    scope['client'] = (to_unicode(ips[1]), 1212)
    r = Request(scope, None)
    assert r.remote_route == [to_unicode(ips[1]),]
Exemple #6
0
async def test_request_json():
    scope = dict(copy.deepcopy(scope1))
    scope['headers'].append([b'content_type', b'application/json'])
    req = Request(scope, receive=json_data_receive)
    json_data = await req.json()
    assert len(json_data) == 2
    assert json_data['hello'] == 'world'
    assert json_data['code'] == 200
Exemple #7
0
def test_remote_addr():
    ips = [b'1.2.3.4', b'2.3.4.5', b'3.4.5.6']
    scope = dict(copy.deepcopy(scope1))
    scope['headers'].append([b'x-forwarded-for', b', '.join(ips)])
    r = Request(scope, None)
    assert r.remote_addr == to_unicode(ips[0])

    scope = dict(copy.deepcopy(scope1))
    scope['headers'].append([b'x-forwarded-for', b', '.join(ips)])
    scope['client'] = (to_unicode(ips[1]), 1212)
    r = Request(scope, None)
    assert r.remote_addr == to_unicode(ips[0])

    scope = dict(copy.deepcopy(scope1))
    scope['headers'].append([b'remote-addr', ips[1]])
    scope['client'] = (to_unicode(ips[1]), 1212)
    r = Request(scope, None)
    assert r.remote_addr == to_unicode(ips[1])
Exemple #8
0
async def test_request_form():
    scope = dict(copy.deepcopy(scope1))
    scope['headers'].append([b'content_type', b'application/x-www-form-urlencoded'])
    req = Request(scope, receive=form_data_receive)
    form_data = await req.form()
    form_data2 = await req.form()
    assert len(form_data) == 2
    assert form_data['hello'] == 'world'
    assert form_data['code'] == '200'
    assert len(form_data2) == 2
    assert form_data2['hello'] == 'world'
    assert form_data2['code'] == '200'
Exemple #9
0
def test_basic_error():
    scope = dict(copy.deepcopy(scope1))
    scope['headers'].append([b'content-length', b'20a'])
    req = Request(scope, None)
    assert req.content_length == 0