Skip to content

Commit 47f6c88

Browse files
committed
Move @Apiver version extraction code after the input is parsed for
json. Otherwise json input causes issues. test 400 return code if ;version=1.1. test for version param in accept header ;version=1 test version in type/vnd.subtype (vnd.json.test-v1) and @Apiver=1 query param.
1 parent 5f2fb5a commit 47f6c88

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

roundup/rest.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,24 +1476,6 @@ def dispatch(self, method, uri, input):
14761476
part[1]['version']))
14771477
output = self.error_obj(400, msg)
14781478

1479-
# check for @apiver in query string
1480-
try:
1481-
if not self.api_version:
1482-
self.api_version = int(input['@apiver'].value)
1483-
except KeyError:
1484-
self.api_version = None
1485-
except ValueError:
1486-
msg=( "Unrecognized version: %s. "
1487-
"See /rest without specifying version "
1488-
"for supported versions."%(
1489-
input['@apiver'].value))
1490-
output = self.error_obj(400, msg)
1491-
1492-
# FIXME: do we need to raise an error if client did not specify
1493-
# version? This may be a good thing to require. Note that:
1494-
# Accept: application/json; version=1 may not be legal but....
1495-
1496-
14971479
# get the request format for response
14981480
# priority : extension from uri (/rest/data/issue.json),
14991481
# header (Accept: application/json, application/xml)
@@ -1550,6 +1532,23 @@ def dispatch(self, method, uri, input):
15501532
except KeyError:
15511533
pretty_output = True
15521534

1535+
# check for @apiver in query string
1536+
try:
1537+
if not self.api_version:
1538+
self.api_version = int(input['@apiver'].value)
1539+
except KeyError:
1540+
self.api_version = None
1541+
except ValueError:
1542+
msg=( "Unrecognized version: %s. "
1543+
"See /rest without specifying version "
1544+
"for supported versions."%(
1545+
input['@apiver'].value))
1546+
output = self.error_obj(400, msg)
1547+
1548+
# FIXME: do we need to raise an error if client did not specify
1549+
# version? This may be a good thing to require. Note that:
1550+
# Accept: application/json; version=1 may not be legal but....
1551+
15531552
# Call the appropriate method
15541553
try:
15551554
# If output was defined by a prior error

test/rest_common.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ def testDispatch(self):
715715
"CONTENT_LENGTH": len(body),
716716
"REQUEST_METHOD": "PUT"
717717
}
718-
headers={"accept": "application/json",
718+
headers={"accept": "application/json; version=1",
719719
"content-type": env['CONTENT_TYPE'],
720720
"content-length": env['CONTENT_LENGTH'],
721721
"if-match": etag
@@ -738,6 +738,20 @@ def testDispatch(self):
738738
self.assertEqual(self.dummy_client.response_code, 200)
739739
self.assertEqual(results['data']['attributes']['realname'],
740740
'Joe Doe 1')
741+
742+
743+
# substitute the version with an unacceptable version
744+
# and verify it returns 400 code.
745+
self.headers["accept"] = "application/json; version=1.1"
746+
body_file=BytesIO(body) # FieldStorage needs a file
747+
form = client.BinaryFieldStorage(body_file,
748+
headers=headers,
749+
environ=env)
750+
self.server.client.request.headers.get=self.get_header
751+
results = self.server.dispatch('PUT',
752+
"/rest/data/user/%s/realname"%self.joeid,
753+
form)
754+
self.assertEqual(self.server.client.response_code, 400)
741755
del(self.headers)
742756

743757
# TEST #2
@@ -785,11 +799,12 @@ def testDispatch(self):
785799
# the results from the db.
786800
etag = calculate_etag(self.db.user.getnode(self.joeid))
787801
headers={"if-match": etag,
788-
"accept": "application/json",
802+
"accept": "application/vnd.json.test-v1+json",
789803
}
790804
form = cgi.FieldStorage()
791805
form.list = [
792806
cgi.MiniFieldStorage('data', 'Joe Doe'),
807+
cgi.MiniFieldStorage('@apiver', '1'),
793808
]
794809
self.headers = headers
795810
self.server.client.request.headers.get = self.get_header

0 commit comments

Comments
 (0)