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)
    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)
 def test_render_cache_header(self):
     path = self.create_file()
     request = DummyRequest(DummyChannel(), 1)
     request.method = "GET"
     static_path = StaticPath(path)
     static_path.render(request)
     self.assertEqual(
         request.responseHeaders.getRawHeaders("Cache-Control"),
         ["max-age=%s" % StaticPath.EXPIRES])
예제 #4
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)
    def test_method_not_allowed(self):
        resource = Resource()
        request = DummyRequest()
        request.method = "foobar"

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

        self.assertEqual(response, NOT_DONE_YET)
        error.assert_called_once_with(
            request, METHOD_NOT_ALLOWED,
            "Method %s is not supported" % request.method
        )
예제 #6
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)
 def test_data_empty(self):
     for method in ("POST", "PUT"):
         request = DummyRequest()
         request.method = method
         request.requestHeaders.setRawHeaders(
             "Content-Type", ["application/json"])
         request.requestHeaders.setRawHeaders(
             "Accept", ["application/json"])
         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)
         response = resource.render(request)
         self.assertEqual(response, NOT_DONE_YET)
         method_impl.assert_called_once_with(request=request)