def test_write_only(self): output = uio.BytesIO() writer = uhttp.ResponseWriter(output) data = b'This is a buffer that will be written' writer.write(data) output.seek(0) statusLine = output.readline() self.assertEqual(statusLine.decode().strip(), 'HTTP/1.1 200 OK') got_headers = uhttp._parse_headers(output) want_len = len(data) # Default headers should be there self.assertTrue('server' in got_headers, 'No server header') self.assertTrue('connection' in got_headers, 'No connection header') self.assertEqual( got_headers, { 'server': got_headers['server'], 'connection': got_headers['connection'], 'content-length': str(want_len) }) got_data = output.read(want_len) self.assertEqual(got_data, data)
def test_write_header_custom_headers(self): output = uio.BytesIO() writer = uhttp.ResponseWriter(output) writer.headers['X-Header-1'] = 'value1' writer.headers['X-Header-2'] = 'value2' writer.write_header(534, 'Custom Reason Phrase') output.write('\n\n') output.seek(0) output.readline() got_headers = uhttp._parse_headers(output) pythonName = sys.implementation.name pythonVersion = '.'.join(map(str, sys.implementation.version)) self.assertEqual( got_headers, { 'server': 'uhttp/0.1 %s/%s %s' % (pythonName, pythonVersion, sys.platform), 'connection': 'close', 'x-header-1': 'value1', 'x-header-2': 'value2' })
def test_write_header_known_status(self): output = uio.BytesIO() writer = uhttp.ResponseWriter(output) writer.write_header(uhttp.HTTP_STATUS_OK) output.seek(0) status_line = output.readline() self.assertEqual(b"HTTP/1.1 200 OK", status_line.rstrip()) output.seek(0) writer.write_header(uhttp.HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE) output.seek(0) status_line = output.readline() self.assertEqual(b"HTTP/1.1 415 Unsupported Media Type", status_line.rstrip()) self.assertEqual(writer.status, uhttp.HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE) output.seek(0) writer.write_header(uhttp.HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE, 'Custom reason phrase 123321') output.seek(0) status_line = output.readline() self.assertEqual(b"HTTP/1.1 415 Custom reason phrase 123321", status_line.rstrip())
def test_write_header_unknown_status(self): output = uio.BytesIO() writer = uhttp.ResponseWriter(output) writer.write_header(534, 'Custom Reason Phrase') output.seek(0) status_line = output.readline() self.assertEqual(b"HTTP/1.1 534 Custom Reason Phrase", status_line.rstrip()) thrown = False err = None try: output.seek(0) writer.write_header(534) except Exception as e: thrown = True err = e self.assertTrue(thrown) self.assertEqual('reason_phrase must be provided', str(err))
def test_process_client_handle_unhandled_handler_errors(self): def handler(w, req): raise Exception('Something happened') server = uhttp.HTTPServer( handler=handler, port=8080, logger=logger.TestLogger(), ) client = uio.BytesIO(('GET /some-resource?qs=value HTTP/1.1\n' 'Host: domain.com\n' '\n')) output = uio.BytesIO() writer = uhttp.ResponseWriter(output) server._process_client(writer, client) output.seek(0) self.assertEqual( output.readline().decode().strip(), 'HTTP/1.1 %s %s' % (uhttp.HTTP_STATUS_INTERNAL_SERVER_ERROR, uhttp.HTTP_REASON_PHRASE[ uhttp.HTTP_STATUS_INTERNAL_SERVER_ERROR])) server.stop()
def test_write_header_default_headers(self): pythonName = sys.implementation.name pythonVersion = '.'.join(map(str, sys.implementation.version)) want_server = 'uhttp/0.1 %s/%s %s' % (pythonName, pythonVersion, sys.platform) output = uio.BytesIO() writer = uhttp.ResponseWriter(output) self.assertEqual(writer.headers, { "Server": want_server, "Connection": "close", }) writer.write_header(534, 'Custom Reason Phrase') output.write('\n\n') output.seek(0) output.readline() got_headers = uhttp._parse_headers(output) self.assertEqual(got_headers, { 'server': want_server, 'connection': 'close' })
def test_process_client_handle_parsing_errors(self): server = uhttp.HTTPServer( handler=lambda w, req: w.write('PONG'), port=8080, logger=logger.TestLogger(), ) client = uio.BytesIO(('SOMETHING /some-resource?qs=value HTTP/1.1\n' 'Host: domain.com\n' '\n')) output = uio.BytesIO() writer = uhttp.ResponseWriter(output) server._process_client(writer, client) output.seek(0) self.assertEqual( output.readline().decode().strip(), 'HTTP/1.1 %s %s' % (uhttp.HTTP_STATUS_NOT_IMPLEMENTED, uhttp.HTTP_REASON_PHRASE[uhttp.HTTP_STATUS_NOT_IMPLEMENTED])) # Scroll to body while output.readline() != b'\r\n': pass self.assertEqual(output.readline().decode().strip(), 'Unrecognized method') server.stop()