Example #1
0
    def process(self, request: Request) -> Response:
        if request.method.value is None:
            method_error = "Could not find a method %s for path %s on hostname %s." % (
                request.method.value,
                request.path,
                request.host,
            )
            return self.match_error(method_error, request)

        specs = self._rest_middleware_manager.spew(request, self._specs)

        logger.debug("Matching to security schemes of %d specs", len(specs))
        maybe_security_response = match_to_security_schemes(
            request, [spec.api for spec in specs])

        if maybe_security_response is not None:
            return maybe_security_response

        pathname, spec = match_request_to_openapi(request, specs)

        if pathname is None:
            return self._callback_manager(
                request,
                self.match_error(
                    "Could not find an open API schema for the host {} and the path {}"
                    .format(request.host, request.path),
                    request,
                ),
                self._mock_data_store.default,
            )

        storage = self._mock_data_store[spec.source]
        response = self._match_response(
            pathname, typing.cast(OpenAPISpecification, spec), request)
        return self._callback_manager(request, response, storage)
Example #2
0
def test_matcher_10():
    assert (match_request_to_openapi(
        RequestBuilder.from_dict({
            "headers": {},
            "host": "api.baz.com",
            "path": "/guest",
            "pathname": "/guest",
            "protocol": "https",
            "method": "get",
            "query": {},  # query is not validated here
        }),
        store,
    )[0] == "/guest")
Example #3
0
def test_matcher_7():
    assert match_request_to_openapi(
        RequestBuilder.from_dict({
            "headers": {},
            "host": "api.foo.commmm",  # does not exist
            "path": "/user",
            "pathname": "/user",
            "protocol": "https",
            "method": "get",
            "query": {},
        }),
        store,
    ) == (None, None)
Example #4
0
def test_matcher_5():
    assert match_request_to_openapi(
        RequestBuilder.from_dict({
            "headers": {},
            "host": "api.foo.com",
            "path": "/user/fdsfsfwef",  # no validation on path params
            "pathname": "/user/fdsfsfwef",  # no validation on path params
            "protocol": "https",
            "method": "get",
            "query": {},
        }),
        store,
    ) == ("/user/{id}", store[0])
Example #5
0
def test_matcher_4():
    assert match_request_to_openapi(
        RequestBuilder.from_dict({
            "headers": {},
            "host": "api.foo.com",
            "path": "/user/55",  # correctly parses number
            "pathname": "/user/55",  # correcly parses number
            "protocol": "https",
            "method": "get",
            "query": {},
        }),
        store,
    ) == ("/user/{id}", store[0])
Example #6
0
def test_matcher_3():
    assert match_request_to_openapi(
        RequestBuilder.from_dict({
            "headers": {},
            "host": "api.foo.com",
            "path": "/users",  # incorrect, should be user
            "pathname": "/users",  # incorrect, should be user
            "protocol": "https",
            "method": "get",
            "query": {},
        }),
        store,
    ) == (None, None)
Example #7
0
def test_matcher_2():
    assert match_request_to_openapi(
        RequestBuilder.from_dict({
            "headers": {},
            "host": "api.bar.com",
            "path": "/v1/guest/{id}",
            "pathname": "/v1/guest/{id}",
            "protocol": "https",
            "method": "post",
            "query": {},
        }),
        store,
    ) == ("/guest/{id}", store[1])
Example #8
0
def test_matcher_14():
    assert (match_request_to_openapi(
        RequestBuilder.from_dict({
            "headers": {},  # no header will lead to undefined
            "host": "api.baz.com",
            "path": "/guest/4",
            "pathname": "/guest/4",
            "protocol": "https",
            "method": "post",
            "query": {
                "zzz": "aaa",
                "a": "foo",
                "b": "baz"
            },
        }),
        store,
    )[0] == "/guest/{id}")
Example #9
0
def test_matcher_12():
    assert (match_request_to_openapi(
        RequestBuilder.from_dict({
            "headers": {},
            "host": "api.baz.com",
            "path": "/guest/3/name",
            "pathname": "/guest/3/name",
            "protocol": "https",
            "method": "post",
            "query": {},
            "body": json.dumps({"age": "42"}),
            "bodyAsJson": {
                "age": "42"
            },  # wrong type, as 42 is string
        }),
        store,
    )[0] == "/guest/{id}/name")
Example #10
0
    def process(self, request: Request) -> Response:
        if request.method.value is None:
            method_error = "Could not find a method %s for path %s on hostname %s." % (
                request.method.value,
                request.path,
                request.host,
            )
            return self.match_error(method_error, request)

        specs = self._rest_middleware_manager.spew(request, self._specs)

        logger.debug("Matching to security schemes of %d specs", len(specs))
        maybe_security_response = match_to_security_schemes(
            request, [spec.api for spec in specs])

        if maybe_security_response is not None:
            logger.debug("Matched to security scheme, returning response.")
            return maybe_security_response

        match = match_request_to_openapi(request, specs)

        if len(match) == 0:
            return self._callback_manager(
                request,
                self.match_error(
                    "Could not find an open API schema for the host %s." %
                    request.host,
                    request,
                ),
                self._mock_data_store.default,
            )

        spec = random.choice(match)
        if spec.api.paths is None or len(spec.api.paths.items()) == 0:
            path_error = "Could not find a path %s on hostname %s." % (
                request.path,
                request.host,
            )
            return self.match_error(path_error, request)

        storage = self._mock_data_store[spec.source]
        response = self._match_response(spec, request)
        return self._callback_manager(request, response, storage)