def test_multiple_values(self, request_factory, request):
        request = request_factory('GET',
                                  '/',
                                  subdomain='www',
                                  query_string='a=b&a=c')

        openapi_request = RequestsOpenAPIRequest(request)

        path = {}
        query = ImmutableMultiDict([
            ('a', 'b'),
            ('a', 'c'),
        ])
        headers = request.headers
        cookies = {}
        assert openapi_request.parameters == RequestParameters(
            path=path,
            query=query,
            header=headers,
            cookie=cookies,
        )
        assert openapi_request.method == request.method.lower()
        assert openapi_request.full_url_pattern == 'http://localhost/'
        assert openapi_request.body == request.data
        assert openapi_request.mimetype == 'application/json'
예제 #2
0
    def test_multiple_values(self, request_factory, request):
        request = request_factory("GET",
                                  "/",
                                  subdomain="www",
                                  query_string="a=b&a=c")

        openapi_request = RequestsOpenAPIRequest(request)

        path = {}
        query = ImmutableMultiDict([
            ("a", "b"),
            ("a", "c"),
        ])
        headers = Headers(dict(request.headers))
        cookies = {}
        assert openapi_request.parameters == RequestParameters(
            path=path,
            query=query,
            header=headers,
            cookie=cookies,
        )
        prepared = request.prepare()
        assert openapi_request.method == request.method.lower()
        assert openapi_request.full_url_pattern == "http://localhost/"
        assert openapi_request.body == prepared.body
        assert openapi_request.mimetype == "application/json"
예제 #3
0
 def test_response_validator_path_pattern(self,
                                          spec,
                                          request_factory,
                                          response_factory):
     validator = ResponseValidator(spec)
     request = request_factory('GET', '/browse/12/', subdomain='kb')
     openapi_request = RequestsOpenAPIRequest(request)
     response = response_factory('{"data": "data"}', status_code=200)
     openapi_response = RequestsOpenAPIResponse(response)
     result = validator.validate(openapi_request, openapi_response)
     assert not result.errors
예제 #4
0
 def test_request_validator_path_pattern(self, spec):
     validator = RequestValidator(spec)
     request = requests.Request(
         'POST', 'http://localhost/browse/12/',
         params={'q': 'string'},
         headers={'content-type': 'application/json'},
         json={'param1': 1},
     )
     openapi_request = RequestsOpenAPIRequest(request)
     result = validator.validate(openapi_request)
     assert not result.errors
 def test_request_validator_path_pattern(self, spec):
     validator = RequestValidator(spec)
     request = requests.Request(
         "POST",
         "http://localhost/browse/12/",
         params={"q": "string"},
         headers={"content-type": "application/json"},
         json={"param1": 1},
     )
     openapi_request = RequestsOpenAPIRequest(request)
     result = validator.validate(openapi_request)
     assert not result.errors
 def test_response_validator_path_pattern(self, spec):
     responses.add(
         responses.GET, 'http://localhost/browse/12/',
         json={"data": "data"}, status=200)
     validator = ResponseValidator(spec)
     request = requests.Request('GET', 'http://localhost/browse/12/')
     request_prepared = request.prepare()
     session = requests.Session()
     response = session.send(request_prepared)
     openapi_request = RequestsOpenAPIRequest(request)
     openapi_response = RequestsOpenAPIResponse(response)
     result = validator.validate(openapi_request, openapi_response)
     assert not result.errors
 def test_response_validator_path_pattern(self, spec):
     responses.add(
         responses.POST, 'http://localhost/browse/12/?q=string',
         json={"data": "data"}, status=200, match_querystring=True,
     )
     validator = ResponseValidator(spec)
     request = requests.Request(
         'POST', 'http://localhost/browse/12/',
         params={'q': 'string'},
         headers={'content-type': 'application/json'},
         json={'param1': 1},
     )
     request_prepared = request.prepare()
     session = requests.Session()
     response = session.send(request_prepared)
     openapi_request = RequestsOpenAPIRequest(request)
     openapi_response = RequestsOpenAPIResponse(response)
     result = validator.validate(openapi_request, openapi_response)
     assert not result.errors
    def test_simple(self, request_factory, request):
        request = request_factory('GET', '/', subdomain='www')

        openapi_request = RequestsOpenAPIRequest(request)

        path = {}
        query = ImmutableMultiDict([])
        headers = request.headers
        cookies = {}
        prepared = request.prepare()
        assert openapi_request.parameters == RequestParameters(
            path=path,
            query=query,
            header=headers,
            cookie=cookies,
        )
        assert openapi_request.method == request.method.lower()
        assert openapi_request.full_url_pattern == 'http://localhost/'
        assert openapi_request.body == prepared.body
        assert openapi_request.mimetype == 'application/json'
    def test_url_rule(self, request_factory, request):
        request = request_factory('GET', '/browse/12/', subdomain='kb')

        openapi_request = RequestsOpenAPIRequest(request)

        # empty when not bound to spec
        path = {}
        query = ImmutableMultiDict([])
        headers = request.headers
        cookies = {}
        assert openapi_request.parameters == RequestParameters(
            path=path,
            query=query,
            header=headers,
            cookie=cookies,
        )
        assert openapi_request.method == request.method.lower()
        assert openapi_request.full_url_pattern == \
            'http://localhost/browse/12/'
        assert openapi_request.body == request.data
        assert openapi_request.mimetype == 'application/json'
예제 #10
0
    def test_url_rule(self, request_factory, request):
        request = request_factory("GET", "/browse/12/", subdomain="kb")

        openapi_request = RequestsOpenAPIRequest(request)

        # empty when not bound to spec
        path = {}
        query = ImmutableMultiDict([])
        headers = Headers({
            "Content-Type": "application/json",
        })
        cookies = {}
        assert openapi_request.parameters == RequestParameters(
            path=path,
            query=query,
            header=headers,
            cookie=cookies,
        )
        prepared = request.prepare()
        assert openapi_request.method == request.method.lower()
        assert (
            openapi_request.full_url_pattern == "http://localhost/browse/12/")
        assert openapi_request.body == prepared.body
        assert openapi_request.mimetype == "application/json"
예제 #11
0
 def test_response_validator_path_pattern(self, spec):
     responses.add(
         responses.POST,
         "http://localhost/browse/12/?q=string",
         json={"data": "data"},
         status=200,
         match_querystring=True,
         headers={"X-Rate-Limit": "12"},
     )
     validator = ResponseValidator(spec)
     request = requests.Request(
         "POST",
         "http://localhost/browse/12/",
         params={"q": "string"},
         headers={"content-type": "application/json"},
         json={"param1": 1},
     )
     request_prepared = request.prepare()
     session = requests.Session()
     response = session.send(request_prepared)
     openapi_request = RequestsOpenAPIRequest(request)
     openapi_response = RequestsOpenAPIResponse(response)
     result = validator.validate(openapi_request, openapi_response)
     assert not result.errors
 def test_request_validator_path_pattern(self, spec):
     validator = RequestValidator(spec)
     request = requests.Request('GET', 'http://localhost/browse/12/')
     openapi_request = RequestsOpenAPIRequest(request)
     result = validator.validate(openapi_request)
     assert not result.errors
예제 #13
0
 def test_request_validator_path_pattern(self, spec, request_factory):
     validator = RequestValidator(spec)
     request = request_factory('GET', '/browse/12/', subdomain='kb')
     openapi_request = RequestsOpenAPIRequest(request)
     result = validator.validate(openapi_request)
     assert not result.errors
예제 #14
0
    def call(self, method, *args, **kwargs):
        timeout = kwargs.pop("timeout", 10)
        openapi_validate = kwargs.pop("openapi_validate", True)

        if method not in self.methods:
            raise NotImplementedError("Method %s not implemented by %s API" %
                                      (method, self.name))

        payload = {}
        if args:
            if len(args) == 1:
                payload = args[0]
            else:
                payload = list(args)
        elif kwargs:
            payload = kwargs

        method_item = self.methods.get(method, {})
        _object = method_item.get("object", None)
        if _object is None:
            operation = "POST"
        else:
            operation = list(_object.operations.keys()).pop()

        params = {
            "method": operation.upper(),
            "url": self.base_url + method_item.get("path", ""),
            "headers": {
                "Content-Type": "application/json",
                "Accept": "application/json",
                "Connection": "close",
            },
        }
        if payload is not None:
            params["data"] = json.dumps(payload)

        # Generate a request
        request = requests.Request(**params)

        # Validate the request
        if openapi_validate:
            openapi_request = RequestsOpenAPIRequest(request)
            result = self.request_validator.validate(openapi_request)
            result.raise_for_errors()

        response = api_handler.send(request, timeout)
        self.response = response
        try:
            message = response.json()
        except Exception as e:
            logger.error("Exception decoding response: %s" % e)
            logger.debug("length: %s, headers: %s" %
                         (len(response.content), response.headers))
            with open("/tmp/failedresponse", "wb") as f:
                f.write(response.content)
            raise SwaggerAPIError(500, response.content)

        if int(response.status_code / 100) != 2:
            logger.error("Error %s on request %s" %
                         (response.status_code, params))
            raise SwaggerAPIError(response.status_code, message)

        # Validate the response
        if openapi_validate:
            openapi_response = RequestsOpenAPIResponse(response)
            # noinspection PyUnboundLocalVariable
            result = self.response_validator.validate(openapi_request,
                                                      openapi_response)
            result.raise_for_errors()

        return message