Beispiel #1
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
Beispiel #2
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
Beispiel #3
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
Beispiel #4
0
def test_client_formdata():
    app = Cocopot()

    @app.route('/hello')
    def hello():
        return 'ok'

    c = CocopotClient(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 = CocopotClient(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 = CocopotClient(app)
    r = c.open('/hello', content_type='text/plain', input_stream=BytesIO())
    assert r[0] == b'ok'
    assert r[1] == '200 OK'
Beispiel #5
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
Beispiel #6
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)
Beispiel #7
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())
Beispiel #8
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