def test_response_get_bytes_code_only(self) -> None: response = Response(200) expected = ( HTTP_VERSION + ' 200 OK' + CRLF + CRLF ).encode() self.assertEqual(response.get_bytes(), expected)
def test_response_get_bytes_no_message_body(self) -> None: response = Response(200, content_type=('text', 'plain')) expected = ( HTTP_VERSION + ' 200 OK' + CRLF + 'Content-Type: text/plain' + CRLF + CRLF ).encode() self.assertEqual(response.get_bytes(), expected)
def test_response_get_bytes_special_unicode_as_str(self) -> None: response = Response(200, ('text', 'plain'), 'λ') expected = ( HTTP_VERSION + ' 200 OK' + CRLF + 'Content-Type: text/plain' + CRLF + 'Content-Length: 2' + CRLF + CRLF + 'λ' ).encode() self.assertEqual(response.get_bytes(), expected)
def test_response_get_bytes_no_content_type(self) -> None: message_body = 'here is some text' response = Response(200, message_body=message_body) expected = ( HTTP_VERSION + ' 200 OK' + CRLF + 'Content-Length: {}'.format(len(message_body)) + CRLF + CRLF + message_body ).encode() self.assertEqual(response.get_bytes(), expected)
def test_handler_code_500(self) -> None: def custom_handler(request: Request) -> Response: raise Exception() request = Request(GET_METHOD, [''], HTTP_VERSION) response = Response(500) with self.assertRaises(Exception): custom_handler(request) wrapped_handler = create_handler(custom_handler) request_str = '{} / {}{}'.format(GET_METHOD, HTTP_VERSION, CRLF) response_str = response.get_bytes() self.assertEqual(wrapped_handler(request_str.encode()), response_str)
def test_handler_code_400(self) -> None: response_200 = Response(200, ('text', 'plain'), '') @create_handler def custom_handler(request: Request) -> Response: return response_200 good_request_str = '{} / {}{}'.format(GET_METHOD, HTTP_VERSION, CRLF) self.assertIsNotNone(parse(good_request_str)) self.assertEqual(response_200.get_bytes(), custom_handler(good_request_str.encode())) bad_request_str = '{}/ {}{}'.format(GET_METHOD, HTTP_VERSION, CRLF) self.assertIsNone(parse(bad_request_str)) self.assertEqual( Response(400).get_bytes(), custom_handler(bad_request_str.encode()))
def test_handler(self) -> None: def custom_handler(request: Request) -> Response: message_body = 'You requested URI {}'.format(request.uri) return Response(200, ('text', 'plain'), message_body) request = Request(GET_METHOD, ['hello', 'world'], HTTP_VERSION) response = Response(200, ('text', 'plain'), "You requested URI ['hello', 'world']") self.assertEqual(custom_handler(request), response) wrapped_handler = create_handler(custom_handler) request_str = ('{} /hello/world {}{}'.format(GET_METHOD, HTTP_VERSION, CRLF)) response_str = response.get_bytes() self.assertEqual(wrapped_handler(request_str.encode()), response_str)
def test_create_response(self) -> None: Response(200, ('text', 'html'), '')
def test_response_invalid_content_type(self) -> None: with self.assertRaises(ValueError): Response(200, ('html', 'text'), '')
def test_response_invalid_status_code(self) -> None: with self.assertRaises(ValueError): Response(50, ('text', 'html'), '')
def custom_handler(request: Request) -> Response: message_body = 'You requested URI {}'.format(request.uri) return Response(200, ('text', 'plain'), message_body)