コード例 #1
0
ファイル: test_response.py プロジェクト: nickfrostatx/malt
def test_response_headers():
    response = Response()
    assert list(response.headers) == [('Content-Type',
                                       'text/plain; charset=utf-8')]
    response.headers[u'X-Powered-By'] = [b'Coffee', u'Ramen']
    assert list(response.headers) == [
        ('Content-Type', 'text/plain; charset=utf-8'),
        ('X-Powered-By', 'Coffee'), ('X-Powered-By', 'Ramen')]

    assert response.headers[u'Content-Type'] == u'text/plain; charset=utf-8'
    assert response.headers[u'X-Powered-By'] == b'Coffee'

    del response.headers[b'x-powered-by']
    for key in (u'X-Powered-By', u'X-Abc'):
        with pytest.raises(KeyError) as exc_info:
            response.headers[key]
        assert exc_info.value.args[0] == key

    response.headers.add(u'X-Snoop-Options', u'nosnoop')
    response.headers.add(b'Set-Cookie', u'a=b')
    response.headers.add(u'set-cookie', b'c=d')
    assert list(response.headers) == [
        ('Content-Type', 'text/plain; charset=utf-8'),
        ('X-Snoop-Options', 'nosnoop'),
        ('Set-Cookie', 'a=b'), ('Set-Cookie', 'c=d')]

    with pytest.raises(TypeError) as exc_info:
        response.headers.add(u'Set-Cookie', ['abc'])
    assert exc_info.value.args[0] == 'Headers.add does not expect a list'
コード例 #2
0
def test_response_headers():
    response = Response()
    assert list(response.headers) == [('Content-Type',
                                       'text/plain; charset=utf-8')]
    response.headers[u'X-Powered-By'] = [b'Coffee', u'Ramen']
    assert list(response.headers) == [('Content-Type',
                                       'text/plain; charset=utf-8'),
                                      ('X-Powered-By', 'Coffee'),
                                      ('X-Powered-By', 'Ramen')]

    assert response.headers[u'Content-Type'] == u'text/plain; charset=utf-8'
    assert response.headers[u'X-Powered-By'] == b'Coffee'

    del response.headers[b'x-powered-by']
    for key in (u'X-Powered-By', u'X-Abc'):
        with pytest.raises(KeyError) as exc_info:
            response.headers[key]
        assert exc_info.value.args[0] == key

    response.headers.add(u'X-Snoop-Options', u'nosnoop')
    response.headers.add(b'Set-Cookie', u'a=b')
    response.headers.add(u'set-cookie', b'c=d')
    assert list(response.headers) == [('Content-Type',
                                       'text/plain; charset=utf-8'),
                                      ('X-Snoop-Options', 'nosnoop'),
                                      ('Set-Cookie', 'a=b'),
                                      ('Set-Cookie', 'c=d')]

    with pytest.raises(TypeError) as exc_info:
        response.headers.add(u'Set-Cookie', ['abc'])
    assert exc_info.value.args[0] == 'Headers.add does not expect a list'
コード例 #3
0
def test_alternate_charsets():
    assert list(Response(u'böse', charset='latin')) == [b'b\xf6se']

    with pytest.raises(UnicodeEncodeError):
        list(Response(u'こんにちは', charset='latin'))

    assert list(Response(u'こんにちは', charset='euc-jp')) == [
        b'\xa4\xb3\xa4\xf3\xa4\xcb\xa4\xc1\xa4\xcf'
    ]
コード例 #4
0
def test_status_invalid():
    with pytest.raises(ValueError) as exc:
        Response(code=200.)
    assert 'Invalid status: 200.0' in str(exc)

    for code in (100., '', '200', -15, 0, 199, 600):
        with pytest.raises(ValueError) as exc:
            Response().status_code = code
        assert 'Invalid status: ' + repr(code) in str(exc)
コード例 #5
0
def test_set_cookie():
    response = Response()
    assert list(response.headers) == [('Content-Type',
                                       'text/plain; charset=utf-8')]

    response.set_cookie(u'a', u'1')
    response.set_cookie('b', '2')
    response.set_cookie('empty_value', '')
    response.set_cookie('key_only')
    assert list(response.headers)[1:] == [
        ('Set-Cookie', 'a=1'),
        ('Set-Cookie', 'b=2'),
        ('Set-Cookie', 'empty_value='),
        ('Set-Cookie', 'key_only'),
    ]
コード例 #6
0
    def secretly_bad_response():
        def gen():
            for i in range(10):
                if i == 9:
                    raise Exception()
                yield str(i)

        return Response(gen())
コード例 #7
0
def test_save_session():
    req = Request({}, {'SECRET_KEY': b'abc'})
    req.session = {u'a': 1}
    resp = Response()

    save_session(req, resp)
    expected = u'eyJhIjoxfQ.2XFQKMS-erhoKkSGsezDxFsim6YctUnzxaiiMP1wzFs'
    assert resp.headers['Set-Cookie'] == u'session=' + expected
コード例 #8
0
ファイル: test_response.py プロジェクト: nickfrostatx/malt
def test_set_cookie():
    response = Response()
    assert list(response.headers) == [('Content-Type',
                                       'text/plain; charset=utf-8')]

    response.set_cookie(u'a', u'1')
    response.set_cookie('b', '2')
    response.set_cookie('empty_value', '')
    response.set_cookie('key_only')
    assert list(response.headers)[1:] == [
        ('Set-Cookie', 'a=1'),
        ('Set-Cookie', 'b=2'),
        ('Set-Cookie', 'empty_value='),
        ('Set-Cookie', 'key_only'),
    ]
コード例 #9
0
def create_task(request):
    data = request.json()
    try:
        name = data['name']
    except (KeyError, TypeError):
        raise HTTPException(400, 'Missing task name')

    task = {'id': len(tasks), 'name': name}
    tasks.append(task)

    return Response('/tasks/{0:d}'.format(task['id']))
コード例 #10
0
def test_status_text():
    assert Response().status == '200 OK'
    assert Response(code=100).status == '100 Continue'
    assert Response(code=418).status == '418 I\'m a teapot'
コード例 #11
0
def thing(request, thing_id):
    thing_id = int(thing_id)
    return Response('Thing {0:d}\n'.format(thing_id))
コード例 #12
0
def print_url(request):
    return Response(request.url + '\n')
コード例 #13
0
def handle(error):
    return Response('%s\n' % error.message, code=error.status_code)
コード例 #14
0
def root(request):
    return Response('Hello World!\n')
コード例 #15
0
def before(request):
    if request.path == '/before':
        return Response(u'before_request returned\n')
コード例 #16
0
def after(request, response):
    if request.path == '/after':
        return Response(u'after_request returned\n')
    return response
コード例 #17
0
def test_response_iterable():
    assert Response('abc').response == ['abc']
    assert Response([b'abc', 'déf']).response == [b'abc', 'déf']
コード例 #18
0
 def handle(exc):
     return Response('%d\n%s' % (exc.status_code, exc.message),
                     code=exc.status_code)
コード例 #19
0
def test_response_text():
    assert list(Response(b'Some text')) == [b'Some text']
    assert list(Response(u'Some text')) == [b'Some text']
    assert list(Response([b'abc', 'déf'])) == [b'abc', b'd\xc3\xa9f']
コード例 #20
0
def test_unicode_response():
    assert list(Response(u'こんにちは')) == [
        b'\xe3\x81\x93\xe3\x82\x93\xe3\x81\xab\xe3\x81\xa1\xe3\x81\xaf'
    ]
コード例 #21
0
def test_status_code():
    assert Response('').status_code == 200
    assert Response('', 418).status_code == 418