Пример #1
0
    async def request(self, headers: Dict[str, str],
                      body: Optional[ParsedBodyType]):
        stream_id = self.h2conn.get_next_available_stream_id()
        self.h2conn.send_headers(stream_id, headers.items(),
                                 end_stream=True)
        from aiosonic import HttpResponse
        future = asyncio.Future()
        self.requests[stream_id] = {
            'body': b'',
            'headers': None,
            'future': future
        }
        await self.writer_q.put(True)
        await future
        res = self.requests[stream_id].copy()
        del self.requests[stream_id]

        response = HttpResponse()
        for key, val in res['headers']:
            if key == b':status':
                response.response_initial = {
                    'version': b'2',
                    'code': val
                }
            else:
                response._set_header(key, val)

        if res['body']:
            response._set_body(res['body'])

        return response
Пример #2
0
async def test_json_response_parsing():
    """Test json response parsing."""
    response = HttpResponse()
    response._set_response_initial(b'HTTP/1.1 200 OK\r\n')
    response._set_header('content-type', 'application/json; charset=utf-8')
    response.body = b'{"foo": "bar"}'
    assert (await response.json()) == {'foo': 'bar'}
Пример #3
0
def test_encoding_from_header():
    """Test use encoder from header."""
    response = HttpResponse()
    response._set_response_initial(b'HTTP/1.1 200 OK\r\n')
    response._set_header('content-type', 'text/html; charset=utf-8')
    response.body = b'foo'
    assert response._get_encoding() == 'utf-8'

    response._set_header('content-type', 'application/json')
    assert response._get_encoding() == 'utf-8'

    response._set_header('content-type', 'text/html; charset=weirdencoding')
    assert response._get_encoding() == 'ascii'
Пример #4
0
def test_headers_parsing():
    """Test parsing header with no value."""
    parsing = HttpResponse()
    parsing._set_header(*HttpHeaders._clear_line(b'Expires: \r\n'))
    assert parsing.raw_headers == [('Expires', '')]