Exemplo n.º 1
0
def test_client_formdata():
    app = Flagon()
    @app.route('/hello')
    def hello():
        return 'ok'

    c = FlagonClient(app)
    r = c.open('/hello', content_type='application/x-www-form-urlencoded', form={'c':1, 'd':'woo'})
    assert r[0] == b'ok'
    assert r[1] == '200 OK'

    files = {
        'a.txt':FileUpload(BytesIO(to_bytes('text default')), 'file1', 'a.txt', headers={'Content-Type': 'text/plain'}),
        'a.html':FileUpload(BytesIO(to_bytes('<!DOCTYPE html><title>Content of a.html.</title>')), 'file2', 'a.html', headers={'Content-Type': 'text/plain'}),
        'b.txt': 'b txt content'
    }
    c = FlagonClient(app)
    r = c.open('/hello', content_type='multipart/form-data',
                form={'text':'text default'},
                files=files)
    assert r[0] == b'ok'
    assert r[1] == '200 OK'

    c = FlagonClient(app)
    r = c.open('/hello', content_type='text/plain',
                input_stream = BytesIO())
    assert r[0] == b'ok'
    assert r[1] == '200 OK'
Exemplo n.º 2
0
def test_fileupload():
    f = FileUpload(BytesIO(to_bytes('a'*256)), 'a.txt', 'a a a.txt', headers={'Content-Type': 'text/plain', 'Content-Length': 256})
    dstpath = '%s/%s'%(tempfile.mkdtemp(), f.name)
    f.save(dstpath)
    with open(dstpath, 'rb') as df:
        assert df.read() == to_bytes('a'*256)
    try:
        os.unlink(dstpath)
    except:
        raise
Exemplo n.º 3
0
def _test_chunked(body, expect):
    env = dict(copy.deepcopy(env1))
    env['wsgi.input'] = BytesIO(to_bytes(body))
    env['HTTP_TRANSFER_ENCODING'] = 'chunked'
    env['QUERY_STRING'] = ''
    req = Request(env)
    assert req.chunked == True
    if inspect.isclass(expect) and issubclass(expect, Exception):
        with pytest.raises(BadRequest):
            req.get_data()
    else:
        assert req.data == to_bytes(expect)
Exemplo n.º 4
0
def test_multipart():
    env = dict(copy.deepcopy(env1))
    form_data = '''-----------------------------9051914041544843365972754266
Content-Disposition: form-data; name="text"

text default
-----------------------------9051914041544843365972754266
Content-Disposition: form-data; name="file1"; filename="a.txt"
Content-Type: text/plain

Content of a.txt.

-----------------------------9051914041544843365972754266
Content-Disposition: form-data; name="file2"; filename="a.html"
Content-Type: text/html

<!DOCTYPE html><title>Content of a.html.</title>

-----------------------------9051914041544843365972754266--
'''
    env['CONTENT_TYPE'] = 'multipart/form-data; boundary=---------------------------9051914041544843365972754266'
    env['wsgi.input'] = BytesIO(to_bytes(form_data))
    env['CONTENT_LENGTH'] = len(form_data)
    env['QUERY_STRING'] = ''
    req = Request(env)
    assert req.args == MultiDict()
    assert req.form == FormsDict({'text':'text default'}.items())
    assert req.values == MultiDict({'text':'text default'}.items())
    a_txt = req.files['file1']
    a_html = req.files['file2']
    assert a_txt.filename == 'a.txt'
    assert a_txt.headers['Content-Type'] == 'text/plain'
    assert a_html.filename == 'a.html'
    assert a_html.headers['Content-Type'] == 'text/html'
    assert req.close() == None
Exemplo n.º 5
0
def test_json_forged_header_issue616():
    test = dict(a=5, b='test', c=[1,2,3])
    env = dict(copy.deepcopy(env1))
    env['CONTENT_TYPE'] = 'text/plain;application/json'
    env['wsgi.input'] = BytesIO(to_bytes(json.dumps(test)))
    env['CONTENT_LENGTH'] = str(len(json.dumps(test)))
    r = Request(env)
    assert r.json == None
Exemplo n.º 6
0
def test_json_valid():
    """ Environ: Request.json property. """
    test = dict(a=5, b='test', c=[1,2,3])
    env = dict(copy.deepcopy(env1))
    env['CONTENT_TYPE'] = 'application/json; charset=UTF-8'
    env['wsgi.input'] = BytesIO(to_bytes(json.dumps(test)))
    env['CONTENT_LENGTH'] = str(len(json.dumps(test)))
    r = Request(env)
    assert r.json == test
Exemplo n.º 7
0
def test_auth():
    user, pwd = 'marc', 'secret'
    basic = to_unicode(base64.b64encode(to_bytes('%s:%s' % (user, pwd))))
    env = dict(copy.deepcopy(env1))
    r = Request(env)
    assert r.authorization == None
    env['HTTP_AUTHORIZATION'] = 'basic %s' % basic
    r = Request(env)
    assert r.authorization == (user, pwd)
Exemplo n.º 8
0
def test_unicode_route():
    app = Flagon()

    @app.route(u'/地球')
    def hello():
        return u'你好地球'

    c = FlagonClient(app)
    r = c.open(u'/地球')
    assert r[0] == to_bytes(u'你好地球')
    assert r[1] == '200 OK'
Exemplo n.º 9
0
def test_run():
    p = None
    for port in range(18800, 18900):
        r, p = start_server(port)
        if r == 'ok' and p:
            break
    assert to_bytes('ok') == fetch(port, 'test')
    os.kill(p.pid, signal.SIGTERM)
    while p.poll() == None:
        os.kill(p.pid, signal.SIGTERM)
        time.sleep(1)
Exemplo n.º 10
0
def test_form_data():
    env = dict(copy.deepcopy(env1))
    form_data = 'c=1&d=woo'
    env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded'
    env['wsgi.input'] = BytesIO(to_bytes(form_data))
    env['CONTENT_LENGTH'] = len(form_data)
    req = Request(env)
    assert req.input_stream == env['wsgi.input']
    assert req.args == MultiDict({'a':'1', 'b':'2'}.items())
    assert req.form == FormsDict({'c':'1', 'd':'woo'}.items())
    assert req.form.c == '1'
    assert req.form.d == 'woo'
    assert req.values == MultiDict({'a':'1', 'b':'2', 'c':'1', 'd':'woo'}.items())
Exemplo n.º 11
0
def test_wsgiheaders():
    env = {
        'REQUEST_METHOD':       'POST',
        'SCRIPT_NAME':          '/foo',
        'PATH_INFO':            '/bar',
        'QUERY_STRING':         'a=1&b=2',
        'SERVER_NAME':          'test.flagon.org',
        'SERVER_PORT':          '80',
        'HTTP_HOST':            'test.flagon.org',
        'SERVER_PROTOCOL':      'http',
        'CONTENT_TYPE':         'text/plain; charset=utf-8',
        'CONTENT_LENGTH':       '0',
        'wsgi.url_scheme':      'http',
        'HTTP_X_FORWARDED_FOR': '5.5.5.5',
    }
    user, pwd = 'marc', 'secret'
    basic = to_unicode(base64.b64encode(to_bytes('%s:%s' % (user, pwd))))
    env['HTTP_AUTHORIZATION'] = 'basic %s' % basic
    env['HTTP_COOKIE'] = 'a=a; a=b'
    w = WSGIHeaders(env)
    assert w.raw('authorization') == 'basic %s' % basic
    assert w.raw('content_type') == 'text/plain; charset=utf-8'
    assert w.raw('cookie') == 'a=a; a=b'
    assert w.raw('range') == None

    assert w['authorization'] == 'basic %s' % basic
    assert w['content_type'] == 'text/plain; charset=utf-8'

    with pytest.raises(TypeError):
        w['content_type'] = 'text/plain'

    with pytest.raises(TypeError):
        del w['range']

    assert len(w) == len(w.keys())
    assert len(w) == 6
Exemplo n.º 12
0
def test_basic_error():
    env = dict(copy.deepcopy(env1))
    env['wsgi.input'] = BytesIO(to_bytes('a'*20))
    env['CONTENT_LENGTH'] = '20a'
    req = Request(env)
    assert req.content_length == 0