Ejemplo 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, response.body)
Ejemplo n.º 2
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, response.body)
Ejemplo n.º 3
0
    def test_resource_not_authorized(self):
        class Controller(object):
            def index(self, req):
                raise exception.NotAuthorized()

        req = webob.Request.blank('/tests')
        app = fakes.TestRouter(Controller())
        response = req.get_response(app)
        self.assertEqual(response.status_int, 403)
Ejemplo n.º 4
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)
Ejemplo n.º 5
0
    def test_resource_call(self):
        class Controller(object):
            def index(self, req):
                return 'off'

        req = webob.Request.blank('/tests')
        app = fakes.TestRouter(Controller())
        response = req.get_response(app)
        self.assertEqual(response.body, 'off')
        self.assertEqual(response.status_int, 200)
Ejemplo n.º 6
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)
Ejemplo n.º 7
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('nova.context', req.environ)
        self.assertEqual(response.body, '')
        self.assertEqual(response.status_int, 200)
Ejemplo n.º 8
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('nova.context', req.environ)
        self.assertEqual(response.body, '{"foo": "bar"}')
        self.assertEqual(response.status_int, 200)
Ejemplo n.º 9
0
    def test_get_request_id_no_response_body(self):
        class Controller(object):
            def index(self, req):
                pass

        req = fakes.HTTPRequest.blank('/tests')
        context = req.environ['nova.context']
        app = fakes.TestRouter(Controller())
        response = req.get_response(app)
        self.assertEqual(response.headers['x-compute-request-id'],
                         context.request_id)
        self.assertEqual(response.body, '')
        self.assertEqual(response.status_int, 200)
Ejemplo n.º 10
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.
        self.assertFalse(hasattr(response.headers, 'x-compute-request-id'))
        self.assertEqual(response.body, 'foo')
        self.assertEqual(response.status_int, 200)
Ejemplo n.º 11
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)
Ejemplo n.º 12
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')
Ejemplo n.º 13
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)
Ejemplo n.º 14
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')