コード例 #1
0
    def test_data_schema_validation_failed(self):
        for method in ("POST", "PUT"):
            request = DummyRequest()
            request.method = method
            request.requestHeaders.setRawHeaders(
                "Content-Type", ["application/json"])
            request.requestHeaders.setRawHeaders(
                "Accept", ["application/json"])
            request.set_content(json.dumps({"bar": ""}))
            resource = Resource()
            resource.ALLOWED_CONTENT_TYPE = frozenset(["application/json"])
            resource.ALLOWED_ACCEPT = frozenset(["application/json"])
            resource.SCHEMAS = {
                method: Schema({Required("foo"): str})
            }
            method_impl = Mock(return_value=("", OK))
            setattr(resource, method.lower(), method_impl)

            with patch.object(resource, "error") as error:
                response = resource.render(request)

            self.assertEqual(response, NOT_DONE_YET)
            error.assert_called_once_with(
                request, BAD_REQUEST,
                "Failed to validate the request data against the schema: extra "
                "keys not allowed @ data[u'bar']"
            )
            self.assertFalse(method_impl.called)
コード例 #2
0
    def test_data_is_not_json(self):
        for method in ("POST", "PUT"):
            request = DummyRequest()
            request.method = method
            request.requestHeaders.setRawHeaders(
                "Content-Type", ["application/json"])
            request.requestHeaders.setRawHeaders(
                "Accept", ["application/json"])
            request.set_content("/")
            resource = Resource()
            resource.ALLOWED_CONTENT_TYPE = frozenset(["application/json"])
            resource.ALLOWED_ACCEPT = frozenset(["application/json"])
            method_impl = Mock()
            setattr(resource, method.lower(), method_impl)

            with patch.object(resource, "error") as error:
                response = resource.render(request)

            self.assertEqual(response, NOT_DONE_YET)
            error.assert_called_once_with(
                request, BAD_REQUEST,
                "Failed to decode json data: "
                "ValueError('No JSON object could be decoded',)"
            )
            self.assertFalse(method_impl.called)
コード例 #3
0
    def test_undefined_headers_processes_json(self):
        request = DummyRequest()
        request.method = "POST"
        request.set_content(dumps(self.data))
        resource = FakeAPIResource()
        resource.render(request)

        self.assertEqual(len(request.written), 1)
        self.assertEqual(loads(request.written[0]), self.data)
コード例 #4
0
    def test_rejects_when_accept_not_json(self):
        request = DummyRequest()
        request.method = "POST"
        request.set_content(dumps(self.data))
        request.requestHeaders.setRawHeaders("Accept", ["foobar"])

        resource = FakeAPIResource()
        resource.render(request)

        expected = {
            "error":
                "Can only handle one of %s "
                "here" % FakeAPIResource.ALLOWED_ACCEPT
        }
        self.assertEqual(len(request.written), 1)
        self.assertEqual(loads(request.written[0]), expected)
コード例 #5
0
    def test_unsupported_media_type(self):
        resource = Resource()
        resource.ALLOWED_CONTENT_TYPE = frozenset("")
        resource.ALLOWED_ACCEPT = frozenset("")
        resource.get = lambda **_: ""
        request = DummyRequest()
        request.requestHeaders.setRawHeaders("Accept", ["foobar"])
        request.set_content("hello")

        with patch.object(resource, "error") as error:
            response = resource.render(request)

        self.assertEqual(response, NOT_DONE_YET)
        error.assert_called_once_with(
            request, UNSUPPORTED_MEDIA_TYPE,
            "Can only support content type(s) frozenset([])"
        )