Example #1
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'}
Example #2
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
Example #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'
Example #4
0
def test_parse_bad_response_line():
    """Test parsing bad response line"""
    with pytest.raises(HttpParsingError):
        HttpResponse()._set_response_initial(b'foo bar baz')
Example #5
0
def test_parse_response_line_with_empty_reason():
    """Test parsing response line with empty reason-phrase"""
    response = HttpResponse()
    response._set_response_initial(b'HTTP/1.1 200 \r\n')
    assert response.status_code == 200
Example #6
0
def test_parse_response_line():
    """Test parsing response line"""
    response = HttpResponse()
    response._set_response_initial(b'HTTP/1.1 200 OK\r\n')
    assert response.status_code == 200
Example #7
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', '')]