def test_request_cookie__set_item(self): self._REQUEST.headers['COOKIE'] = 'name=value' req = Request('host', self._REQUEST, None, loop=self.loop) self.assertEqual(req.cookies, {'name': 'value'}) with self.assertRaises(TypeError): req.cookies['my'] = 'value'
def test_global_event_loop(self): loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) req = Request('host', self._REQUEST, None) self.assertIs(req._loop, loop) loop.close()
def test_coockie_policy(self): identity_policy = CookieIdentityPolicy() request = Request('host', aiohttp.RawRequestMessage('GET', '/post/123/', '1.1', {}, True, None), None, loop=self.loop) @asyncio.coroutine def f(): user_id = yield from identity_policy.identify(request) self.assertFalse(user_id) yield from identity_policy.remember(request, 'anton') # emulate response-request cycle request._cookies = request.response.cookies.copy() user_id = yield from identity_policy.identify(request) self.assertEqual(user_id.value, 'anton') yield from identity_policy.forget(request) # emulate response-request cycle request._cookies = request.response.cookies.copy() user_id = yield from identity_policy.identify(request) self.assertFalse(user_id.value) self.loop.run_until_complete(f())
def go(): with self.assertRaises(aiohttp.HttpException) as ctx: request = Request('host', aiohttp.RawRequestMessage( 'POST', '/not/found', '1.1', {}, True, None), None, loop=self.loop) yield from self.server.dispatch(request) self.assertEqual(404, ctx.exception.code)
def test_request_cookie(self): self._REQUEST.headers['COOKIE'] = 'cookie1=value1; cookie2=value2' req = Request('host', self._REQUEST, None, loop=self.loop) self.assertEqual(req.cookies, { 'cookie1': 'value1', 'cookie2': 'value2', })
def go(): with self.assertRaises(aiohttp.HttpException) as ctx: request = Request('host', aiohttp.RawRequestMessage( 'DELETE', '/post/123', '1.1', {}, True, None), None, loop=self.loop) yield from self.server.dispatch(request) self.assertEqual(405, ctx.exception.code) self.assertEqual((('Allow', 'GET, POST'),), ctx.exception.headers)
def test_no_request_cookies(self): req = Request('host', aiohttp.RawRequestMessage('GET', '/some/path', '1.1', CIMultiDict(), True, None), None, loop=self.loop) self.assertEqual(req.cookies, {}) cookies = req.cookies self.assertIs(cookies, req.cookies)
def test_dispatch_with_ending_slash(self): def f(id): return {'a': 1, 'b': 2} self.server.add_url('get', '/post/{id}/', f) request = Request('host', aiohttp.RawRequestMessage( 'GET', '/post/123/', '1.1', {}, True, None), None, loop=self.loop) ret, code = self.loop.run_until_complete(self.server.dispatch(request)) # json.loads is required to avoid items order in dict self.assertEqual({"b": 2, "a": 1}, json.loads(ret)) self.assertEqual(200, code)
def test_dispatch_with_ending_slash_not_found2(self): def f(id): return {'a': 1, 'b': 2} self.server.add_url('get', '/post/{id}/', f) request = Request('host', aiohttp.RawRequestMessage( 'GET', '/po/123', '1.1', {}, True, None), None, loop=self.loop) with self.assertRaises(aiohttp.HttpException) as ctx: self.loop.run_until_complete(self.server.dispatch(request)) self.assertEqual(404, ctx.exception.code)
def test_dispatch_with_request(self): def f(id, req): self.assertIsInstance(req, Request) self.assertEqual('GET', req.method) self.assertEqual('/post/123', req.path) return {'a': 1, 'b': 2} self.server.add_url('get', '/post/{id}', f, use_request='req') request = Request('host', aiohttp.RawRequestMessage( 'GET', '/post/123', '1.1', {}, True, None), None, loop=self.loop) ret, code = self.loop.run_until_complete(self.server.dispatch(request)) # json.loads is required to avoid items order in dict self.assertEqual({"b": 2, "a": 1}, json.loads(ret)) self.assertEqual(200, code)
def test_dispatch_bad_signature2(self): def f(unknown_argname): return {'a': 1, 'b': 2} self.server.add_url('get', '/post/{id}', f) request = Request('host', aiohttp.RawRequestMessage( 'GET', '/post/123', '1.1', {}, True, None), None, loop=self.loop) @asyncio.coroutine def go(): with self.assertRaises(aiohttp.HttpException) as ctx: yield from self.server.dispatch(request) self.assertEqual(500, ctx.exception.code) self.loop.run_until_complete(go())
def test_dispatch_http_exception_from_handler(self): def f(id): raise aiohttp.HttpErrorException( 401, headers=(('WWW-Authenticate', 'Basic'),)) self.server.add_url('get', '/post/{id}', f) request = Request('host', aiohttp.RawRequestMessage( 'GET', '/post/123', '1.1', {}, True, None), None, loop=self.loop) @asyncio.coroutine def go(): with self.assertRaises(aiohttp.HttpException) as ctx: yield from self.server.dispatch(request) self.assertEqual(401, ctx.exception.code) self.assertEqual((('WWW-Authenticate', 'Basic'),), ctx.exception.headers) self.loop.run_until_complete(go())