コード例 #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 = b'{"body": {"key": "value"}}'
     expected_body = {'body': {
         "key": "value"
         }
     }
     response = req.get_response(app)
     self.assertEqual(b'success', response.body)
     self.assertEqual(response.status_int, http.OK)
     req.body = None
     expected_body = None
     response = req.get_response(app)
     self.assertEqual(response.status_int, http.OK)
     # verify no content_type is contained in the request
     req = webob.Request.blank('/tests/test_id', method="PUT",
                               content_type='application/xml')
     req.content_type = 'application/xml'
     req.body = b'{"body": {"key": "value"}}'
     response = req.get_response(app)
     expected_unsupported_type_body = {'badMediaType':
         {'message': 'Unsupported Content-Type',
          'code': http.UNSUPPORTED_MEDIA_TYPE}}
     self.assertEqual(response.status_int, http.UNSUPPORTED_MEDIA_TYPE)
     self.assertEqual(expected_unsupported_type_body,
                      jsonutils.loads(response.body))
コード例 #2
0
 def test_resource_call_with_method_post(self):
     class Controller(object):
         @extensions.expected_errors(http.BAD_REQUEST)
         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 = b'{"body": {"key": "value"}}'
     expected_body = {'body': {
         "key": "value"
         }
     }
     response = req.get_response(app)
     self.assertEqual(response.status_int, http.OK)
     self.assertEqual(b'success', response.body)
     # verify without body
     expected_body = None
     req.body = None
     response = req.get_response(app)
     self.assertEqual(response.status_int, http.OK)
     self.assertEqual(b'success', response.body)
     # 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': http.BAD_REQUEST}}
     self.assertEqual(response.status_int, http.BAD_REQUEST)
     self.assertEqual(expected_unsupported_type_body,
                      jsonutils.loads(response.body))
コード例 #3
0
ファイル: test_wsgi.py プロジェクト: iorchard/okidoki
    def test_resource_headers_py2_are_utf8(self):
        resp = webob.Response(status_int=http.ACCEPTED)
        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)

        if six.PY2:
            for val in response.headers.values():
                # All headers must be utf8
                self.assertThat(val, matchers.EncodedByUTF8())
            self.assertEqual(b'1', response.headers['x-header1'])
            self.assertEqual(b'header2', response.headers['x-header2'])
            self.assertEqual(b'header3', response.headers['x-header3'])
        else:
            self.assertEqual('1', response.headers['x-header1'])
            self.assertEqual(u'header2', response.headers['x-header2'])
            self.assertEqual(u'header3', response.headers['x-header3'])
コード例 #4
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, http.FORBIDDEN)
コード例 #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 = b""" {"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)
コード例 #6
0
    def test_get_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('masakari.context', req.environ)
        self.assertEqual(b'', response.body)
        self.assertEqual(response.status_int, http.OK)
コード例 #7
0
    def test_get_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('masakari.context', req.environ)
        self.assertEqual(b'{"foo": "bar"}', response.body)
        self.assertEqual(response.status_int, http.OK)
コード例 #8
0
    def test_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)
        expected_header = self.get_req_id_header_name(req)
        self.assertFalse(hasattr(response.headers, expected_header))
        self.assertEqual(b'foo', response.body)
        self.assertEqual(response.status_int, http.OK)
コード例 #9
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 = b""" {"name": "\xe6\xa6\x82\xe5\xbf\xb5" } """
        expected_body = b'{"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, http.OK)
コード例 #10
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, http.OK)
        self.assertEqual(b'success', response.body)
        # ignore the body
        req.body = b'{"body": {"key": "value"}}'
        response = req.get_response(app)
        self.assertEqual(response.status_int, http.OK)
        self.assertEqual(b'success', response.body)
コード例 #11
0
ファイル: test_wsgi.py プロジェクト: takanattie/masakari
    def test_resource_headers_py2_are_utf8(self):
        resp = webob.Response(status_int=HTTPStatus.ACCEPTED)
        resp.headers['x-header1'] = 1
        resp.headers['x-header2'] = 'header2'
        resp.headers['x-header3'] = 'header3'

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

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

        self.assertEqual('1', response.headers['x-header1'])
        self.assertEqual('header2', response.headers['x-header2'])
        self.assertEqual('header3', response.headers['x-header3'])
コード例 #12
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(b'success', response.body)
        self.assertEqual(response.status_int, http.OK)
        req.body = b'{"body": {"key": "value"}}'
        response = req.get_response(app)
        self.assertEqual(b'success', response.body)
        self.assertEqual(response.status_int, http.OK)
        req.content_type = 'application/json'
        response = req.get_response(app)
        self.assertEqual(b'success', response.body)
        self.assertEqual(response.status_int, http.OK)