def test_pipelined_response_parse(self) -> None: self.assert_pipeline_response( okResponse( headers={ b'Content-Length': b'15', }, content=b'{"key":"value"}', ), )
def test_pipelined_chunked_response_parse(self) -> None: self.assert_pipeline_response( okResponse( headers={ b'Transfer-Encoding': b'chunked', b'Content-Type': b'application/json', }, content=b'f\r\n{"key":"value"}\r\n0\r\n\r\n', compress=False, ), )
def test_build_response(self) -> None: self.assertEqual( okResponse(protocol_version=b'HTTP/1.1'), CRLF.join([ b'HTTP/1.1 200 OK', CRLF, ]), ) self.assertEqual( okResponse( headers={b'key': b'value'}, protocol_version=b'HTTP/1.1', ), CRLF.join([ b'HTTP/1.1 200 OK', b'key: value', CRLF, ]), )
def test_close_header(self) -> None: self.assertEqual( okResponse( content=b'Hello World', headers={ b'Host': b'jaxl.com', }, conn_close=True, ), b'HTTP/1.1 200 OK\r\nHost: jaxl.com\r\nContent-Length: 11\r\nConnection: close\r\n\r\nHello World', )
def test_basic(self) -> None: self.assertEqual( okResponse(), b'HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n', ) self.assertEqual( okResponse(headers={ b'X-Custom-Header': b'my value', }, ), b'HTTP/1.1 200 OK\r\nX-Custom-Header: my value\r\nContent-Length: 0\r\n\r\n', ) self.assertEqual( okResponse( content=b'Hello World', headers={ b'X-Custom-Header': b'my value', }, ), b'HTTP/1.1 200 OK\r\nX-Custom-Header: my value\r\nContent-Length: 11\r\n\r\nHello World', )
def test_chunked_with_compression(self) -> None: chunks = ChunkParser.to_chunks(b'Hello World', chunk_size=5) self.assertEqual( gzip.decompress( okResponse( content=chunks, headers={ b'Transfer-Encoding': b'chunked', }, ).tobytes().split(CRLF + CRLF, maxsplit=1)[-1], ), chunks, )
def test_compression(self) -> None: content = b'H' * 21 self.assertEqual( gzip.decompress( okResponse( content=content, headers={ b'X-Custom-Header': b'my value', }, ).tobytes().split(CRLF + CRLF, maxsplit=1)[-1], ), content, ) self.assertEqual( okResponse( content=content, headers={ b'Host': b'jaxl.com', }, min_compression_length=len(content), ), b'HTTP/1.1 200 OK\r\nHost: jaxl.com\r\nContent-Length: 21\r\n\r\nHHHHHHHHHHHHHHHHHHHHH', )
def test_chunked_without_compression(self) -> None: chunks = ChunkParser.to_chunks(b'Hello World', chunk_size=5) self.assertEqual( okResponse( content=chunks, headers={ b'Transfer-Encoding': b'chunked', }, # Avoid compressing chunks for demo purposes here # Ideally you should omit this flag and send # compressed chunks. min_compression_length=len(chunks), ), b'HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nHello\r\n5\r\n Worl\r\n1\r\nd\r\n0\r\n\r\n', )
def test_build_response_adds_content_length_header(self) -> None: body = b'Hello world!!!' self.assertEqual( okResponse( headers={b'key': b'value'}, content=body, protocol_version=b'HTTP/1.1', ), CRLF.join([ b'HTTP/1.1 200 OK', b'key: value', b'Content-Length: ' + bytes_(len(body)), CRLF, ]) + body, )
async def test_man_in_the_middle_plugin(self) -> None: await self.protocol_handler._run_once() self.assertEqual(self.mock_sign_csr.call_count, 1) self.assertEqual(self.mock_gen_csr.call_count, 1) self.assertEqual(self.mock_gen_public_key.call_count, 1) self.mock_server_conn.assert_called_once_with('uni.corn', 443) self.server.connect.assert_called() self.assertEqual( self.protocol_handler.work.connection, self.client_ssl_connection, ) self.assertEqual(self.server.connection, self.server_ssl_connection) self._conn.send.assert_called_with( PROXY_TUNNEL_ESTABLISHED_RESPONSE_PKT, ) self.assertFalse(self.protocol_handler.work.has_buffer()) # request = build_http_request( b'GET', b'/', headers={ b'Host': b'uni.corn', }, no_ua=True, ) self.client_ssl_connection.recv.return_value = request # Client read await self.protocol_handler._run_once() self.server.queue.assert_called_once_with(request) # Server write await self.protocol_handler._run_once() self.server.flush.assert_called_once() # Server read self.server.recv.return_value = okResponse( content=b'Original Response From Upstream', ) await self.protocol_handler._run_once() response = HttpParser(httpParserTypes.RESPONSE_PARSER) response.parse(self.protocol_handler.work.buffer[0]) assert response.body self.assertEqual( gzip.decompress(response.body), b'Hello from man in the middle', )
def handle_request(self, request: HttpParser) -> None: self.client.queue(okResponse(content=b'Hello World'))