Example #1
0
    def test_validation(self):
        app = TestApp(main({}))
        app.get('/service', status=400)

        res = app.post('/service', params='buh', status=400)
        self.assertTrue(b'Not a json body' in res.body)

        res = app.post('/service', params=json.dumps('buh'))

        self.assertEqual(res.body,
                         json.dumps({
                             'body': '"buh"'
                         }).encode('ascii'))

        app.get('/service?paid=yup')

        # valid = foo is one
        res = app.get('/service?foo=1&paid=yup')
        self.assertEqual(res.json['foo'], 1)

        # invalid value for foo
        res = app.get('/service?foo=buh&paid=yup', status=400)

        # check that json is returned
        errors = Errors.from_json(res.body)
        self.assertEqual(len(errors), 1)
Example #2
0
    def test_validation(self):
        app = TestApp(main({}))
        app.get('/service', status=400)

        res = app.post('/service', params='buh', status=400)
        self.assertTrue('Not a json body' in res.body)

        res = app.post('/service', params=json.dumps('buh'))

        self.assertEqual(res.body, json.dumps({'body': '"buh"'}))

        app.get('/service?paid=yup')

        # valid = foo is one
        res = app.get('/service?foo=1&paid=yup')
        self.assertEqual(res.json['foo'], 1)

        # invalid value for foo
        res = app.get('/service?foo=buh&paid=yup', status=400)

        # check that json is returned
        errors = Errors.from_json(res.body)
        self.assertEqual(len(errors), 1)

        # the "apidocs" registry entry contains all the needed information
        # to build up documentation
        # in this case, this means the function is registered and the argument
        # of the service are defined (e.g "validator" is set)
        apidocs = app.app.registry.settings['apidocs']

        self.assertTrue(_json in apidocs[('/service', 'POST')]['validators'])
Example #3
0
    def test_multiple_querystrings(self):
        app = TestApp(main({}))

        # it is possible to have more than one value with the same name in the
        # querystring
        self.assertEquals(b'{"field": ["5"]}', app.get("/foobaz?field=5").body)
        self.assertEquals(b'{"field": ["5", "2"]}', app.get("/foobaz?field=5&field=2").body)
Example #4
0
    def test_accept_issue_113_text_application_json(self):
        app = TestApp(main({}))

        res = app.get('/service3',
                      headers={'Accept': 'application/json'},
                      status=200)
        self.assertEquals(res.content_type, "application/json")
Example #5
0
    def test_accept(self):
        # tests that the accept headers are handled the proper way
        app = TestApp(main({}))

        # requesting the wrong accept header should return a 406 ...
        res = app.get('/service2', headers={'Accept': 'audio/*'}, status=406)

        # ... with the list of accepted content-types
        self.assertTrue('application/json' in res.json)
        self.assertTrue('text/json' in res.json)

        app.get('/service2', headers={'Accept': 'application/*'}, status=200)

        # it should also work with multiple Accept headers
        app.get('/service2', headers={'Accept': 'audio/*, application/*'},
                status=200)

        # test that using a callable to define what's accepted works as well
        res = app.get('/service3', headers={'Accept': 'audio/*'}, status=406)
        self.assertTrue('text/json' in res.json)

        app.get('/service3', headers={'Accept': 'text/*'}, status=200)

        # if we are not asking for a particular content-type, everything
        # should work just fine
        app.get('/service2', status=200)
Example #6
0
    def test_validation(self):
        app = TestApp(main({}))
        app.get('/service', status=400)

        res = app.post('/service', params='buh', status=400)
        self.assertTrue('Not a json body' in res.body)

        res = app.post('/service', params=json.dumps('buh'))

        self.assertEqual(res.body, json.dumps({'body': '"buh"'}))

        app.get('/service?paid=yup')

        # valid = foo is one
        res = app.get('/service?foo=1&paid=yup')
        self.assertEqual(res.json['foo'], 1)

        # invalid value for foo
        res = app.get('/service?foo=buh&paid=yup', status=400)

        # check that json is returned
        errors = Errors.from_json(res.body)
        self.assertEqual(len(errors), 1)

        # let's see the docstring !
        apidocs = app.app.registry.settings['apidocs']
        post_doc = apidocs[('/service', 'POST')]['docstring']
        self.assertEqual(post_doc, 'The request body should be a JSON object.')
Example #7
0
    def test_content_type_correct(self):
        # tests that the Content-Type request header satisfies the requirement
        app = TestApp(main({}))

        # requesting with one of the allowed Content-Type headers should work,
        # even when having a charset parameter as suffix
        response = app.put("/service5", headers={"Content-Type": "text/plain; charset=utf-8"})
        self.assertEqual(response.json, "some response")
Example #8
0
 def post(self, settings={}, headers={}):
     app = TestApp(main({}, **settings))
     return app.post_json('/foobar?yeah=test', {
         'foo': 'hello',
         'bar': 'open',
         'yeah': 'man',
         'ipsum': 10,
     }, status=400, headers=headers)
Example #9
0
    def test_multiple_querystrings(self):
        app = TestApp(main({}))

        # it is possible to have more than one value with the same name in the
        # querystring
        self.assertEquals(b'{"field": ["5"]}', app.get('/foobaz?field=5').body)
        self.assertEquals(b'{"field": ["5", "2"]}',
                          app.get('/foobaz?field=5&field=2').body)
Example #10
0
    def test_content_type_with_callable(self):
        # test that using a callable for content_type works as well
        app = TestApp(main({}))
        res = app.post('/service6', headers={'Content-Type': 'audio/*'}, status=415)
        error_description = res.json['errors'][0]['description']
        self.assertTrue('text/xml' in error_description)
        self.assertTrue('application/json' in error_description)

        app.post('/service6', headers={'Content-Type': 'text/xml'}, status=200)
Example #11
0
 def test_content_type_with_callable_returning_scalar(self):
     # Test that using a callable for content_type works as well.
     # Now, the callable returns a scalar instead of a list.
     app = TestApp(main({}))
     response = app.put('/service6',
                        headers={'Content-Type': 'audio/*'},
                        status=415)
     error_description = response.json['errors'][0]['description']
     self.assertIn('text/xml', error_description)
Example #12
0
    def test_content_type_missing_with_no_body_should_pass(self):
        # test that a Content-Type request headers is present
        app = TestApp(main({}))

        # requesting without a Content-Type header nor a body should
        # return a 200.
        request = app.RequestClass.blank('/service5', method='POST')
        response = app.do_request(request, 200, True)
        self.assertEqual(response.status_code, 200)
Example #13
0
 def test_content_type_with_callable(self):
     # Test that using a callable for content_type works as well.
     app = TestApp(main({}))
     response = app.post('/service6',
                         headers={'Content-Type': 'audio/*'},
                         status=415)
     error_description = response.json['errors'][0]['description']
     self.assertIn('text/xml', error_description)
     self.assertIn('application/json', error_description)
Example #14
0
    def test_content_type_correct(self):
        # tests that the Content-Type request header satisfies the requirement
        app = TestApp(main({}))

        # requesting with one of the allowed Content-Type headers should work,
        # even when having a charset parameter as suffix
        response = app.put(
            '/service5', headers={'Content-Type': 'text/plain; charset=utf-8'})
        self.assertEqual(response.json, "some response")
Example #15
0
    def test_content_type_with_callable(self):
        # test that using a callable for content_type works as well
        app = TestApp(main({}))
        res = app.post("/service6", headers={"Content-Type": "audio/*"}, status=415)
        error_description = res.json["errors"][0]["description"]
        self.assertTrue("text/xml" in error_description)
        self.assertTrue("application/json" in error_description)

        app.post("/service6", headers={"Content-Type": "text/xml"})
Example #16
0
    def test_accept_and_content_type(self):
        # tests that giving both Accept and Content-Type
        # request headers satisfy the requirement
        app = TestApp(main({}))

        # POST endpoint just has one accept and content_type definition
        response = app.post('/service7',
                            headers={
                                'Accept': 'text/xml, application/json',
                                'Content-Type':
                                'application/json; charset=utf-8'
                            },
                            status=200)
        self.assertEqual(response.json, "some response")

        response = app.post('/service7',
                            headers={
                                'Accept': 'text/plain, application/json',
                                'Content-Type':
                                'application/json; charset=utf-8'
                            },
                            status=406)

        response = app.post('/service7',
                            headers={
                                'Accept': 'text/xml, application/json',
                                'Content-Type':
                                'application/x-www-form-urlencoded'
                            },
                            status=415)

        # PUT endpoint has a list of accept and content_type definitions
        response = app.put('/service7',
                           headers={
                               'Accept': 'text/xml, application/json',
                               'Content-Type':
                               'application/json; charset=utf-8'
                           },
                           status=200)
        self.assertEqual(response.json, "some response")

        response = app.put('/service7',
                           headers={
                               'Accept': 'audio/*',
                               'Content-Type':
                               'application/json; charset=utf-8'
                           },
                           status=406)

        response = app.put('/service7',
                           headers={
                               'Accept': 'text/xml, application/json',
                               'Content-Type':
                               'application/x-www-form-urlencoded'
                           },
                           status=415)
Example #17
0
    def test_content_type_wrong_single(self):
        # tests that the Content-Type request header satisfies the requirement
        app = TestApp(main({}))

        # requesting the wrong Content-Type header should return a 415 ...
        response = app.post("/service5", headers={"Content-Type": "text/plain"}, status=415)

        # ... with an appropriate json error structure
        error_description = response.json["errors"][0]["description"]
        self.assertTrue("application/json" in error_description)
Example #18
0
    def test_accept_issue_113_audio_or_text(self):
        app = TestApp(main({}))

        res = app.get('/service2', headers={'Accept': 'audio/mp4; q=0.9, text/plain; q=0.5'}, status=200)
        self.assertEquals(res.content_type, "text/plain")

        # if we are not asking for a particular content-type,
        # we should get one of the two types that the service supports.
        r = app.get('/service2', status=200)
        self.assertTrue(r.content_type in ("application/json", "text/plain"))
Example #19
0
    def test_accept(self):
        # tests that the accept headers are handled the proper way
        app = TestApp(main({}))

        # requesting the wrong accept header should return a 406 ...
        response = app.get('/service2',
                           headers={'Accept': 'audio/*'},
                           status=406)

        # ... with the list of accepted content-types
        error_location = response.json['errors'][0]['location']
        error_name = response.json['errors'][0]['name']
        error_description = response.json['errors'][0]['description']
        self.assertEquals('header', error_location)
        self.assertEquals('Accept', error_name)
        self.assertTrue('application/json' in error_description)
        self.assertTrue('text/json' in error_description)
        self.assertTrue('text/plain' in error_description)

        # requesting a supported type should give an appropriate response type
        response = app.get('/service2', headers={'Accept': 'application/*'})
        self.assertEqual(response.content_type, "application/json")

        response = app.get('/service2', headers={'Accept': 'text/plain'})
        self.assertEqual(response.content_type, "text/plain")

        # it should also work with multiple Accept headers
        response = app.get('/service2',
                           headers={'Accept': 'audio/*, application/*'})
        self.assertEqual(response.content_type, "application/json")

        # and requested preference order should be respected
        headers = {'Accept': 'application/json; q=1.0, text/plain; q=0.9'}
        response = app.get('/service2', headers=headers)
        self.assertEqual(response.content_type, "application/json")

        headers = {'Accept': 'application/json; q=0.9, text/plain; q=1.0'}
        response = app.get('/service2', headers=headers)
        self.assertEqual(response.content_type, "text/plain")

        # test that using a callable to define what's accepted works as well
        response = app.get('/service3',
                           headers={'Accept': 'audio/*'},
                           status=406)
        error_description = response.json['errors'][0]['description']
        self.assertTrue('text/json' in error_description)

        response = app.get('/service3', headers={'Accept': 'text/*'})
        self.assertEqual(response.content_type, "text/json")

        # if we are not asking for a particular content-type,
        # we should get one of the two types that the service supports.
        response = app.get('/service2')
        self.assertTrue(response.content_type in ("application/json",
                                                  "text/plain"))
Example #20
0
    def test_accept(self):
        # tests that the accept headers are handled the proper way
        app = TestApp(main({}))

        # requesting the wrong accept header should return a 406 ...
        response = app.get('/service2', headers={'Accept': 'audio/*'},
                           status=406)

        # ... with the list of accepted content-types
        error_location = response.json['errors'][0]['location']
        error_name = response.json['errors'][0]['name']
        error_description = response.json['errors'][0]['description']
        self.assertEquals('header', error_location)
        self.assertEquals('Accept', error_name)
        self.assertTrue('application/json' in error_description)
        self.assertTrue('text/json' in error_description)
        self.assertTrue('text/plain' in error_description)

        # requesting a supported type should give an appropriate response type
        response = app.get('/service2', headers={'Accept': 'application/*'})
        self.assertEqual(response.content_type, "application/json")

        response = app.get('/service2', headers={'Accept': 'text/plain'})
        self.assertEqual(response.content_type, "text/plain")

        # it should also work with multiple Accept headers
        response = app.get('/service2', headers={
            'Accept': 'audio/*, application/*'
        })
        self.assertEqual(response.content_type, "application/json")

        # and requested preference order should be respected
        headers = {'Accept': 'application/json; q=1.0, text/plain; q=0.9'}
        response = app.get('/service2', headers=headers)
        self.assertEqual(response.content_type, "application/json")

        headers = {'Accept': 'application/json; q=0.9, text/plain; q=1.0'}
        response = app.get('/service2', headers=headers)
        self.assertEqual(response.content_type, "text/plain")

        # test that using a callable to define what's accepted works as well
        response = app.get('/service3', headers={'Accept': 'audio/*'},
                           status=406)
        error_description = response.json['errors'][0]['description']
        self.assertTrue('text/json' in error_description)

        response = app.get('/service3', headers={'Accept': 'text/*'})
        self.assertEqual(response.content_type, "text/json")

        # if we are not asking for a particular content-type,
        # we should get one of the two types that the service supports.
        response = app.get('/service2')
        self.assertTrue(response.content_type
                        in ("application/json", "text/plain"))
Example #21
0
    def test_content_type_wrong_single(self):
        # tests that the Content-Type request header satisfies the requirement
        app = TestApp(main({}))

        # requesting the wrong Content-Type header should return a 415 ...
        response = app.post('/service5',
            headers={'Content-Type': 'text/plain'}, status=415)

        # ... with an appropriate json error structure
        error_description = response.json['errors'][0]['description']
        self.assertTrue('application/json' in error_description)
Example #22
0
    def test_accept_issue_113_audio_or_text(self):
        app = TestApp(main({}))

        res = app.get(
            '/service2',
            headers={'Accept': 'audio/mp4; q=0.9, text/plain; q=0.5'})
        self.assertEqual(res.content_type, "text/plain")

        # if we are not asking for a particular content-type,
        # we should get one of the two types that the service supports.
        r = app.get('/service2')
        self.assertTrue(r.content_type in ("application/json", "text/plain"))
Example #23
0
    def test_override_default_accept_issue_252(self):
        # override default acceptable content_types for interoperate with
        # legacy applications i.e. ExtJS 3
        from cornice.util import _JsonRenderer
        _JsonRenderer.acceptable += ('text/html',)

        app = TestApp(main({}))

        response = app.get('/service5', headers={'Accept': 'text/html'})
        self.assertEqual(response.content_type, "text/html")
        # revert the override
        _JsonRenderer.acceptable = _JsonRenderer.acceptable[:-1]
Example #24
0
    def test_content_type_wrong_single(self):
        # tests that the Content-Type request header satisfies the requirement
        app = TestApp(main({}))

        # requesting the wrong Content-Type header should return a 415 ...
        response = app.post('/service5',
                            headers={'Content-Type': 'text/plain'},
                            status=415)

        # ... with an appropriate json error structure
        error_description = response.json['errors'][0]['description']
        self.assertTrue('application/json' in error_description)
Example #25
0
    def test_override_default_accept_issue_252(self):
        # override default acceptable content_types for interoperate with
        # legacy applications i.e. ExtJS 3
        from cornice.util import _JsonRenderer
        _JsonRenderer.acceptable += ('text/html',)

        app = TestApp(main({}))

        response = app.get('/service5', headers={'Accept': 'text/html'})
        self.assertEqual(response.content_type, "text/html")
        # revert the override
        _JsonRenderer.acceptable = _JsonRenderer.acceptable[:-1]
Example #26
0
    def test_content_type_wrong_multiple(self):
        # Tests that the Content-Type request header satisfies the requirement.
        app = TestApp(main({}))

        # Requesting without a Content-Type header should
        # return "415 Unsupported Media Type" ...
        response = app.put('/service5',
                           headers={'Content-Type': 'text/xml'},
                           status=415)

        # ... with an appropriate json error structure.
        error_description = response.json['errors'][0]['description']
        self.assertIn('text/plain', error_description)
        self.assertIn('application/json', error_description)
Example #27
0
    def test_accept(self):
        # tests that the accept headers are handled the proper way
        app = TestApp(main({}))

        # requesting the wrong accept header should return a 406 ...
        res = app.get("/service2", headers={"Accept": "audio/*"}, status=406)

        # ... with the list of accepted content-types
        error_location = res.json["errors"][0]["location"]
        error_name = res.json["errors"][0]["name"]
        error_description = res.json["errors"][0]["description"]
        self.assertEquals("header", error_location)
        self.assertEquals("Accept", error_name)
        self.assertTrue("application/json" in error_description)
        self.assertTrue("text/json" in error_description)
        self.assertTrue("text/plain" in error_description)

        # requesting a supported type should give an appropriate response type
        r = app.get("/service2", headers={"Accept": "application/*"})
        self.assertEqual(r.content_type, "application/json")

        r = app.get("/service2", headers={"Accept": "text/plain"})
        self.assertEqual(r.content_type, "text/plain")

        # it should also work with multiple Accept headers
        r = app.get("/service2", headers={"Accept": "audio/*, application/*"})
        self.assertEqual(r.content_type, "application/json")

        # and requested preference order should be respected
        headers = {"Accept": "application/json; q=1.0, text/plain; q=0.9"}
        r = app.get("/service2", headers=headers)
        self.assertEqual(r.content_type, "application/json")

        headers = {"Accept": "application/json; q=0.9, text/plain; q=1.0"}
        r = app.get("/service2", headers=headers)
        self.assertEqual(r.content_type, "text/plain")

        # test that using a callable to define what's accepted works as well
        res = app.get("/service3", headers={"Accept": "audio/*"}, status=406)
        error_description = res.json["errors"][0]["description"]
        self.assertTrue("text/json" in error_description)

        res = app.get("/service3", headers={"Accept": "text/*"})
        self.assertEqual(res.content_type, "text/json")

        # if we are not asking for a particular content-type,
        # we should get one of the two types that the service supports.
        r = app.get("/service2")
        self.assertTrue(r.content_type in ("application/json", "text/plain"))
Example #28
0
    def test_accept_and_content_type(self):
        # tests that giving both Accept and Content-Type
        # request headers satisfy the requirement
        app = TestApp(main({}))

        # POST endpoint just has one accept and content_type definition
        response = app.post('/service7', headers={
            'Accept': 'text/xml, application/json',
            'Content-Type': 'application/json; charset=utf-8'
        })
        self.assertEqual(response.json, "some response")

        response = app.post(
            '/service7',
            headers={
                'Accept': 'text/plain, application/json',
                'Content-Type': 'application/json; charset=utf-8'
            },
            status=406)

        response = app.post(
            '/service7',
            headers={
                'Accept': 'text/xml, application/json',
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            status=415)

        # PUT endpoint has a list of accept and content_type definitions
        response = app.put('/service7', headers={
            'Accept': 'text/xml, application/json',
            'Content-Type': 'application/json; charset=utf-8'
        })
        self.assertEqual(response.json, "some response")

        response = app.put(
            '/service7',
            headers={
                'Accept': 'audio/*',
                'Content-Type': 'application/json; charset=utf-8'
            },
            status=406)

        response = app.put(
            '/service7',
            headers={
                'Accept': 'text/xml, application/json',
                'Content-Type': 'application/x-www-form-urlencoded'
            }, status=415)
Example #29
0
    def test_content_type_missing(self):
        # test that a Content-Type request headers is present
        app = TestApp(main({}))

        # requesting without a Content-Type header should return a 415 ...
        request = app.RequestClass.blank("/service5", method="POST")
        response = app.do_request(request, 415, True)

        # ... with an appropriate json error structure
        error_location = response.json["errors"][0]["location"]
        error_name = response.json["errors"][0]["name"]
        error_description = response.json["errors"][0]["description"]
        self.assertEqual("header", error_location)
        self.assertEqual("Content-Type", error_name)
        self.assertTrue("application/json" in error_description)
Example #30
0
    def test_content_type_missing(self):
        # test that a Content-Type request headers is present
        app = TestApp(main({}))

        # requesting without a Content-Type header should return a 415 ...
        request = app.RequestClass.blank('/service5', method='POST')
        response = app.do_request(request, 415, True)

        # ... with an appropriate json error structure
        error_location = response.json['errors'][0]['location']
        error_name = response.json['errors'][0]['name']
        error_description = response.json['errors'][0]['description']
        self.assertEqual('header', error_location)
        self.assertEqual('Content-Type', error_name)
        self.assertTrue('application/json' in error_description)
Example #31
0
    def test_content_type_missing(self):
        # test that a Content-Type request headers is present
        app = TestApp(main({}))

        # requesting without a Content-Type header should return a 415 ...
        request = app.RequestClass.blank('/service5', method='POST')
        response = app.do_request(request, 415, True)

        # ... with an appropriate json error structure
        error_location = response.json['errors'][0]['location']
        error_name = response.json['errors'][0]['name']
        error_description = response.json['errors'][0]['description']
        self.assertEqual('header', error_location)
        self.assertEqual('Content-Type', error_name)
        self.assertTrue('application/json' in error_description)
Example #32
0
    def test_content_type_missing(self):
        # test that a Content-Type request headers is present
        app = TestApp(main({}))

        # Requesting without a Content-Type header should
        # return "415 Unsupported Media Type" ...
        request = app.RequestClass.blank('/service5',
                                         method='POST',
                                         POST="some data")
        response = app.do_request(request, 415, True)
        self.assertEqual(response.status_code, 415)

        # ... with an appropriate json error structure.
        error_location = response.json['errors'][0]['location']
        error_name = response.json['errors'][0]['name']
        error_description = response.json['errors'][0]['description']
        self.assertEqual('header', error_location)
        self.assertEqual('Content-Type', error_name)
        self.assertIn('application/json', error_description)
Example #33
0
    def test_accept(self):
        # tests that the accept headers are handled the proper way
        app = TestApp(main({}))

        # requesting the wrong accept header should return a 406 ...
        res = app.get('/service2', headers={'Accept': 'audio/*'}, status=406)

        # ... with the list of accepted content-types
        self.assertTrue('application/json' in res.json)
        self.assertTrue('text/json' in res.json)
        self.assertTrue('text/plain' in res.json)

        # requesting a supported type should give an appropriate response type
        r = app.get('/service2', headers={'Accept': 'application/*'})
        self.assertEquals(r.content_type, "application/json")

        r = app.get('/service2', headers={'Accept': 'text/plain'})
        self.assertEquals(r.content_type, "text/plain")

        # it should also work with multiple Accept headers
        r = app.get('/service2', headers={'Accept': 'audio/*, application/*'})
        self.assertEquals(r.content_type, "application/json")

        # and requested preference order should be respected
        r = app.get('/service2',
                    headers={'Accept': 'application/json, text/plain'})
        self.assertEquals(r.content_type, "application/json")

        r = app.get('/service2',
                    headers={'Accept': 'text/plain, application/json'})
        self.assertEquals(r.content_type, "application/json")

        # test that using a callable to define what's accepted works as well
        res = app.get('/service3', headers={'Accept': 'audio/*'}, status=406)
        self.assertTrue('text/json' in res.json)

        res = app.get('/service3', headers={'Accept': 'text/*'}, status=200)
        self.assertEquals(res.content_type, "text/json")

        # if we are not asking for a particular content-type,
        # we should get the type defined by outermost declaration.
        r = app.get('/service2', status=200)
        self.assertEquals(r.content_type, "application/json")
Example #34
0
    def test_accept(self):
        # tests that the accept headers are handled the proper way
        app = TestApp(main({}))

        # requesting the wrong accept header should return a 406 ...
        res = app.get('/service2', headers={'Accept': 'audio/*'}, status=406)

        # ... with the list of accepted content-types
        self.assertTrue('application/json' in res.json)
        self.assertTrue('text/json' in res.json)
        self.assertTrue('text/plain' in res.json)

        # requesting a supported type should give an appropriate response type
        r = app.get('/service2', headers={'Accept': 'application/*'})
        self.assertEquals(r.content_type, "application/json")

        r = app.get('/service2', headers={'Accept': 'text/plain'})
        self.assertEquals(r.content_type, "text/plain")

        # it should also work with multiple Accept headers
        r = app.get('/service2', headers={'Accept': 'audio/*, application/*'})
        self.assertEquals(r.content_type, "application/json")

        # and requested preference order should be respected
        r = app.get('/service2',
                    headers={'Accept': 'application/json, text/plain'})
        self.assertEquals(r.content_type, "application/json")

        r = app.get('/service2',
                    headers={'Accept': 'text/plain, application/json'})
        self.assertEquals(r.content_type, "application/json")

        # test that using a callable to define what's accepted works as well
        res = app.get('/service3', headers={'Accept': 'audio/*'}, status=406)
        self.assertTrue('text/json' in res.json)

        app.get('/service3', headers={'Accept': 'text/*'}, status=200)

        # if we are not asking for a particular content-type,
        # we should get the type defined by outermost declaration.
        r = app.get('/service2', status=200)
        self.assertEquals(r.content_type, "application/json")
Example #35
0
    def test_accept(self):
        # tests that the accept headers are handled the proper way
        app = TestApp(main({}))

        # requesting the wrong accept header should return a 406 ...
        res = app.get('/service2', headers={'Accept': 'audio/*'}, status=406)

        # ... with the list of accepted content-types
        self.assertTrue('application/json' in res.json)

        app.get('/service2', headers={'Accept': 'application/*'}, status=200)

        # it should also work with multiple Accept headers
        app.get('/service2', headers={'Accept': 'audio/*, application/*'},
                status=200)

        # test that using a callable to define what's accepted works as well
        app.get('/service3', headers={'Accept': 'audio/*'}, status=406)
        app.get('/service3', headers={'Accept': 'text/*'}, status=200)

        # if we are not asking for a particular content-type, everything
        # should work just fine
        app.get('/service2', status=200)
Example #36
0
    def test_validation(self):
        app = TestApp(main({}))
        app.get('/service', status=400)

        res = app.post('/service', params='buh', status=400)
        self.assertTrue('Not a json body' in res.body)

        res = app.post('/service', params=json.dumps('buh'))

        self.assertEqual(res.body, json.dumps({'body': '"buh"'}))

        app.get('/service?paid=yup')

        # valid = foo is one
        res = app.get('/service?foo=1&paid=yup')
        self.assertEqual(res.json['foo'], 1)

        # invalid value for foo
        res = app.get('/service?foo=buh&paid=yup', status=400)

        # check that json is returned
        errors = Errors.from_json(res.body)
        self.assertEqual(len(errors), 1)
Example #37
0
    def test_validation(self):
        app = TestApp(main({}))

        # 413
        self.assertRaises(AppError, app.get, '/service')

        self.assertRaises(AppError, app.post, '/service', params='buh')
        res = app.post('/service', params=json.dumps('buh'))

        self.assertEqual(res.body, json.dumps({'body': '"buh"'}))

        app.get('/service?paid=yup')

        # valid = foo is one
        res = app.get('/service?foo=1&paid=yup')
        self.assertEqual(res.json['foo'], 1)

        # invalid value for foo
        self.assertRaises(AppError, app.get, '/service?foo=buh&paid=yup')

        # let's see the docstring !
        apidocs = app.app.registry.settings['apidocs']
        post_doc = apidocs[('/service', 'POST')]['docstring']
        self.assertEqual(post_doc, 'The request body should be a JSON object.')
    def test_validation(self):
        app = TestApp(main({}))

        # 413
        self.assertRaises(AppError, app.get, '/service')

        self.assertRaises(AppError, app.post, '/service', params='buh')
        res = app.post('/service', params=json.dumps('buh'))

        self.assertEqual(res.body, json.dumps({'body': '"buh"'}))

        app.get('/service?paid=yup')

        # valid = foo is one
        res = app.get('/service?foo=1&paid=yup')
        self.assertEqual(res.json['foo'], 1)

        # invalid value for foo
        self.assertRaises(AppError, app.get, '/service?foo=buh&paid=yup')

        # let's see the docstring !
        apidocs = app.app.registry.settings['apidocs']
        post_doc = apidocs[('/service', 'POST')]['docstring']
        self.assertEqual(post_doc, 'The request body should be a JSON object.')
Example #39
0
    def test_validation(self):
        app = TestApp(main({}))
        app.get("/service", status=400)

        res = app.post("/service", params="buh", status=400)
        self.assertTrue(b"Not a json body" in res.body)

        res = app.post("/service", params=json.dumps("buh"))

        expected = json.dumps({"body": '"buh"'}).encode("ascii")
        self.assertEqual(res.body, expected)

        app.get("/service?paid=yup")

        # valid = foo is one
        res = app.get("/service?foo=1&paid=yup")
        self.assertEqual(res.json["foo"], 1)

        # invalid value for foo
        res = app.get("/service?foo=buh&paid=yup", status=400)

        # check that json is returned
        errors = Errors.from_json(res.body)
        self.assertEqual(len(errors), 1)
Example #40
0
    def test_accept_issue_113_audio_or_text(self):
        app = TestApp(main({}))

        res = app.get('/service2', headers={'Accept': 'audio/mp4; q=0.9, text/plain; q=0.5'}, status=200)
        self.assertEquals(res.content_type, "text/plain")
Example #41
0
    def test_accept_issue_113_text_application_star(self):
        app = TestApp(main({}))

        response = app.get('/service3', headers={'Accept': 'application/*'})
        self.assertEqual(response.content_type, "application/json")
Example #42
0
    def test_accept_issue_113_text_html_not_acceptable(self):
        app = TestApp(main({}))

        # Requesting an unsupported content type should
        # return HTTP response "406 Not Acceptable".
        app.get('/service3', headers={'Accept': 'text/html'}, status=406)
Example #43
0
 def make_ordinary_app(self):
     return TestApp(main({}))
Example #44
0
    def test_validation_hooked_error_response(self):
        app = TestApp(main({}))

        res = app.post('/service4', status=400)
        self.assertTrue('<errors>' in res.body)
Example #45
0
    def test_validation_hooked_error_response(self):
        app = TestApp(main({}))

        res = app.post('/service4', status=400)
        self.assertTrue(b'<errors>' in res.body)
Example #46
0
    def test_accept_issue_113_text_star(self):
        app = TestApp(main({}))

        res = app.get('/service3', headers={'Accept': 'text/*'})
        self.assertEqual(res.content_type, "text/json")
Example #47
0
    def test_accept_issue_113_text_star(self):
        app = TestApp(main({}))

        response = app.get('/service3', headers={'Accept': 'text/*'})
        self.assertEqual(response.content_type, "text/json")
Example #48
0
 def test_content_type_on_get(self):
     # test that a Content-Type request header is not
     # checked on GET requests, they don't usually have a body
     app = TestApp(main({}))
     response = app.get('/service5')
     self.assertEqual(response.json, "some response")
Example #49
0
    def test_accept_issue_113_text_html_not_acceptable(self):
        app = TestApp(main({}))

        # requesting an unsupported content type should return a HTTP 406 (Not
        # Acceptable)
        app.get('/service3', headers={'Accept': 'text/html'}, status=406)
Example #50
0
    def test_accept_issue_113_text_application_json(self):
        app = TestApp(main({}))

        response = app.get('/service3', headers={'Accept': 'application/json'})
        self.assertEqual(response.content_type, "application/json")
Example #51
0
 def test_email_field(self):
     app = TestApp(main({}))
     content = json.dumps({'email': '*****@*****.**'})
     app.post('/newsletter', params=content)
Example #52
0
 def test_email_field(self):
     app = TestApp(main({}))
     content = {'email': '*****@*****.**'}
     app.post_json('/newsletter', params=content)
Example #53
0
 def test_content_type_on_get(self):
     # test that a Content-Type request header is not
     # checked on GET requests, they don't usually have a body
     app = TestApp(main({}))
     response = app.get('/service5')
     self.assertEqual(response.json, "some response")
Example #54
0
    def test_accept_issue_113_text_html_not_acceptable(self):
        app = TestApp(main({}))

        # requesting an unsupported content type should return a HTTP 406 (Not
        # Acceptable)
        app.get('/service3', headers={'Accept': 'text/html'}, status=406)
Example #55
0
 def make_ordinary_app(self):
     return TestApp(main({}))
Example #56
0
    def test_filters(self):
        app = TestApp(main({}))

        # filters can be applied to all the methods of a service
        self.assertTrue("filtered response" in app.get('/filtered').body)
        self.assertTrue("unfiltered" in app.post('/filtered').body)
Example #57
0
    def test_accept_issue_113_text_application_star(self):
        app = TestApp(main({}))

        res = app.get('/service3', headers={'Accept': 'application/*'}, status=200)
        self.assertEquals(res.content_type, "application/json")