asyrjasalo / RESTinstance

Robot Framework library for RESTful JSON APIs
https://pypi.org/project/RESTinstance
GNU Lesser General Public License v3.0
205 stars 84 forks source link

Feature: Validation of response.reason #94

Closed Caritrian closed 3 years ago

Caritrian commented 4 years ago

I bring in this feature, because in my current project its necessary to validate not only the response.status also response.reason.

I walked through the code and extend _instantiate.response with "reason": response.reason (like in following code) in keywords.py.

    def _instantiate(self, request, response, validate_schema=True):
        try:
            response_body = response.json()
        except ValueError:
            response_body = response.text
            if response_body:
                logger.warn(
                    "Response body content is not JSON. "
                    + "Content-Type is: %s" % response.headers["Content-Type"]
                )
        response = {
            "seconds": response.elapsed.microseconds / 1000 / 1000,
            "status": response.status_code,
            "reason": response.reason,
            "body": response_body,
            "headers": dict(response.headers),
        }
        schema = deepcopy(self.schema)
        schema["title"] = "%s %s" % (request["method"], request["url"])
        try:
            schema["description"] = "%s: %s" % (
                BuiltIn().get_variable_value("${SUITE NAME}"),
                BuiltIn().get_variable_value("${TEST NAME}"),
            )
        except RobotNotRunningError:
            schema["description"] = ""
        request_properties = schema["properties"]["request"]["properties"]
        response_properties = schema["properties"]["response"]["properties"]
        if validate_schema:
            if request_properties:
                self._validate_schema(request_properties, request)
            if response_properties:
                self._validate_schema(response_properties, response)
        request_properties["body"] = self._new_schema(request["body"])
        request_properties["query"] = self._new_schema(request["query"])
        response_properties["body"] = self._new_schema(response["body"])
        if "default" in schema and schema["default"]:
            self._add_defaults_to_schema(schema, response)
        return {
            "request": request,
            "response": response,
            "schema": schema,
            "spec": self.spec,
        }

Currently i am not waiting for it, but if other people want to use it, it would be a great extension.

BR Kevin

asimell commented 3 years ago

Looks good with a quick glance. Can you make a pull request and add test cases to test the functionality?

asimell commented 3 years ago

This has been merged to Eficode fork of this repository as https://github.com/eficode/RESTinstance/pull/11. We'll make a release candidate PR to this repository once we reach all the milestones for the next release.