comparison test/test_cgi.py @ 7154:f614176903d0

fix test; string for json object has extra space under python2. So compensate by comparing json parsed into objects and compensate with a different content-length between py2 and py3..
author John Rouillard <rouilj@ieee.org>
date Tue, 21 Feb 2023 23:06:15 -0500
parents 1181157d7cec
children 89a59e46b3af
comparison
equal deleted inserted replaced
7153:1181157d7cec 7154:f614176903d0
1346 self.assertEqual(json.loads(b2s(out[0])),json.loads(expected)) 1346 self.assertEqual(json.loads(b2s(out[0])),json.loads(expected))
1347 del(out[0]) 1347 del(out[0])
1348 1348
1349 1349
1350 def testRestOptionsBadAttribute(self): 1350 def testRestOptionsBadAttribute(self):
1351 import json
1351 out = [] 1352 out = []
1352 def wh(s): 1353 def wh(s):
1353 out.append(s) 1354 out.append(s)
1354 1355
1355 # rest has no form content 1356 # rest has no form content
1376 cl.request.headers = MockNull(**h) 1377 cl.request.headers = MockNull(**h)
1377 1378
1378 cl.write = wh # capture output 1379 cl.write = wh # capture output
1379 cl.handle_rest() 1380 cl.handle_rest()
1380 1381
1382 _py3 = sys.version_info[0] > 2
1383
1381 expected_headers = { 1384 expected_headers = {
1382 'Access-Control-Allow-Credentials': 'true', 1385 'Access-Control-Allow-Credentials': 'true',
1383 'Access-Control-Allow-Headers': 'Content-Type, Authorization, ' 1386 'Access-Control-Allow-Headers': 'Content-Type, Authorization, '
1384 'X-Requested-With, X-HTTP-Method-Override', 1387 'X-Requested-With, X-HTTP-Method-Override',
1385 'Access-Control-Allow-Methods': 'HEAD, OPTIONS, GET, POST, PUT, DELETE, PATCH', 1388 'Access-Control-Allow-Methods': 'HEAD, OPTIONS, GET, POST, PUT, DELETE, PATCH',
1386 'Access-Control-Allow-Origin': 'http://whoami.com', 1389 'Access-Control-Allow-Origin': 'http://whoami.com',
1387 'Access-Control-Max-Age': '86400', 1390 'Access-Control-Max-Age': '86400',
1388 'Allow': 'OPTIONS, GET, POST, PUT, DELETE, PATCH', 1391 'Allow': 'OPTIONS, GET, POST, PUT, DELETE, PATCH',
1389 'Content-Length': '104', 1392 # string representation under python2 has an extra space.
1393 'Content-Length': '104' if _py3 else '105',
1390 'Content-Type': 'application/json', 1394 'Content-Type': 'application/json',
1391 'Vary': 'Origin' 1395 'Vary': 'Origin'
1392 } 1396 }
1393 1397
1394 expected_body = b'{\n "error": {\n "status": 404,\n "msg": "Attribute zot not valid for Class user"\n }\n}\n' 1398 expected_body = b'{\n "error": {\n "status": 404,\n "msg": "Attribute zot not valid for Class user"\n }\n}\n'
1395 1399
1396 self.assertEqual(cl.response_code, 404) 1400 self.assertEqual(cl.response_code, 404)
1397 self.assertEqual(out[0], expected_body) 1401 # json payload string representation differs. Compare as objects.
1402 self.assertEqual(json.loads(b2s(out[0])), json.loads(expected_body))
1398 self.assertEqual(cl.additional_headers, expected_headers) 1403 self.assertEqual(cl.additional_headers, expected_headers)
1399 1404
1400 del(out[0]) 1405 del(out[0])
1401 1406
1402 1407

Roundup Issue Tracker: http://roundup-tracker.org/