Example #1
0
def make_request(app, method, path):
    headers = CIMultiDict()
    if StrictVersion(aiohttp.__version__) < StrictVersion('0.20.0'):
        message = aiohttp.RawRequestMessage(method, path,
                                            aiohttp.HttpVersion(1, 1), headers,
                                            False, False)
    else:
        message = aiohttp.RawRequestMessage(method, path,
                                            aiohttp.HttpVersion(1, 1), headers,
                                            headers, False, False)
    payload = mock.Mock()
    transport = mock.Mock()
    writer = mock.Mock()
    req = web.Request(app, message, payload, transport, writer, 15)
    return req
Example #2
0
    def handle(environ, start_response):

        req = webob.Request(environ)
        vers = aiohttp.HttpVersion10 if req.http_version == 'HTTP/1.0' else aiohttp.HttpVersion11
        message = aiohttp.RawRequestMessage(req.method, req.path_qs, vers,
                                            aiohttp.CIMultiDict(req.headers),
                                            req.headers, False, False)
        payload = aiohttp.StreamReader(loop=loop_)
        payload.feed_data(req.body)
        payload.feed_eof()
        factory = aiohttp.web.RequestHandlerFactory(app_,
                                                    app_.router,
                                                    loop=loop_,
                                                    keep_alive_on=False)
        handler = factory()
        handler.transport = io.BytesIO()
        handler.transport.is_closing = lambda: False
        handler.transport._conn_lost = 0
        handler.transport.get_extra_info = lambda s: ('127.0.0.1', 80)
        handler.writer = aiohttp.parsers.StreamWriter(handler.transport,
                                                      handler, handler.reader,
                                                      handler._loop)
        coro = handler.handle_request(message, payload)
        if loop_.is_running():
            raise RuntimeError(
                'Client cannot start durring another coroutine is running.')

        loop_.run_until_complete(coro)
        handler.transport.seek(9)
        res = webob.Response.from_file(handler.transport)
        start_response(res.status, res.headerlist)
        return res.app_iter
Example #3
0
 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)
Example #4
0
    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())
Example #5
0
 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 make_request(self, app, method, path):
     headers = CIMultiDict()
     message = aiohttp.RawRequestMessage(method, path,
                                         aiohttp.HttpVersion(1, 1), headers,
                                         False, False)
     self.payload = mock.Mock()
     self.transport = mock.Mock()
     self.writer = mock.Mock()
     req = web.Request(app, message, self.payload, self.transport,
                       self.writer, 15)
     return req
Example #7
0
    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)
Example #8
0
    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)
Example #9
0
    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)
Example #10
0
    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)
Example #11
0
    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())
Example #12
0
    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())
Example #13
0
 def setUp(self):
     self.loop = mock.Mock()
     self._REQUEST = aiohttp.RawRequestMessage('GET', '/some/path', '1.1',
                                               MutableMultiDict(), True,
                                               None)