Exemple #1
0
 def test_invalid_headers(self):
     req = b'GET / HTTP/1.1\r\nContent-Type text/html\r\n\r\n'
     transport = unittest.mock.Mock()
     transport.get_extra_info.return_value = ('127.0.0.1', 1)
     stream = asyncio.StreamReader(loop=self.loop)
     stream.set_transport(transport)
     task = asyncio.Task(HttpParser.parse(stream), loop=self.loop)
     self.loop.call_soon(lambda: stream.feed_data(req))
     self.assertRaises(BadRequestException, self.loop.run_until_complete, task)
Exemple #2
0
    def test_eof(self):
        stream = asyncio.StreamReader(loop=self.loop)
        task = asyncio.Task(HttpParser.parse(stream), loop=self.loop)
        self.loop.call_soon(lambda: stream.feed_eof())
        req = self.loop.run_until_complete(task)
        self.assertIs(req, None)
        req = self.loop.run_until_complete(task)

        transport = unittest.mock.Mock()
        transport.get_extra_info.return_value = ('127.0.0.1', 1)
        stream = asyncio.StreamReader(loop=self.loop)
        stream.set_transport(transport)
        task = asyncio.Task(HttpParser.parse(stream), loop=self.loop)
        def feed():
            stream.feed_data(b'GET / HTTP/1.1\r\n')
            stream.feed_eof()
        self.loop.call_soon(feed)
        req = self.loop.run_until_complete(task)
        self.assertIs(req, None)
Exemple #3
0
    def test_no_body(self):
        req = b'GET / HTTP/1.1\r\nContent-Type: text/html\r\n\r\n'
        transport = unittest.mock.Mock()
        transport.get_extra_info.return_value = ('127.0.0.1', 1)
        stream = asyncio.StreamReader(loop=self.loop)
        stream.set_transport(transport)
        task = asyncio.Task(HttpParser.parse(stream), loop=self.loop)
        self.loop.call_soon(lambda: stream.feed_data(req))
        result = self.loop.run_until_complete(task)

        body = self.loop.run_until_complete(asyncio.Task(result.body.read(), loop=self.loop))
        self.assertEqual(body, b'')
Exemple #4
0
    def test_multiline_headers(self):
        req = b'GET / HTTP/1.1\r\nHello: world\r\n foo\r\nContent-Type: text/html\r\n\r\n'
        transport = unittest.mock.Mock()
        transport.get_extra_info.return_value = ('127.0.0.1', 1)
        stream = asyncio.StreamReader(loop=self.loop)
        stream.set_transport(transport)
        task = asyncio.Task(HttpParser.parse(stream), loop=self.loop)
        self.loop.call_soon(lambda: stream.feed_data(req))
        result = self.loop.run_until_complete(task)

        self.assertEqual(result.get('Hello'), 'world foo')
        self.assertEqual(result.get('Content-Type'), 'text/html')
Exemple #5
0
    def test_headers(self):
        req = b'GET / HTTP/1.1\r\nHello: world\r\n\r\n'
        transport = unittest.mock.Mock()
        transport.get_extra_info.return_value = ('127.0.0.1', 1)
        stream = asyncio.StreamReader(loop=self.loop)
        stream.set_transport(transport)
        task = asyncio.Task(HttpParser.parse(stream), loop=self.loop)
        self.loop.call_soon(lambda: stream.feed_data(req))
        result = self.loop.run_until_complete(task)

        self.assertEqual(result.method, 'GET')
        self.assertEqual(result.uri, '/')
        self.assertEqual(result.version, 'HTTP/1.1')
        self.assertEqual(result.get('Hello'), 'world')
Exemple #6
0
    def _handle_client(self):
        while True:
            try:
                req = yield from HttpParser.parse(self._reader)
            except BadRequestException as e:
                self._bad_request(self._writer, e)
                self._writer.close()
                break
            # connection has been closed
            if req is None:
                break

            try:
                yield from self._handler.handle_request(req)
            finally:
                if self._should_close_conn_immediately(req):
                    self._writer.close()
                else:
                    yield from req.body.read()
                    if self._writer is not None:
                        self._writer.restore()
Exemple #7
0
 def test_bad_version(self):
     stream = asyncio.StreamReader(loop=self.loop)
     task = asyncio.Task(HttpParser.parse(stream), loop=self.loop)
     self.loop.call_soon(lambda: stream.feed_data(b'GET / HTTP/2.3\r\n'))
     self.assertRaises(BadRequestException, self.loop.run_until_complete, task)