def test_query_none(): server = mock.MockHTTPServer(routes=query.new('/', QueryHandler)) regex, handler = list(server.routes.items())[0] request = mock.MockHTTPRequest(None, ('', 0), None, method='GET', handler=handler) response = request.handler.respond() assert response[0] == 200 assert json.loads(response[1]) is None
def test_form_get(): request = mock.MockHTTPRequest(None, ('', 0), None, method='GET', handler=EchoHandler) response = request.handler.respond() assert response[0] == 204 assert response[1] == ''
def test_query_empty(): server = mock.MockHTTPServer(routes=query.new('/', QueryHandler)) regex, handler = list(server.routes.items())[0] groups = regex.match('/?').groupdict() request = mock.MockHTTPRequest(None, ('', 0), None, method='GET', resource='/?', groups=groups, handler=handler) response = request.handler.respond() assert response[0] == 200 assert json.loads(response[1]) == {}
def test_query_not_found(): server = mock.MockHTTPServer(routes={r'/\?(?P<custom>.*)': QueryHandler}) regex, handler = list(server.routes.items())[0] groups = regex.match(test_encoded).groupdict() request = mock.MockHTTPRequest(None, ('', 0), None, method='GET', resource=test_encoded, groups=groups, handler=handler) response = request.handler.respond() assert response[0] == 200 assert json.loads(response[1]) is None
def test_json_error_message(): request = mock.MockHTTPRequest(None, ('', 0), None, handler=JSONErrorMessageHandler) headers, response = request.response.headers, request.handler.respond() assert headers.get('Content-Type') == 'application/json' assert response[0] == 500 assert response[1] == 'a' assert response[2] == test_string
def test_json_encode(): request = mock.MockHTTPRequest(None, ('', 0), None, method='GET', handler=JSONHandler) headers, response = request.response.headers, request.handler.respond() assert headers.get('Content-Type') == 'application/json' assert response[0] == 200 assert response[1] == test_string
def test_json_noencode(): request = mock.MockHTTPRequest(None, ('', 0), None, method='GET', handler=JSONEmptyHandler) headers, response = request.response.headers, request.handler.respond() assert not headers.get('Content-Type') assert response[0] == 204 assert response[1] == b''
def test_auth_none(): request = mock.MockHTTPRequest(None, ('', 0), None, method='GET', handler=Handler) with pytest.raises(web.HTTPError) as error: request.handler.respond() assert error.value.headers.get( 'WWW-Authenticate') == 'Any realm="' + test_realm + '"' assert error.value.code == 401
def test_page_error(tmp): request = mock.MockHTTPRequest(None, ('', 0), None, handler=PageErrorHandler) request.handler.directory = tmp headers, response = request.response.headers, request.handler.respond() assert headers.get('Content-Type').startswith('text/html; charset=') assert response[0] == 500 assert response[2] == '500 - Internal Server Error'
def test_form_post(): request = mock.MockHTTPRequest(None, ('', 0), None, body=test_body, method='POST', handler=EchoHandler) response = request.handler.respond() assert request.body == test_body assert response[0] == 200 assert response[1] == test_body
def test_page_format(tmp): request = mock.MockHTTPRequest(None, ('', 0), None, method='GET', handler=PageFormatHandler) request.handler.directory = tmp headers, response = request.response.headers, request.handler.respond() assert headers.get('Content-Type').startswith('text/html; charset=') assert response[0] == 200 assert response[1] == test_string.format(test_fill)
def test_page_error_message(tmp): request = mock.MockHTTPRequest(None, ('', 0), None, handler=PageErrorMessageHandler) request.handler.directory = tmp headers, response = request.response.headers, request.handler.respond() assert headers.get('Content-Type').startswith('text/html; charset=') assert response[0] == 500 assert response[1] == 'a' assert response[2] == test_fill
def run(method, resource, local, body='', headers=None, handler=None, groups=None, remote='', index_files=None, dir_index=False, modify=False, return_handler=False): if not isinstance(body, bytes): body = body.encode('utf-8') if not handler: route = file.new(local, remote, index_files=index_files, dir_index=dir_index, modify=modify) handler = list(route.values())[0] local = handler.kwargs['local'] remote = handler.kwargs['remote'] if groups is None: groups = {'path': resource[len(remote.rstrip('/')):]} request = mock.MockHTTPRequest(None, ('', 0), None, body=body, headers=headers, method=method, resource=resource, groups=groups, handler=handler) handler_obj = request.handler if isinstance(handler_obj, file.PathHandler): handler_obj.local = local else: handler_obj.filename = local if return_handler: return request.response.headers, handler_obj.respond(), handler_obj else: return request.response.headers, handler_obj.respond()
def test_json_error(): request = mock.MockHTTPRequest(None, ('', 0), None, handler=JSONErrorHandler) headers, response = request.response.headers, request.handler.respond() assert headers.get('Content-Type') == 'application/json' assert response[0] == 500 assert response[2] == json.dumps({ 'error': 500, 'status': web.status_messages[500] }).encode(web.http_encoding)
def test_json_nodecode(): request = mock.MockHTTPRequest(None, ('', 0), None, body=test_string, method='POST', handler=JSONDecodeHandler) headers, response = request.response.headers, request.handler.respond() assert headers.get('Content-Type') == 'application/json' assert response[0] == 200 assert response[1] == json.dumps({ 'type': str(bytes) }).encode(web.default_encoding)
def test_auth_token(): request_headers = web.HTTPHeaders() request_headers.set('Authorization', 'Token ' + test_token) request = mock.MockHTTPRequest(None, ('', 0), None, headers=request_headers, method='GET', handler=TokenHandler) headers, response = request.response.headers, request.handler.respond() assert headers.get('WWW-Authenticate') is None assert response[0] == 204
def test_auth_any_forbidden(): request_headers = web.HTTPHeaders() request_headers.set('Authorization', 'Any none') request = mock.MockHTTPRequest(None, ('', 0), None, headers=request_headers, method='GET', handler=ForbiddenHandler) with pytest.raises(web.HTTPError) as error: request.handler.respond() assert error.value.headers is None assert error.value.code == 403
def test_json_bad_decode(): json_headers = web.HTTPHeaders() json_headers.set('Content-Type', 'application/json') request = mock.MockHTTPRequest(None, ('', 0), None, headers=json_headers, body=test_bad, method='POST', handler=JSONNoneHandler) with pytest.raises(web.HTTPError) as error: request.response.headers, request.handler.respond() assert error.value.code == 400
def test_form_multipart_bad_read(): request_headers = web.HTTPHeaders() request_headers.set('Content-Type', 'multipart/form-data; boundary=' + test_boundary) request = mock.MockHTTPRequest(None, ('', 0), None, body=test_mime_empty, headers=request_headers, method='POST', handler=EchoHandler) with pytest.raises(web.HTTPError) as error: request.handler.respond() assert error.value.code == 500
def test_query_bad(): server = mock.MockHTTPServer(routes=query.new('/', QueryHandler)) regex, handler = list(server.routes.items())[0] # not really sure how to get defaults args to parse_qsl # to throw an exception for a string but invalids bytes # works (despite being a scenario that wouldn't actually # happen) # put here just to test that it does the right thing if parse_qsl does happen to throw groups = {'query': b'\xff'} request = mock.MockHTTPRequest(None, ('', 0), None, method='GET', resource='/?\xff', groups=groups, handler=handler) with pytest.raises(web.HTTPError) as error: request.handler.respond() assert error.value.code == 400
def test_auth_nonexistent(): request_headers = web.HTTPHeaders() request_headers.set('Authorization', 'Nonexistent none') request = mock.MockHTTPRequest(None, ('', 0), None, headers=request_headers, method='GET', handler=Handler) with pytest.raises(web.HTTPError) as error: request.handler.respond() assert error.value.headers.get( 'WWW-Authenticate') == 'Any realm="' + test_realm + '"' assert error.value.code == 401
def test_auth_basic(): request_headers = web.HTTPHeaders() request_headers.set('Authorization', 'Basic ' + base64.b64encode(b'a:a').decode()) request = mock.MockHTTPRequest(None, ('', 0), None, headers=request_headers, method='GET', handler=BasicHandler) headers, response = request.response.headers, request.handler.respond() assert headers.get('WWW-Authenticate') is None assert response[0] == 204
def test_form_multipart_extra(): request_headers = web.HTTPHeaders() request_headers.set('Content-Type', 'multipart/form-data; boundary=' + test_boundary) request = mock.MockHTTPRequest(None, ('', 0), None, body=test_mime_extra + test_separator + test_end, headers=request_headers, method='POST', handler=EchoHandler) request.handler.respond() assert request.body['binary_utf8'] == test_binary.decode()
def test_auth_basic_fail(): request_headers = web.HTTPHeaders() request_headers.set('Authorization', 'Basic ' + base64.b64encode(b'a:b').decode()) request = mock.MockHTTPRequest(None, ('', 0), None, headers=request_headers, method='GET', handler=BasicHandler) with pytest.raises(web.HTTPError) as error: request.handler.respond() assert error.value.headers.get( 'WWW-Authenticate') == 'Basic realm="' + test_realm + '"' assert error.value.code == 401
def test_form_urlencoded(): request_headers = web.HTTPHeaders() request_headers.set('Content-Type', 'application/x-www-form-urlencoded') request = mock.MockHTTPRequest(None, ('', 0), None, body=test_encoded, headers=request_headers, method='POST', handler=EchoHandler) request.handler.respond() assert request.body['test'] == 'tëst' assert request.body['foo'] == 'bar' assert request.body['wow'] == '' assert request.body['such'] == 'dog'
def test_form_multipart_too_many(): request_headers = web.HTTPHeaders() request_headers.set('Content-Type', 'multipart/form-data; boundary=' + test_boundary) request = mock.MockHTTPRequest(None, ('', 0), None, body=(test_mime_basic + test_separator) * (form.max_multipart_fragments + 1) + test_end, headers=request_headers, method='POST', handler=EchoHandler) with pytest.raises(web.HTTPError) as error: request.handler.respond() assert error.value.code == 413
def test_form_multipart_filename_bad_type(): request_headers = web.HTTPHeaders() request_headers.set('Content-Type', 'multipart/form-data; boundary=' + test_boundary) request = mock.MockHTTPRequest(None, ('', 0), None, body=test_mime_filename_bad_type + test_separator + test_end, headers=request_headers, method='POST', handler=EchoHandler) request.handler.respond() assert request.body['binary']['filename'] == 'binary' assert request.body['binary']['type'] == 'text/plain' assert request.body['binary']['length'] == len(test_binary) assert request.body['binary']['file'].read() == test_binary
def test_form_multipart_bad_mime(): request_headers = web.HTTPHeaders() request_headers.set( 'Content-Length', str(len(test_mime_basic + test_separator + test_end) + 1)) request_headers.set('Content-Type', 'multipart/form-data; boundary=' + test_boundary) request = mock.MockHTTPRequest(None, ('', 0), None, body=test_mime_basic + test_separator + test_end, headers=request_headers, method='POST', handler=EchoHandler) with pytest.raises(web.HTTPError) as error: request.handler.respond() assert error.value.code == 400
def test_form_multipart_multi(): request_headers = web.HTTPHeaders() request_headers.set('Content-Type', 'multipart/form-data; boundary=' + test_boundary) request = mock.MockHTTPRequest(None, ('', 0), None, body=test_mime_basic + test_separator + test_mime_filename + test_separator + test_mime_utf8 + test_separator + test_end, headers=request_headers, method='POST', handler=EchoHandler) request.handler.respond() assert request.body['body'] == test_body.decode() assert request.body['binary']['filename'] == 'binary' assert request.body['binary']['type'] == 'application/octet-stream' assert request.body['binary']['length'] == len(test_binary) assert request.body['binary']['file'].read() == test_binary assert request.body['binary_utf8'] == test_binary.decode()
def test_worker_handle_exception(): sync = multiprocessing.get_context(web.start_method).Manager() request_queue = sync.Queue() server = mock.MockHTTPServer(sync=sync) process = multiprocessing.get_context(web.start_method).Process( target=web.HTTPServer.worker, args=(server, 0, request_queue)) process.start() # wait a bit time.sleep(server.poll_interval + 1) try: request = mock.MockHTTPRequest(None, None, None, throw=True, namespace=server.namespace) with server.requests_lock: request_queue.put((request, False, None, True)) server.requests.value += 1 # wait another bit time.sleep(server.poll_interval + 1) assert request_queue.qsize() == 0 assert server.requests.value == 0 assert process.is_alive() finally: server.namespace.worker_shutdown = -1 process.join(timeout=server.poll_interval + 1) server.namespace.worker_shutdown = None