From 529526c68acb3acdf732d962b7cc8195081cbf56 Mon Sep 17 00:00:00 2001 From: Prashanth Pai Date: Thu, 25 Feb 2016 14:17:09 +0530 Subject: Use correct content type on JSON responses Currently, in cases where swauth returns a JSON document as its body, it does not specify a content type, and swob defaults it to text/html. This change uses a standard content type of 'application/json' in each of these instances, and adjusts the tests accordingly. This is ported from following commit in swauth repo: 556aa156979741292bde78425f413f9dee639b4f Change-Id: Ib61370ba10b5e0364c2aed6321388715a6710355 Signed-off-by: Prashanth Pai Reviewed-on: http://review.gluster.org/13521 Reviewed-by: Thiago da Silva Tested-by: Thiago da Silva --- .../common/middleware/gswauth/swauth/middleware.py | 19 ++++++++++++------ .../middleware/gswauth/swauth/test_middleware.py | 23 ++++++++++++++++++++++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/gluster/swift/common/middleware/gswauth/swauth/middleware.py b/gluster/swift/common/middleware/gswauth/swauth/middleware.py index cbcd95d..ccd85ca 100644 --- a/gluster/swift/common/middleware/gswauth/swauth/middleware.py +++ b/gluster/swift/common/middleware/gswauth/swauth/middleware.py @@ -43,6 +43,8 @@ import swift.common.wsgi from gluster.swift.common.middleware.gswauth.swauth import authtypes +CONTENT_TYPE_JSON = 'application/json' + class Swauth(object): """ @@ -589,7 +591,8 @@ class Swauth(object): if container['name'][0] != '.': listing.append({'name': container['name']}) marker = sublisting[-1]['name'].encode('utf-8') - return Response(body=json.dumps({'accounts': listing})) + return Response(body=json.dumps({'accounts': listing}), + content_type=CONTENT_TYPE_JSON) def handle_get_account(self, req): """ @@ -646,9 +649,10 @@ class Swauth(object): if obj['name'][0] != '.': listing.append({'name': obj['name']}) marker = sublisting[-1]['name'].encode('utf-8') - return Response(body=json.dumps( - {'account_id': account_id, - 'services': services, 'users': listing})) + return Response(content_type=CONTENT_TYPE_JSON, + body=json.dumps({'account_id': account_id, + 'services': services, + 'users': listing})) def handle_set_services(self, req): """ @@ -718,7 +722,8 @@ class Swauth(object): if resp.status_int // 100 != 2: raise Exception('Could not save .services object: %s %s' % (path, resp.status)) - return Response(request=req, body=services) + return Response(request=req, body=services, + content_type=CONTENT_TYPE_JSON) def handle_put_account(self, req): """ @@ -962,7 +967,7 @@ class Swauth(object): ('.reseller_admin' in display_groups and not self.is_super_admin(req)): return HTTPForbidden(request=req) - return Response(body=body) + return Response(body=body, content_type=CONTENT_TYPE_JSON) def handle_put_user(self, req): """ @@ -1208,6 +1213,7 @@ class Swauth(object): token = self.get_itoken(req.environ) url = '%s/%s' % (self.dsc_url, self.auth_account) return Response( + content_type=CONTENT_TYPE_JSON, request=req, body=json.dumps( {'storage': {'default': 'local', @@ -1315,6 +1321,7 @@ class Swauth(object): detail = json.loads(resp.body) url = detail['storage'][detail['storage']['default']] return Response( + content_type=CONTENT_TYPE_JSON, request=req, body=resp.body, headers={'x-auth-token': token, 'x-storage-token': token, 'x-auth-token-expires': str(int(expires - time())), diff --git a/test/unit/common/middleware/gswauth/swauth/test_middleware.py b/test/unit/common/middleware/gswauth/swauth/test_middleware.py index e0d4ce8..668f266 100644 --- a/test/unit/common/middleware/gswauth/swauth/test_middleware.py +++ b/test/unit/common/middleware/gswauth/swauth/test_middleware.py @@ -26,6 +26,7 @@ from swift.common.swob import Request, Response from gluster.swift.common.middleware.gswauth.swauth import middleware as auth from gluster.swift.common.middleware.gswauth.swauth.authtypes import MAX_TOKEN_LENGTH +from gluster.swift.common.middleware.gswauth.swauth.middleware import CONTENT_TYPE_JSON DEFAULT_TOKEN_LIFE = 86400 @@ -864,6 +865,7 @@ class TestAuth(unittest.TestCase): headers={'X-Auth-User': 'act:usr', 'X-Auth-Key': 'key'}).get_response(self.test_auth) self.assertEquals(resp.status_int, 200) + self.assertEqual(resp.content_type, CONTENT_TYPE_JSON) self.assert_(resp.headers.get( 'x-auth-token', '').startswith('AUTH_tk'), resp.headers.get('x-auth-token')) @@ -901,6 +903,7 @@ class TestAuth(unittest.TestCase): 'X-Auth-Key': 'key', 'X-Auth-Token-Lifetime': 10}).get_response(self.test_auth) self.assertEquals(resp.status_int, 200) + self.assertEqual(resp.content_type, CONTENT_TYPE_JSON) left = int(resp.headers['x-auth-token-expires']) self.assertTrue(left > 0, '%d > 0' % left) self.assertTrue(left <= 10, '%d <= 10' % left) @@ -942,6 +945,7 @@ class TestAuth(unittest.TestCase): 'X-Auth-Token-Lifetime': MAX_TOKEN_LIFE * 10}) resp = req.get_response(self.test_auth) self.assertEquals(resp.status_int, 200) + self.assertEqual(resp.content_type, CONTENT_TYPE_JSON) left = int(resp.headers['x-auth-token-expires']) self.assertTrue(left > DEFAULT_TOKEN_LIFE, '%d > %d' % (left, DEFAULT_TOKEN_LIFE)) @@ -982,6 +986,7 @@ class TestAuth(unittest.TestCase): headers={'X-Storage-User': 'usr', 'X-Storage-Pass': 'key'}).get_response(self.test_auth) self.assertEquals(resp.status_int, 200) + self.assertEqual(resp.content_type, CONTENT_TYPE_JSON) self.assert_(resp.headers.get( 'x-auth-token', '').startswith('AUTH_tk'), resp.headers.get('x-auth-token')) @@ -1018,6 +1023,7 @@ class TestAuth(unittest.TestCase): headers={'X-Storage-User': 'act:usr', 'X-Storage-Pass': 'key'}).get_response(self.test_auth) self.assertEquals(resp.status_int, 200) + self.assertEqual(resp.content_type, CONTENT_TYPE_JSON) self.assert_( resp.headers.get( 'x-auth-token', @@ -1055,6 +1061,7 @@ class TestAuth(unittest.TestCase): headers={'X-Auth-User': 'act:usr', 'X-Auth-Key': 'key'}).get_response(self.test_auth) self.assertEquals(resp.status_int, 200) + self.assertEqual(resp.content_type, CONTENT_TYPE_JSON) self.assert_(resp.headers.get( 'x-auth-token', '').startswith('AUTH_tk'), resp.headers.get('x-auth-token')) @@ -1090,6 +1097,7 @@ class TestAuth(unittest.TestCase): headers={'X-Auth-User': 'act:usr', 'X-Auth-Key': 'key'}).get_response(self.test_auth) self.assertEquals(resp.status_int, 200) + self.assertEqual(resp.content_type, CONTENT_TYPE_JSON) self.assertEquals( resp.headers.get('x-auth-token'), 'AUTH_tktest') @@ -1130,6 +1138,7 @@ class TestAuth(unittest.TestCase): 'X-Auth-New-Token': 'true'}).get_response( self.test_auth) self.assertEquals(resp.status_int, 200) + self.assertEqual(resp.content_type, CONTENT_TYPE_JSON) self.assertNotEquals( resp.headers.get('x-auth-token'), 'AUTH_tktest') self.assertEquals(resp.headers.get('x-auth-token'), @@ -1173,6 +1182,7 @@ class TestAuth(unittest.TestCase): headers={'X-Auth-User': 'act:usr', 'X-Auth-Key': 'key'}).get_response(self.test_auth) self.assertEquals(resp.status_int, 200) + self.assertEqual(resp.content_type, CONTENT_TYPE_JSON) self.assertNotEquals( resp.headers.get('x-auth-token'), 'AUTH_tktest') @@ -1217,6 +1227,7 @@ class TestAuth(unittest.TestCase): headers={'X-Auth-User': 'act:usr', 'X-Auth-Key': 'key'}).get_response(self.test_auth) self.assertEquals(resp.status_int, 200) + self.assertEqual(resp.content_type, CONTENT_TYPE_JSON) self.assertNotEquals( resp.headers.get('x-auth-token'), 'AUTH_tktest') @@ -1385,6 +1396,7 @@ class TestAuth(unittest.TestCase): 'X-Auth-Admin-Key': 'supertest'} ).get_response(self.test_auth) self.assertEquals(resp.status_int, 200) + self.assertEqual(resp.content_type, CONTENT_TYPE_JSON) self.assertEquals(json.loads(resp.body), {"accounts": [{"name": "act"}]}) self.assertEquals(self.test_auth.app.calls, 2) @@ -1410,6 +1422,7 @@ class TestAuth(unittest.TestCase): 'X-Auth-Admin-Key': 'key'} ).get_response(self.test_auth) self.assertEquals(resp.status_int, 200) + self.assertEqual(resp.content_type, CONTENT_TYPE_JSON) self.assertEquals(json.loads(resp.body), {"accounts": [{"name": "act"}]}) self.assertEquals(self.test_auth.app.calls, 3) @@ -1519,6 +1532,7 @@ class TestAuth(unittest.TestCase): 'X-Auth-Admin-Key': 'supertest'} ).get_response(self.test_auth) self.assertEquals(resp.status_int, 200) + self.assertEqual(resp.content_type, CONTENT_TYPE_JSON) self.assertEquals( json.loads(resp.body), {'account_id': 'AUTH_cfa', @@ -1563,6 +1577,7 @@ class TestAuth(unittest.TestCase): 'X-Auth-Admin-Key': 'key'} ).get_response(self.test_auth) self.assertEquals(resp.status_int, 200) + self.assertEqual(resp.content_type, CONTENT_TYPE_JSON) self.assertEquals( json.loads(resp.body), {'account_id': 'AUTH_cfa', @@ -1740,6 +1755,7 @@ class TestAuth(unittest.TestCase): {'new_endpoint': 'new_value'}}) ).get_response(self.test_auth) self.assertEquals(resp.status_int, 200) + self.assertEqual(resp.content_type, CONTENT_TYPE_JSON) self.assertEquals( json.loads(resp.body), {'storage': {'default': 'local', @@ -1767,6 +1783,7 @@ class TestAuth(unittest.TestCase): {'storage': {'new_endpoint': 'new_value'}}) ).get_response(self.test_auth) self.assertEquals(resp.status_int, 200) + self.assertEqual(resp.content_type, CONTENT_TYPE_JSON) self.assertEquals( json.loads(resp.body), {'storage': {'default': 'local', @@ -1794,6 +1811,7 @@ class TestAuth(unittest.TestCase): {'storage': {'local': 'new_value'}}) ).get_response(self.test_auth) self.assertEquals(resp.status_int, 200) + self.assertEqual(resp.content_type, CONTENT_TYPE_JSON) self.assertEquals(json.loads(resp.body), {'storage': {'default': 'local', 'local': 'new_value'}}) @@ -2853,6 +2871,7 @@ class TestAuth(unittest.TestCase): 'X-Auth-Admin-Key': 'supertest'} ).get_response(self.test_auth) self.assertEquals(resp.status_int, 200) + self.assertEqual(resp.content_type, CONTENT_TYPE_JSON) self.assertEquals(resp.body, json.dumps( {"groups": [{"name": "act:usr"}, {"name": "act"}, {"name": ".admin"}], @@ -2912,6 +2931,7 @@ class TestAuth(unittest.TestCase): 'X-Auth-Admin-Key': 'supertest'} ).get_response(self.test_auth) self.assertEquals(resp.status_int, 200) + self.assertEqual(resp.content_type, CONTENT_TYPE_JSON) self.assertEquals(resp.body, json.dumps( {"groups": [{"name": ".admin"}, {"name": "act"}, {"name": "act:tester"}, {"name": "act:tester3"}]})) @@ -3024,6 +3044,7 @@ class TestAuth(unittest.TestCase): 'X-Auth-Admin-Key': 'key'} ).get_response(self.test_auth) self.assertEquals(resp.status_int, 200) + self.assertEqual(resp.content_type, CONTENT_TYPE_JSON) self.assertEquals(resp.body, json.dumps( {"groups": [{"name": "act:usr"}, {"name": "act"}], "auth": "plaintext:key"})) @@ -3115,6 +3136,7 @@ class TestAuth(unittest.TestCase): 'X-Auth-Admin-Key': 'supertest'} ).get_response(self.test_auth) self.assertEquals(resp.status_int, 200) + self.assertEqual(resp.content_type, CONTENT_TYPE_JSON) self.assertEquals(resp.body, json.dumps( {"groups": [{"name": "act:usr"}, {"name": "act"}, {"name": ".reseller_admin"}], @@ -4467,6 +4489,7 @@ class TestAuth(unittest.TestCase): headers={'X-Auth-User': sent_user, 'X-Auth-Key': sent_key}).get_response(self.test_auth) self.assertEquals(resp.status_int, 200) + self.assertEqual(resp.content_type, CONTENT_TYPE_JSON) self.assert_( resp.headers.get('x-auth-token', '').startswith('AUTH_tk'), -- cgit