aiohttp.web.HTTPOk

Here are the examples of the python api aiohttp.web.HTTPOk taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

19 Examples 7

Example 1

Project: sockjs
License: View license
Source File: test_route.py
    @mock.patch('sockjs.route.RawWebSocketTransport')
    def test_raw_websocket(self, ws):
        ws.return_value.process.return_value = asyncio.Future(loop=self.loop)
        ws.return_value.process.return_value.set_result(web.HTTPOk())

        route = self.make_route()
        request = self.make_request(
            'GET', '/sm/', headers=CIMultiDict({}))
        res = self.loop.run_until_complete(route.websocket(request))

        self.assertIsInstance(res, web.HTTPOk)
        self.assertTrue(ws.called)
        self.assertTrue(ws.return_value.process.called)

Example 2

Project: aiohttp
License: View license
Source File: test_connector.py
@asyncio.coroutine
def test_tcp_connector(test_client, loop):
    @asyncio.coroutine
    def handler(request):
        return web.HTTPOk()

    app = web.Application(loop=loop)
    app.router.add_get('/', handler)
    client = yield from test_client(app)

    r = yield from client.get('/')
    assert r.status == 200

Example 3

Project: aiohttp
License: View license
Source File: test_connector.py
    @unittest.skipUnless(hasattr(socket, 'AF_UNIX'), 'requires unix')
    def test_unix_connector(self):
        @asyncio.coroutine
        def handler(request):
            return web.HTTPOk()

        app, srv, url, sock_path = self.loop.run_until_complete(
            self.create_unix_server('get', '/', handler))

        connector = aiohttp.UnixConnector(sock_path, loop=self.loop)
        self.assertEqual(sock_path, connector.path)

        r = self.loop.run_until_complete(
            client.request(
                'get', url,
                connector=connector,
                loop=self.loop))
        self.assertEqual(r.status, 200)
        r.close()

Example 4

Project: aiohttp
License: View license
Source File: test_resp.py
async def test_await(test_server, loop):

    async def handler(request):
        return web.HTTPOk()

    app = web.Application(loop=loop)
    app.router.add_route('GET', '/', handler)
    server = await test_server(app)
    resp = await aiohttp.get(server.make_url('/'), loop=loop)
    assert resp.status == 200
    assert resp.connection is not None
    await resp.release()
    assert resp.connection is None

Example 5

Project: aiohttp
License: View license
Source File: test_resp.py
async def test_response_context_manager(test_server, loop):

    async def handler(request):
        return web.HTTPOk()

    app = web.Application(loop=loop)
    app.router.add_route('GET', '/', handler)
    server = await test_server(app)
    resp = await aiohttp.get(server.make_url('/'), loop=loop)
    async with resp:
        assert resp.status == 200
        assert resp.connection is not None
    assert resp.connection is None

Example 6

Project: aiohttp
License: View license
Source File: test_resp.py
async def test_response_context_manager_error(test_server, loop):

    async def handler(request):
        return web.HTTPOk()

    app = web.Application(loop=loop)
    app.router.add_route('GET', '/', handler)
    server = await test_server(app)
    cm = aiohttp.get(server.make_url('/'), loop=loop)
    session = cm._session
    resp = await cm
    with pytest.raises(RuntimeError):
        async with resp:
            assert resp.status == 200
            resp.content.set_exception(RuntimeError())
            await resp.read()
    assert len(session._connector._conns) == 0

Example 7

Project: aiohttp
License: View license
Source File: test_resp.py
async def test_client_api_context_manager(test_server, loop):

    async def handler(request):
        return web.HTTPOk()

    app = web.Application(loop=loop)
    app.router.add_route('GET', '/', handler)
    server = await test_server(app)

    async with aiohttp.get(server.make_url('/'), loop=loop) as resp:
        assert resp.status == 200
        assert resp.connection is not None
    assert resp.connection is None

Example 8

Project: aiohttp
License: View license
Source File: test_web_exceptions.py
@asyncio.coroutine
def test_HTTPOk(buf, request):
    resp = web.HTTPOk()
    yield from resp.prepare(request)
    yield from resp.write_eof()
    txt = buf.decode('utf8')
    assert re.match(('HTTP/1.1 200 OK\r\n'
                     'Content-Type: text/plain; charset=utf-8\r\n'
                     'Content-Length: 7\r\n'
                     'Date: .+\r\n'
                     'Server: .+\r\n\r\n'
                     '200: OK'), txt)

Example 9

Project: aiohttp
License: View license
Source File: test_web_functional.py
@asyncio.coroutine
def test_subapp_app(loop, test_client):
    @asyncio.coroutine
    def handler(request):
        assert request.app is subapp
        return web.HTTPOk(text='OK')

    app = web.Application(loop=loop)
    subapp = web.Application(loop=loop)
    subapp.router.add_get('/to', handler)
    app.router.add_subapp('/path/', subapp)

    client = yield from test_client(app)
    resp = yield from client.get('/path/to')
    assert resp.status == 200
    txt = yield from resp.text()
    assert 'OK' == txt

Example 10

Project: aiohttp
License: View license
Source File: test_web_functional.py
@asyncio.coroutine
def test_subapp_not_found(loop, test_client):
    @asyncio.coroutine
    def handler(request):
        return web.HTTPOk(text='OK')

    app = web.Application(loop=loop)
    subapp = web.Application(loop=loop)
    subapp.router.add_get('/to', handler)
    app.router.add_subapp('/path/', subapp)

    client = yield from test_client(app)
    resp = yield from client.get('/path/other')
    assert resp.status == 404

Example 11

Project: aiohttp
License: View license
Source File: test_web_functional.py
@asyncio.coroutine
def test_subapp_not_found2(loop, test_client):
    @asyncio.coroutine
    def handler(request):
        return web.HTTPOk(text='OK')

    app = web.Application(loop=loop)
    subapp = web.Application(loop=loop)
    subapp.router.add_get('/to', handler)
    app.router.add_subapp('/path/', subapp)

    client = yield from test_client(app)
    resp = yield from client.get('/invalid/other')
    assert resp.status == 404

Example 12

Project: aiohttp
License: View license
Source File: test_web_functional.py
@asyncio.coroutine
def test_subapp_not_allowed(loop, test_client):
    @asyncio.coroutine
    def handler(request):
        return web.HTTPOk(text='OK')

    app = web.Application(loop=loop)
    subapp = web.Application(loop=loop)
    subapp.router.add_get('/to', handler)
    app.router.add_subapp('/path/', subapp)

    client = yield from test_client(app)
    resp = yield from client.post('/path/to')
    assert resp.status == 405
    assert resp.headers['Allow'] == 'GET'

Example 13

Project: aiohttp
License: View license
Source File: test_web_functional.py
@asyncio.coroutine
def test_subapp_cannot_add_app_in_handler(loop, test_client):
    @asyncio.coroutine
    def handler(request):
        request.match_info.add_app(app)
        return web.HTTPOk(text='OK')

    app = web.Application(loop=loop)
    subapp = web.Application(loop=loop)
    subapp.router.add_get('/to', handler)
    app.router.add_subapp('/path/', subapp)

    client = yield from test_client(app)
    resp = yield from client.get('/path/to')
    assert resp.status == 500

Example 14

@asyncio.coroutine
def test_forget(make_app, test_client):

    @asyncio.coroutine
    def index(request):
        session = yield from get_session(request)
        return web.HTTPOk(text=session.get('AIOHTTP_SECURITY', ''))

    @asyncio.coroutine
    def login(request):
        response = web.HTTPFound(location='/')
        yield from remember(request, response, 'Andrew')
        return response

    @asyncio.coroutine
    def logout(request):
        response = web.HTTPFound('/')
        yield from forget(request, response)
        return response

    app = make_app()
    app.router.add_route('GET', '/', index)
    app.router.add_route('POST', '/login', login)
    app.router.add_route('POST', '/logout', logout)

    client = yield from test_client(app)

    resp = yield from client.post('/login')
    assert 200 == resp.status
    assert resp.url.endswith('/')
    txt = yield from resp.text()
    assert 'Andrew' == txt
    yield from resp.release()

    resp = yield from client.post('/logout')
    assert 200 == resp.status
    assert resp.url.endswith('/')
    txt = yield from resp.text()
    assert '' == txt
    yield from resp.release()

Example 15

Project: sockjs
License: View license
Source File: test_route.py
    def _test_transport(self):
        route = self.make_route()
        request = self.make_request(
            'GET', '/sm/',
            match_info={
                'transport': 'xhr', 'session': 's1', 'server': '000'})

        params = []

        class Transport:
            def __init__(self, manager, session, request):
                params.append((manager, session, request))

            def process(self):
                return web.HTTPOk()

        route = self.make_route(handlers={'test': (True, Transport)})
        res = self.loop.run_until_complete(route.handler(request))
        self.assertIsInstance(res, web.HTTPOk)
        self.assertEqual(
            params[0], (route.manager, route.manager['s1'], request))

Example 16

Project: aiohttp
License: View license
Source File: test_connector.py
    def test_tcp_connector_uses_provided_local_addr(self):
        @asyncio.coroutine
        def handler(request):
            return web.HTTPOk()

        app, srv, url = self.loop.run_until_complete(
            self.create_server('get', '/', handler)
        )

        port = unused_port()
        conn = aiohttp.TCPConnector(loop=self.loop,
                                    local_addr=('127.0.0.1', port))

        r = self.loop.run_until_complete(
            aiohttp.request(
                'get', url,
                connector=conn
            ))

        self.loop.run_until_complete(r.release())
        first_conn = next(iter(conn._conns.values()))[0][0]
        self.assertEqual(first_conn._sock.getsockname(), ('127.0.0.1', port))
        r.close()

        conn.close()

Example 17

Project: aiohttp
License: View license
Source File: test_web_exceptions.py
def test_default_body():
    resp = web.HTTPOk()
    assert b'200: OK' == resp.body

Example 18

Project: aiohttp
License: View license
Source File: test_web_functional.py
@asyncio.coroutine
def test_subapp_middlewares(loop, test_client):
    order = []

    @asyncio.coroutine
    def handler(request):
        return web.HTTPOk(text='OK')

    @asyncio.coroutine
    def middleware_factory(app, handler):

        @asyncio.coroutine
        def middleware(request):
            order.append((1, app))
            resp = yield from handler(request)
            assert 200 == resp.status
            order.append((2, app))
            return resp
        return middleware

    app = web.Application(loop=loop, middlewares=[middleware_factory])
    subapp1 = web.Application(loop=loop, middlewares=[middleware_factory])
    subapp2 = web.Application(loop=loop, middlewares=[middleware_factory])
    subapp2.router.add_get('/to', handler)
    subapp1.router.add_subapp('/b/', subapp2)
    app.router.add_subapp('/a/', subapp1)

    client = yield from test_client(app)
    resp = yield from client.get('/a/b/to')
    assert resp.status == 200
    assert [(1, app), (1, subapp1), (1, subapp2),
            (2, subapp2), (2, subapp1), (2, app)] == order

Example 19

Project: aiohttp
License: View license
Source File: test_web_functional.py
@asyncio.coroutine
def test_subapp_on_response_prepare(loop, test_client):
    order = []

    @asyncio.coroutine
    def handler(request):
        return web.HTTPOk(text='OK')

    def make_signal(app):

        @asyncio.coroutine
        def on_response(request, response):
            order.append(app)

        return on_response

    app = web.Application(loop=loop)
    app.on_response_prepare.append(make_signal(app))
    subapp1 = web.Application(loop=loop)
    subapp1.on_response_prepare.append(make_signal(subapp1))
    subapp2 = web.Application(loop=loop)
    subapp2.on_response_prepare.append(make_signal(subapp2))
    subapp2.router.add_get('/to', handler)
    subapp1.router.add_subapp('/b/', subapp2)
    app.router.add_subapp('/a/', subapp1)

    client = yield from test_client(app)
    resp = yield from client.get('/a/b/to')
    assert resp.status == 200
    assert [app, subapp1, subapp2] == order