Exemplo n.º 1
0
    def test_resource_call_with_method_put(self):
        class Controller(object):
            def update(self, req, id, body):
                if expected_body != body:
                    msg = "The request body invalid"
                    raise webob.exc.HTTPBadRequest(explanation=msg)
                return "success"

        # verify the method: PUT
        app = fakes.TestRouter(Controller())
        req = webob.Request.blank('/tests/test_id',
                                  method="PUT",
                                  content_type='application/json')
        req.body = '{"body": {"key": "value"}}'
        expected_body = {'body': {"key": "value"}}
        response = req.get_response(app)
        self.assertEqual(response.body, 'success')
        self.assertEqual(response.status_int, 200)
        req.body = None
        expected_body = None
        response = req.get_response(app)
        self.assertEqual(response.status_int, 200)
        # verify no content_type is contained in the request
        req.content_type = None
        req.body = '{"body": {"key": "value"}}'
        response = req.get_response(app)
        expected_unsupported_type_body = {
            'badRequest': {
                'message': 'Unsupported Content-Type',
                'code': 400
            }
        }
        self.assertEqual(response.status_int, 400)
        self.assertEqual(expected_unsupported_type_body,
                         jsonutils.loads(response.body))
Exemplo n.º 2
0
    def test_resource_not_authorized(self):
        class Controller(object):
            def index(self, req):
                raise exception.Forbidden()

        req = webob.Request.blank('/tests')
        app = fakes.TestRouter(Controller())
        response = req.get_response(app)
        self.assertEqual(response.status_int, 403)
Exemplo n.º 3
0
    def test_get_request_id_no_response_body(self):
        class Controller(object):
            def index(self, req):
                pass

        req = fakes.HTTPRequest.blank('/tests')
        app = fakes.TestRouter(Controller())
        response = req.get_response(app)
        self.assertIn('patron.context', req.environ)
        self.assertEqual(response.body, '')
        self.assertEqual(response.status_int, 200)
Exemplo n.º 4
0
    def test_get_request_id_with_dict_response_body(self):
        class Controller(wsgi.Controller):
            def index(self, req):
                return {'foo': 'bar'}

        req = fakes.HTTPRequest.blank('/tests')
        app = fakes.TestRouter(Controller())
        response = req.get_response(app)
        self.assertIn('patron.context', req.environ)
        self.assertEqual(response.body, '{"foo": "bar"}')
        self.assertEqual(response.status_int, 200)
Exemplo n.º 5
0
    def test_resource_invalid_utf8(self):
        class Controller(object):
            def update(self, req, id, body):
                return body

        req = webob.Request.blank('/tests/test_id', method="PUT")
        body = """ {"name": "\xf0\x28\x8c\x28" } """
        req.body = body
        req.headers['Content-Type'] = 'application/json'
        app = fakes.TestRouter(Controller())
        self.assertRaises(UnicodeDecodeError, req.get_response, app)
Exemplo n.º 6
0
    def test_resource_no_project_prefix(self):
        class Controller(object):
            def index(self, req):
                return 'foo'

        app = fakes.TestRouter(Controller(),
                               openstack_api.PlainMapper())
        req = webob.Request.blank('/tests')
        resp = req.get_response(app)
        self.assertEqual(resp.body, 'foo')
        self.assertEqual(resp.status_int, 200)
Exemplo n.º 7
0
    def test_resource_valid_utf8_body(self):
        class Controller(object):
            def update(self, req, id, body):
                return body

        req = webob.Request.blank('/tests/test_id', method="PUT")
        body = """ {"name": "\xe6\xa6\x82\xe5\xbf\xb5" } """
        expected_body = '{"name": "\\u6982\\u5ff5"}'
        req.body = body
        req.headers['Content-Type'] = 'application/json'
        app = fakes.TestRouter(Controller())
        response = req.get_response(app)
        self.assertEqual(response.body, expected_body)
        self.assertEqual(response.status_int, 200)
Exemplo n.º 8
0
    def test_no_request_id_with_str_response_body(self):
        class Controller(wsgi.Controller):
            def index(self, req):
                return 'foo'

        req = fakes.HTTPRequest.blank('/tests')
        app = fakes.TestRouter(Controller())
        response = req.get_response(app)
        # NOTE(alaski): This test is really to ensure that a str response
        # doesn't error.  Not having a request_id header is a side effect of
        # our wsgi setup, ideally it would be there.
        expected_header = self.get_req_id_header_name(req)
        self.assertFalse(hasattr(response.headers, expected_header))
        self.assertEqual(response.body, 'foo')
        self.assertEqual(response.status_int, 200)
Exemplo n.º 9
0
    def test_resource_call_with_method_delete(self):
        class Controller(object):
            def delete(self, req, id):
                return "success"

        # verify the method: DELETE
        app = fakes.TestRouter(Controller())
        req = webob.Request.blank('/tests/test_id', method="DELETE")
        response = req.get_response(app)
        self.assertEqual(response.status_int, 200)
        self.assertEqual(response.body, 'success')
        # ignore the body
        req.body = '{"body": {"key": "value"}}'
        response = req.get_response(app)
        self.assertEqual(response.status_int, 200)
        self.assertEqual(response.body, 'success')
Exemplo n.º 10
0
    def test_resource_call_with_method_get(self):
        class Controller(object):
            def index(self, req):
                return 'success'

        app = fakes.TestRouter(Controller())
        # the default method is GET
        req = webob.Request.blank('/tests')
        response = req.get_response(app)
        self.assertEqual(response.body, 'success')
        self.assertEqual(response.status_int, 200)
        req.body = '{"body": {"key": "value"}}'
        response = req.get_response(app)
        self.assertEqual(response.body, 'success')
        self.assertEqual(response.status_int, 200)
        req.content_type = 'application/json'
        response = req.get_response(app)
        self.assertEqual(response.body, 'success')
        self.assertEqual(response.status_int, 200)
Exemplo n.º 11
0
    def test_resource_headers_are_utf8(self):
        resp = webob.Response(status_int=202)
        resp.headers['x-header1'] = 1
        resp.headers['x-header2'] = u'header2'
        resp.headers['x-header3'] = u'header3'

        class Controller(object):
            def index(self, req):
                return resp

        req = webob.Request.blank('/tests')
        app = fakes.TestRouter(Controller())
        response = req.get_response(app)

        for hdr, val in response.headers.iteritems():
            # All headers must be utf8
            self.assertIsInstance(hdr, str)
            self.assertIsInstance(val, str)
        self.assertEqual(response.headers['x-header1'], '1')
        self.assertEqual(response.headers['x-header2'], 'header2')
        self.assertEqual(response.headers['x-header3'], 'header3')
Exemplo n.º 12
0
    def test_resource_call_with_method_post(self):
        class Controller(object):
            @extensions.expected_errors(400)
            def create(self, req, body):
                if expected_body != body:
                    msg = "The request body invalid"
                    raise webob.exc.HTTPBadRequest(explanation=msg)
                return "success"

        # verify the method: POST
        app = fakes.TestRouter(Controller())
        req = webob.Request.blank('/tests',
                                  method="POST",
                                  content_type='application/json')
        req.body = '{"body": {"key": "value"}}'
        expected_body = {'body': {"key": "value"}}
        response = req.get_response(app)
        self.assertEqual(response.status_int, 200)
        self.assertEqual(response.body, 'success')
        # verify without body
        expected_body = None
        req.body = None
        response = req.get_response(app)
        self.assertEqual(response.status_int, 200)
        self.assertEqual(response.body, 'success')
        # the body is validated in the controller
        expected_body = {'body': None}
        response = req.get_response(app)
        expected_unsupported_type_body = {
            'badRequest': {
                'message': 'The request body invalid',
                'code': 400
            }
        }
        self.assertEqual(response.status_int, 400)
        self.assertEqual(expected_unsupported_type_body,
                         jsonutils.loads(response.body))