openapi-core icon indicating copy to clipboard operation
openapi-core copied to clipboard

Support validation of Response without providing Request

Open valentijnscholten opened this issue 3 years ago • 2 comments

Hi,

I was planning on using the (Django REST Framework) Response validation in some unit tests. These unit tests are performing different actions on the REST API and validate the result. I would like to validate the response against the schema as well.

The problem is, all the django unit tests are based on having a self.client that is called to performing actions using response = self.client.post()..., response = self.client.get(...), etc.

So I don't have access to the request that was sent, so I can't use the pattern provided in the examples:

from openapi_core.validation.response.validators import ResponseValidator

validator = ResponseValidator(spec)
result = validator.validate(request, response)

# raise errors if response invalid
result.raise_for_errors()

# get list of errors
errors = result.errors

I went through the validator code and from what I can see now the request object is mainly used to determine which operation & method was executed. I also see there's some refactoring going on on the master branch.

Is there any way to validate the response, without providing the request object?

I tried to make a "mock" request object, but I don't know enough about the (Django) request internals yet to know what attributes and values to set.

I am also interested to know how others are using the validation. Are you using it server site as some (Django) middleware that checks requests/responses?

valentijnscholten avatar May 24 '21 08:05 valentijnscholten

Hmm looks like the request is available as an attribute of the response: response.request. Doesn't seem to work yet, probably due to #247

valentijnscholten avatar May 24 '21 10:05 valentijnscholten

Hi @valentijnscholten

you can check DRF usage in the test for DRF https://github.com/p1c2u/openapi-core/blob/master/tests/integration/contrib/django/data/djangoproject/testapp/views.py

p1c2u avatar May 24 '21 15:05 p1c2u