Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_parser_request_error_in_cb_on_url(self):
class Error(Exception):
pass
m = mock.Mock()
m.on_url.side_effect = Error()
p = httptools.HttpRequestParser(m)
try:
p.feed_data(UPGRADE_REQUEST1)
except httptools.HttpParserCallbackError as ex:
self.assertIsInstance(ex.__context__, Error)
else:
self.fail('HttpParserCallbackError was not raised')
def test_parser_request_3(self):
p = httptools.HttpRequestParser(None)
with self.assertRaises(httptools.HttpParserInvalidURLError):
p.feed_data(b'POST HTTP/1.2')
def test_parser_response_1(self):
m = mock.Mock()
headers = {}
m.on_header.side_effect = headers.__setitem__
p = httptools.HttpResponseParser(m)
p.feed_data(memoryview(RESPONSE1_HEAD))
self.assertEqual(p.get_http_version(), '1.1')
self.assertEqual(p.get_status_code(), 200)
m.on_status.assert_called_once_with(b'OK')
m.on_headers_complete.assert_called_once_with()
self.assertEqual(m.on_header.call_count, 8)
self.assertEqual(len(headers), 8)
self.assertEqual(headers.get(b'Connection'), b'close')
self.assertEqual(headers.get(b'Content-Type'),
b'text/html; charset=UTF-8')
self.assertFalse(m.on_body.called)
p.feed_data(bytearray(RESPONSE1_BODY))
def test_parser_response_cb_on_status_1(self):
class Error(Exception):
pass
m = mock.Mock()
m.on_status.side_effect = Error()
p = httptools.HttpResponseParser(m)
try:
p.feed_data(RESPONSE1_HEAD + RESPONSE1_BODY)
except httptools.HttpParserCallbackError as ex:
self.assertIsInstance(ex.__context__, Error)
else:
self.fail('HttpParserCallbackError was not raised')
def test_parser_response_2(self):
with self.assertRaisesRegex(TypeError, 'a bytes-like object'):
httptools.HttpResponseParser(None).feed_data('')
def test_parser_response_5(self):
m = mock.Mock()
m.on_status = None
m.on_header = None
m.on_body = None
m.on_headers_complete = None
m.on_chunk_header = None
m.on_chunk_complete = None
p = httptools.HttpResponseParser(m)
p.feed_data(RESPONSE1_HEAD)
p.feed_data(RESPONSE1_BODY)
m.on_message_complete.assert_called_once_with()
def parse(self, url:bytes):
parsed = httptools.parse_url(url)
return (parsed.schema, parsed.host, parsed.port, parsed.path,
parsed.query, parsed.fragment, parsed.userinfo)
def test_parser_upgrade_response_1(self):
m = mock.Mock()
headers = {}
m.on_header.side_effect = headers.__setitem__
p = httptools.HttpResponseParser(m)
try:
p.feed_data(UPGRADE_RESPONSE1)
except httptools.HttpParserUpgrade as ex:
offset = ex.args[0]
else:
self.fail('HttpParserUpgrade was not raised')
self.assertEqual(UPGRADE_RESPONSE1[offset:], b'data')
self.assertEqual(p.get_http_version(), '1.1')
self.assertEqual(p.get_status_code(), 101)
m.on_status.assert_called_once_with(b'Switching Protocols')
m.on_headers_complete.assert_called_once_with()
self.assertEqual(m.on_header.call_count, 6)
self.assertEqual(len(headers), 6)
m.on_message_complete.assert_called_once_with()
def test_parser_response_3(self):
callbacks = {'on_header', 'on_headers_complete', 'on_body',
'on_message_complete'}
for cbname in callbacks:
with self.subTest('{} callback fails correctly'.format(cbname)):
with self.assertRaisesRegex(httptools.HttpParserCallbackError,
'callback failed'):
m = mock.Mock()
getattr(m, cbname).side_effect = Exception()
p = httptools.HttpResponseParser(m)
p.feed_data(RESPONSE1_HEAD + RESPONSE1_BODY)
def test_parser_request_error_in_on_message_begin(self):
class Error(Exception):
pass
m = mock.Mock()
m.on_message_begin.side_effect = Error()
p = httptools.HttpRequestParser(m)
try:
p.feed_data(UPGRADE_REQUEST1)
except httptools.HttpParserCallbackError as ex:
self.assertIsInstance(ex.__context__, Error)
else:
self.fail('HttpParserCallbackError was not raised')