from typing import cast from hamcrest import * from http_types import RequestBuilder from openapi_typed_2 import Parameter, Schema from hmt.build.param import ParamBuilder, SchemaParameters from hmt.build.update_mode import UpdateMode req = RequestBuilder.from_url( "https://petstore.swagger.io/v1/pets?id=32&car=ferrari") def test_build_new_query(): query = req.query schema_query = ParamBuilder("query").build(query, UpdateMode.GEN) assert_that(schema_query, has_length(2)) query_parameter = get_query_parameter("id", schema_query) assert query_parameter.name == "id" assert query_parameter._in == "query" assert query_parameter.schema._type == "string" required_query_parameter = Parameter( description=None, deprecated=None, allowEmptyValue=None, style=None, explode=None,
from hmt.serve.mock.matcher import truncate_path from hmt.serve.mock.security import ( match_request_to_security_scheme, match_to_security_schemes, ) from hmt.serve.mock.specs import load_specs spec = load_specs("tests/serve/mock/schemas/nordea")[0].api spec_petstore = load_specs("tests/serve/mock/schemas/petstore")[0].api redirect_uri = "https://example.com/callback" state = "my-state" req = RequestBuilder.from_url( f"https://api.nordeaopenbanking.com/personal/v4/authorize?redirect_uri={redirect_uri}&state={state}" ) def test_truncate_path(): truncated = truncate_path(req.pathname, spec, req) assert truncated == "/v4/authorize" def test_match_to_oauth(): match = match_request_to_security_scheme(req, spec) assert_that(match, instance_of(Response)) def test_match_to_security_schemes(): match = match_to_security_schemes(req, (spec, spec_petstore))
from typing import List from hamcrest import * from http_types import RequestBuilder from openapi_typed_2 import Server from hmt.build.servers import normalize_path_if_matches petstore_req = RequestBuilder.from_url("https://petstore.swagger.io/v1/pets") petstore_server = Server(description=None, variables=None, _x=None, url="https://petstore.swagger.io/v1") def test_normalize_path_for_match(): norm_pathname = normalize_path_if_matches(petstore_req, [petstore_server]) assert_that(norm_pathname, is_("/pets")) def test_no_match(): req_no_match = RequestBuilder.from_url( "https://petstore.swagger.io/v2/pets") norm_pathname = normalize_path_if_matches(req_no_match, [petstore_server]) assert_that(norm_pathname, is_(None))
def test_no_match(): req_no_match = RequestBuilder.from_url( "https://petstore.swagger.io/v2/pets") norm_pathname = normalize_path_if_matches(req_no_match, [petstore_server]) assert_that(norm_pathname, is_(None))
assert_that(paths, has_item(matches_regexp(r"\/v2\/pokemon\/\{[\w]+\}"))) assert_that(paths, has_item(matches_regexp(r"\/v2\/type\/\{[\w]+\}"))) assert_that(paths, has_item(matches_regexp(r"\/v2\/ability\/\{[\w]+\}"))) def test_pokeapi_schema_valid_replay(schema): pokeapi_schema = build_schema_batch(pokeapi_requests, UpdateMode.REPLAY) paths = list(pokeapi_schema.paths.keys()) assert_that(paths, has_length(14)) assert_that(paths, has_item("/v2/pokemon/")) assert_that(paths, has_item(matches_regexp(r"\/v2\/pokemon\/[\w]+\/"))) assert_that(paths, has_item(matches_regexp(r"\/v2\/type\/[\w]+"))) assert_that(paths, has_item(matches_regexp(r"\/v2\/ability\/[\w]+"))) get_pets_req = RequestBuilder.from_url("http://petstore.swagger.io/v1/pets") get_one_pet_req = RequestBuilder.from_url( "http://petstore.swagger.io/v1/pets/32") pet_res = Response(bodyAsJson=None, timestamp=None, body="", statusCode=200, headers={}) @pytest.fixture def get_one_pet_exchange(): return HttpExchange(request=get_one_pet_req, response=pet_res)