Beispiel #1
0
    def test_serialize(self):
        class JSONSerializer(object):
            def serialize(self, obj):
                return 'json'

        class AtomSerializer(object):
            def serialize(self, obj):
                return 'atom'

        robj = wsgi.ResponseObject({},
                                   code=202,
                                   json=JSONSerializer,
                                   atom=AtomSerializer)
        robj['X-header1'] = 'header1'
        robj['X-header2'] = 'header2'
        robj['X-header3'] = 3
        robj['X-header-unicode'] = u'header-unicode'

        for content_type, mtype in wsgi._MEDIA_TYPE_MAP.items():
            request = wsgi.Request.blank('/tests/123')
            response = robj.serialize(request, content_type)
            self.assertEqual(content_type.encode("utf-8"),
                             response.headers['Content-Type'])
            for hdr, val in six.iteritems(response.headers):
                # All headers must be utf8
                self.assertThat(val, matchers.EncodedByUTF8())
            self.assertEqual(b'header1', response.headers['X-header1'])
            self.assertEqual(b'header2', response.headers['X-header2'])
            self.assertEqual(b'3', response.headers['X-header3'])
            self.assertEqual(response.status_int, 202)
            self.assertEqual(mtype.encode("utf-8"), response.body)
Beispiel #2
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 val in six.itervalues(response.headers):
            # All headers must be utf8
            self.assertThat(val, matchers.EncodedByUTF8())
        self.assertEqual('1', response.headers['x-header1'])
        self.assertEqual('header2', response.headers['x-header2'])
        self.assertEqual('header3', response.headers['x-header3'])