def test_reserved_characters_in_operation_name(swagger_20): # See GH-992 # When an API operation name contains `:` operation = APIOperation("/foo:bar", "GET", {}, swagger_20) case = operation.make_case() # Then it should not be truncated during API call assert case.as_requests_kwargs("/")["url"] == "/foo:bar"
def test_get_container_invalid_location(): operation = APIOperation( path="/users/{user_id}", method="get", schema=None, definition=OperationDefinition( raw={}, resolved={}, scope="", parameters=[ OpenAPI30Parameter({ "in": "query", "name": "code", "type": "integer" }), OpenAPI30Parameter({ "in": "query", "name": "user_id", "type": "integer" }), OpenAPI30Parameter({ "in": "query", "name": "common", "type": "integer" }), ], ), ) case = operation.make_case() with pytest.raises( ValueError, match= "Parameter `unknown` is not defined in API operation `GET /users/{user_id}`" ): get_container(case, None, "unknown")
def test_call(override, base_url, swagger_20): operation = APIOperation("/success", "GET", {}, swagger_20) case = operation.make_case() if override: response = case.call(base_url) else: operation.base_url = base_url response = case.call() assert response.status_code == 200 assert response.json() == {"success": True} with pytest.warns(None) as records: del response assert not records
def test_as_requests_kwargs_override_user_agent(server, openapi2_base_url, swagger_20, headers, expected): operation = APIOperation("/success", "GET", {}, swagger_20, base_url=openapi2_base_url) original_headers = headers.copy() if headers is not None else headers case = operation.make_case(headers=headers) data = case.as_requests_kwargs(headers={"X-Key": "foo"}) assert data == { "headers": expected, "method": "GET", "params": None, "cookies": None, "url": f"http://127.0.0.1:{server['port']}/api/success", } assert case.headers == original_headers response = requests.request(**data) assert response.status_code == 200 assert response.json() == {"success": True}
def test_as_requests_kwargs(override, server, base_url, swagger_20, converter): base_url = converter(base_url) operation = APIOperation("/success", "GET", {}, swagger_20) case = operation.make_case(cookies={"TOKEN": "secret"}) if override: data = case.as_requests_kwargs(base_url) else: operation.base_url = base_url data = case.as_requests_kwargs() assert data == { "headers": { "User-Agent": USER_AGENT }, "method": "GET", "params": None, "cookies": { "TOKEN": "secret" }, "url": f"http://127.0.0.1:{server['port']}/api/success", } response = requests.request(**data) assert response.status_code == 200 assert response.json() == {"success": True}
def test_path(swagger_20): operation = APIOperation("/users/{name}", "GET", {}, swagger_20) case = operation.make_case(path_parameters={"name": "test"}) assert case.formatted_path == "/users/test"
def test_case_repr(swagger_20, kwargs, expected): operation = APIOperation("/users/{name}", "GET", {}, swagger_20) case = operation.make_case(**kwargs) assert repr(case) == expected