Skip to content

Commit fbd8895

Browse files
committed
Fix REST tests for Python 3.
FieldStorage requires a file storing bytes not strings. With dispatch returning bytes (as required to work end-to-end), tests of the dispatch method that use its return value must handle decoding it.
1 parent 54bbcce commit fbd8895

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

test/rest_common.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
from roundup.rest import RestfulInstance, calculate_etag
99
from roundup.backends import list_backends
1010
from roundup.cgi import client
11+
from roundup.anypy.strings import b2s, s2b
1112
import random
1213

1314
from .db_test_base import setupTracker
1415

1516
from .mocknull import MockNull
1617

17-
from io import StringIO
18+
from io import BytesIO
1819
import json
1920

2021
NEEDS_INSTANCE = 1
@@ -409,7 +410,7 @@ def testDispatch(self):
409410
# simulate: /rest/data/user/<id>/realname
410411
# use etag in header
411412
etag = calculate_etag(self.db.user.getnode(self.joeid))
412-
body=u'{ "data": "Joe Doe 1" }'
413+
body=b'{ "data": "Joe Doe 1" }'
413414
env = { "CONTENT_TYPE": "application/json",
414415
"CONTENT_LENGTH": len(body),
415416
"REQUEST_METHOD": "PUT"
@@ -422,7 +423,7 @@ def testDispatch(self):
422423
# we need to generate a FieldStorage the looks like
423424
# FieldStorage(None, None, 'string') rather than
424425
# FieldStorage(None, None, [])
425-
body_file=StringIO(body) # FieldStorage needs a file
426+
body_file=BytesIO(body) # FieldStorage needs a file
426427
form = cgi.FieldStorage(body_file,
427428
headers=headers,
428429
environ=env)
@@ -442,7 +443,7 @@ def testDispatch(self):
442443
# simulate: /rest/data/user/<id>/realname
443444
# use etag in payload
444445
etag = calculate_etag(self.db.user.getnode(self.joeid))
445-
body=u'{ "@etag": "%s", "data": "Joe Doe 2" }'%etag
446+
body=s2b('{ "@etag": "%s", "data": "Joe Doe 2" }'%etag)
446447
env = { "CONTENT_TYPE": "application/json",
447448
"CONTENT_LENGTH": len(body),
448449
"REQUEST_METHOD": "PUT"
@@ -451,7 +452,7 @@ def testDispatch(self):
451452
"content-type": env['CONTENT_TYPE']
452453
}
453454
self.headers=headers
454-
body_file=StringIO(body) # FieldStorage needs a file
455+
body_file=BytesIO(body) # FieldStorage needs a file
455456
form = cgi.FieldStorage(body_file,
456457
headers=headers,
457458
environ=env)
@@ -492,7 +493,7 @@ def testDispatch(self):
492493
"/rest/data/user/%s/realname"%self.joeid,
493494
self.empty_form)
494495
self.assertEqual(self.dummy_client.response_code, 200)
495-
json_dict = json.loads(results)
496+
json_dict = json.loads(b2s(results))
496497

497498
self.assertEqual(json_dict['data']['data'], 'Joe Doe')
498499
self.assertEqual(json_dict['data']['link'],
@@ -510,7 +511,7 @@ def testDispatch(self):
510511
self.assertEqual(self.dummy_client.response_code, 200)
511512

512513
etag = calculate_etag(self.db.user.getnode(self.joeid))
513-
body=u'{ "address": "[email protected]", "@etag": "%s"}'%etag
514+
body=s2b('{ "address": "[email protected]", "@etag": "%s"}'%etag)
514515
env = { "CONTENT_TYPE": "application/json",
515516
"CONTENT_LENGTH": len(body),
516517
"REQUEST_METHOD": "PATCH"
@@ -519,7 +520,7 @@ def testDispatch(self):
519520
"content-type": env['CONTENT_TYPE']
520521
}
521522
self.headers=headers
522-
body_file=StringIO(body) # FieldStorage needs a file
523+
body_file=BytesIO(body) # FieldStorage needs a file
523524
form = cgi.FieldStorage(body_file,
524525
headers=headers,
525526
environ=env)
@@ -536,11 +537,11 @@ def testDispatch(self):
536537

537538
# and set it back
538539
etag = calculate_etag(self.db.user.getnode(self.joeid))
539-
body=u'{ "address": "%s", "@etag": "%s"}'%(
540+
body=s2b('{ "address": "%s", "@etag": "%s"}'%(
540541
stored_results['data']['attributes']['address'],
541-
etag)
542+
etag))
542543
# reuse env and headers from prior test.
543-
body_file=StringIO(body) # FieldStorage needs a file
544+
body_file=BytesIO(body) # FieldStorage needs a file
544545
form = cgi.FieldStorage(body_file,
545546
headers=headers,
546547
environ=env)
@@ -557,7 +558,7 @@ def testDispatch(self):
557558
del(self.headers)
558559

559560
# POST to create new issue
560-
body=u'{ "title": "foo bar", "priority": "critical" }'
561+
body=b'{ "title": "foo bar", "priority": "critical" }'
561562

562563
env = { "CONTENT_TYPE": "application/json",
563564
"CONTENT_LENGTH": len(body),
@@ -567,7 +568,7 @@ def testDispatch(self):
567568
"content-type": env['CONTENT_TYPE']
568569
}
569570
self.headers=headers
570-
body_file=StringIO(body) # FieldStorage needs a file
571+
body_file=BytesIO(body) # FieldStorage needs a file
571572
form = cgi.FieldStorage(body_file,
572573
headers=headers,
573574
environ=env)
@@ -577,7 +578,7 @@ def testDispatch(self):
577578
form)
578579

579580
self.assertEqual(self.server.client.response_code, 201)
580-
json_dict = json.loads(results)
581+
json_dict = json.loads(b2s(results))
581582
issue_id=json_dict['data']['id']
582583
results = self.server.get_element('issue',
583584
str(issue_id), # must be a string not unicode

0 commit comments

Comments
 (0)