def test_serialize(self): class JSONSerializer(object): def serialize(self, obj): return six.b('json') class XMLSerializer(object): def serialize(self, obj): return six.b('xml') class AtomSerializer(object): def serialize(self, obj): return six.b('atom') robj = wsgi.ResponseObject({}, code=202, json=JSONSerializer, xml=XMLSerializer, atom=AtomSerializer) robj['X-header1'] = 'header1' robj['X-header2'] = 'header2' 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, response.headers['Content-Type']) self.assertEqual('header1', response.headers['X-header1']) self.assertEqual('header2', response.headers['X-header2']) self.assertEqual(202, response.status_int) self.assertEqual(six.b(mtype), response.body)
def test_used_limits(self): fake_req = FakeRequest(fakes.FakeRequestContext('fake', 'fake')) obj = {"limits": {"rate": [], "absolute": {}}} res = wsgi.ResponseObject(obj) quota_map = { 'totalSharesUsed': 'shares', 'totalShareSnapshotsUsed': 'snapshots', 'totalShareNetworksUsed': 'share_networks', 'totalShareGigabytesUsed': 'gigabytes', } limits = {} for display_name, q in six.iteritems(quota_map): limits[q] = {'limit': 2, 'in_use': 1, } def stub_get_project_quotas(*args, **kwargs): return limits with mock.patch.object(quota.QUOTAS, 'get_project_quotas', mock.Mock(side_effect=stub_get_project_quotas)): self.controller.index(fake_req, res) abs_limits = res.obj['limits']['absolute'] for used_limit, value in six.iteritems(abs_limits): self.assertEqual(value, limits[quota_map[used_limit]]['in_use'])
def test_get_serializer_defaults(self): robj = wsgi.ResponseObject({}) default_serializers = dict(json='json', xml='xml', atom='atom') for content_type, mtype in wsgi._MEDIA_TYPE_MAP.items(): self.assertRaises(exception.InvalidContentType, robj.get_serializer, content_type) _mtype, serializer = robj.get_serializer(content_type, default_serializers) self.assertEqual(mtype, serializer)
def test_get_serializer(self): robj = wsgi.ResponseObject({}, json='json', xml='xml', atom='atom') for content_type, mtype in wsgi._MEDIA_TYPE_MAP.items(): _mtype, serializer = robj.get_serializer(content_type) self.assertEqual(mtype, serializer)
def test_bind_serializers(self): robj = wsgi.ResponseObject({}, json='foo') robj._bind_method_serializers(dict(xml='bar', json='baz')) self.assertEqual(dict(xml='bar', json='foo'), robj.serializers)
def test_default_serializers(self): robj = wsgi.ResponseObject({}) self.assertEqual({}, robj.serializers)
def test_header_isolation(self): robj = wsgi.ResponseObject({}) robj['Header'] = 'foo' hdrs = robj.headers hdrs['hEADER'] = 'bar' self.assertEqual('foo', robj['hEADER'])
def test_del_header(self): robj = wsgi.ResponseObject({}) robj['Header'] = 'foo' del robj['hEADER'] self.assertFalse('header' in robj.headers)
def test_get_header(self): robj = wsgi.ResponseObject({}) robj['Header'] = 'foo' self.assertEqual('foo', robj['hEADER'])
def test_set_header(self): robj = wsgi.ResponseObject({}) robj['Header'] = 'foo' self.assertEqual({'header': 'foo'}, robj.headers)
def test_override_modified_code(self): robj = wsgi.ResponseObject({}, code=404) robj._default_code = 202 self.assertEqual(404, robj.code)
def test_modified_code(self): robj = wsgi.ResponseObject({}) robj._default_code = 202 self.assertEqual(202, robj.code)
def test_default_code(self): robj = wsgi.ResponseObject({}) self.assertEqual(200, robj.code)
def test_override_default_code(self): robj = wsgi.ResponseObject({}, code=404) self.assertEqual(robj.code, 404)