Example #1
0
def test_response():
    req = BaseRequest.blank('/')
    res = req.get_response(simple_app)
    assert res.status == '200 OK'
    assert res.status_int == 200
    assert res.body == "OK"
    assert res.charset == 'utf8'
    assert res.content_type == 'text/html'
    res.status = 404
    assert res.status == '404 Not Found'
    assert res.status_int == 404
    res.body = 'Not OK'
    assert ''.join(res.app_iter) == 'Not OK'
    res.charset = 'iso8859-1'
    assert res.headers['content-type'] == 'text/html; charset=iso8859-1'
    res.content_type = 'text/xml'
    assert res.headers['content-type'] == 'text/xml; charset=iso8859-1'
    res.headers = {'content-type': 'text/html'}
    assert res.headers['content-type'] == 'text/html'
    assert res.headerlist == [('content-type', 'text/html')]
    res.set_cookie('x', 'y')
    assert res.headers['set-cookie'].strip(';') == 'x=y; Path=/'
    res = Response('a body', '200 OK', content_type='text/html')
    res.encode_content()
    assert res.content_encoding == 'gzip'
    assert res.body == '\x1f\x8b\x08\x00\x00\x00\x00\x00\x02\xffKTH\xcaO\xa9\x04\x00\xf6\x86GI\x06\x00\x00\x00'
    res.decode_content()
    assert res.content_encoding is None
    assert res.body == 'a body'
def test_forwarded_middleware():
    @webob.dec.wsgify
    def app(request):
        return Response(body=request.application_url)
    wrapped = forwarded.Middleware(app)

    response = BaseRequest.blank(
        '/foo',
        headers={'Forwarded': 'host=www.example.com'}).get_response(wrapped)
    assert response.body == b'http://www.example.com'
def test_cookies():
    res = Response()
    res.set_cookie('x', u'\N{BLACK SQUARE}') # test unicode value
    eq_(res.headers.getall('set-cookie'), ['x="\\342\\226\\240"; Path=/']) # uft8 encoded
    r2 = res.merge_cookies(simple_app)
    r2 = BaseRequest.blank('/').get_response(r2)
    eq_(r2.headerlist,
        [('Content-Type', 'text/html; charset=utf8'),
        ('Set-Cookie', 'x="\\342\\226\\240"; Path=/'),
        ]
    )
def test_forwarded_handler_error():
    def handle(request):
        pass  # pragma: no cover

    wrapped_handle = forwarded.handler_factory(handle)

    response = wrapped_handle(BaseRequest.blank(
        '/foo',
        headers={'Forwarded': 'blah=www.example.com'}))

    assert response.status_code == 400
def test_forwarded_handler_host_port():
    def handle(request):
        assert request.host == 'www.example.com:8080'
        assert request.host_port == '8080'
        assert request.host_url == 'http://www.example.com:8080'
        assert request.application_url == 'http://www.example.com:8080'
        assert request.path_url == 'http://www.example.com:8080/foo'
        assert request.url == 'http://www.example.com:8080/foo'
        assert request.relative_url('bar') == 'http://www.example.com:8080/bar'
        assert request.domain == 'www.example.com'

    wrapped_handle = forwarded.handler_factory(handle)

    wrapped_handle(BaseRequest.blank(
        '/foo',
        headers={'Forwarded': 'host=www.example.com:8080'}))
def test_forwarded_handler_missing_header():
    def handle(request):
        assert request.scheme == 'http'
        assert request.host == 'example.com'
        assert request.host_port == '80'
        assert request.host_url == 'http://example.com'
        assert request.application_url == 'http://example.com'
        assert request.path_url == 'http://example.com/foo'
        assert request.url == 'http://example.com/foo'
        assert request.relative_url('bar') == 'http://example.com/bar'
        assert request.domain == 'example.com'

    wrapped_handle = forwarded.handler_factory(handle)

    wrapped_handle(BaseRequest.blank(
        '/foo',
        headers={'Host': 'example.com'}))
def test_forwarded_handler_only_proto():
    def handle(request):
        assert request.scheme == 'https'
        assert request.host == 'example.com'
        assert request.host_port == '443'
        assert request.host_url == 'https://example.com'
        assert request.application_url == 'https://example.com'
        assert request.path_url == 'https://example.com/foo'
        assert request.url == 'https://example.com/foo'
        assert request.relative_url('bar') == 'https://example.com/bar'
        assert request.domain == 'example.com'

    wrapped_handle = forwarded.handler_factory(handle)

    wrapped_handle(BaseRequest.blank(
        '/foo',
        headers={'Host': 'example.com', 'Forwarded': 'proto=https'}))
Example #8
0
def test_response():
    req = BaseRequest.blank('/')
    res = req.get_response(simple_app)
    assert res.status == '200 OK'
    assert res.status_int == 200
    assert res.body == "OK"
    assert res.charset == 'utf8'
    assert res.content_type == 'text/html'
    res.status = 404
    assert res.status == '404 Not Found'
    assert res.status_int == 404
    res.body = 'Not OK'
    assert ''.join(res.app_iter) == 'Not OK'
    res.charset = 'iso8859-1'
    assert res.headers['content-type'] == 'text/html; charset=iso8859-1'
    res.content_type = 'text/xml'
    assert res.headers['content-type'] == 'text/xml; charset=iso8859-1'
    res.headers = {'content-type': 'text/html'}
    assert res.headers['content-type'] == 'text/html'
    assert res.headerlist == [('content-type', 'text/html')]
    res.set_cookie('x', 'y')
    assert res.headers['set-cookie'].strip(';') == 'x=y; Path=/'
    res = Response('a body', '200 OK', content_type='text/html')
    res.encode_content()
    assert res.content_encoding == 'gzip'
    eq_(
        res.body,
        '\x1f\x8b\x08\x00\x00\x00\x00\x00\x02\xffKTH\xcaO\xa9\x04\x00\xf6\x86GI\x06\x00\x00\x00'
    )
    res.decode_content()
    assert res.content_encoding is None
    assert res.body == 'a body'
    res.set_cookie('x', u'foo')  # test unicode value
    assert_raises(TypeError, Response, app_iter=iter(['a']), body="somebody")
    del req.environ
    eq_(Response(request=req)._environ, req)
    eq_(Response(request=req)._request, None)
    assert_raises(TypeError, Response, charset=None, body=u"unicode body")
    assert_raises(TypeError, Response, wrong_key='dummy')
Example #9
0
def test_response():
    req = BaseRequest.blank('/')
    res = req.get_response(simple_app)
    assert res.status == '200 OK'
    assert res.status_int == 200
    assert res.body == "OK"
    assert res.charset == 'utf8'
    assert res.content_type == 'text/html'
    res.status = 404
    assert res.status == '404 Not Found'
    assert res.status_int == 404
    res.body = 'Not OK'
    assert ''.join(res.app_iter) == 'Not OK'
    res.charset = 'iso8859-1'
    assert res.headers['content-type'] == 'text/html; charset=iso8859-1'
    res.content_type = 'text/xml'
    assert res.headers['content-type'] == 'text/xml; charset=iso8859-1'
    res.headers = {'content-type': 'text/html'}
    assert res.headers['content-type'] == 'text/html'
    assert res.headerlist == [('content-type', 'text/html')]
    res.set_cookie('x', 'y')
    assert res.headers['set-cookie'].strip(';') == 'x=y; Path=/'
    res = Response('a body', '200 OK', content_type='text/html')
    res.encode_content()
    assert res.content_encoding == 'gzip'
    eq_(res.body, '\x1f\x8b\x08\x00\x00\x00\x00\x00\x02\xffKTH\xcaO\xa9\x04\x00\xf6\x86GI\x06\x00\x00\x00')
    res.decode_content()
    assert res.content_encoding is None
    assert res.body == 'a body'
    res.set_cookie('x', u'foo') # test unicode value
    assert_raises(TypeError, Response, app_iter=iter(['a']),
                  body="somebody")
    del req.environ
    eq_(Response(request=req)._environ, req)
    eq_(Response(request=req)._request, None)
    assert_raises(TypeError, Response, charset=None,
                  body=u"unicode body")
    assert_raises(TypeError, Response, wrong_key='dummy')
Example #10
0
 def test_get_project_context(self):
     test_context = context.RequestContext('user', 'admin', is_admin=True)
     req = BaseRequest({'nova.context': test_context})
     (ctx, proj_id) = util.get_project_context(req)
     self.assertEquals(ctx, test_context, 'Context test util')
     self.assertEquals(proj_id, 'admin')