Skip to content

Commit

Permalink
Pass unicode to json.loads. (#428)
Browse files Browse the repository at this point in the history
In python2 there was auto-coercion happening here but in python3 we need
to pass in unicode and requests body is bytes.
  • Loading branch information
feanil authored Sep 19, 2019
1 parent 85fff32 commit dd19b86
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion xblock/VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.4
1.2.5
2 changes: 1 addition & 1 deletion xblock/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def wrapper(self, request, suffix=''):
if request.method != "POST":
return JsonHandlerError(405, "Method must be POST").get_response(allow=["POST"])
try:
request_json = json.loads(request.body)
request_json = json.loads(request.body.decode('utf-8'))
except ValueError:
return JsonHandlerError(400, "Invalid JSON").get_response()
try:
Expand Down
12 changes: 6 additions & 6 deletions xblock/test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ def test_json_handler_basic():
test_data = {"foo": "bar", "baz": "quux"}
test_data_json = ['{"foo": "bar", "baz": "quux"}', '{"baz": "quux", "foo": "bar"}']
test_suffix = "suff"
test_request = Mock(method="POST", body=test_data_json[0])
test_request = Mock(method="POST", body=test_data_json[0].encode('utf-8'))

@XBlock.json_handler
def test_func(self, request, suffix):
Expand All @@ -843,7 +843,7 @@ def test_func(self, request, suffix):


def test_json_handler_invalid_json():
test_request = Mock(method="POST", body="{")
test_request = Mock(method="POST", body=b"{")

@XBlock.json_handler
def test_func(self, request, suffix): # pylint: disable=unused-argument
Expand Down Expand Up @@ -871,7 +871,7 @@ def test_func(self, request, suffix): # pylint: disable=unused-argument


def test_json_handler_empty_request():
test_request = Mock(method="POST", body="")
test_request = Mock(method="POST", body=b"")

@XBlock.json_handler
def test_func(self, request, suffix): # pylint: disable=unused-argument
Expand All @@ -887,7 +887,7 @@ def test_func(self, request, suffix): # pylint: disable=unused-argument
def test_json_handler_error():
test_status_code = 418
test_message = "I'm a teapot"
test_request = Mock(method="POST", body="{}")
test_request = Mock(method="POST", body=b"{}")

@XBlock.json_handler
def test_func(self, request, suffix): # pylint: disable=unused-argument
Expand All @@ -900,7 +900,7 @@ def test_func(self, request, suffix): # pylint: disable=unused-argument


def test_json_handler_return_response():
test_request = Mock(method="POST", body="{}")
test_request = Mock(method="POST", body=b"{}")

@XBlock.json_handler
def test_func(self, request, suffix): # pylint: disable=unused-argument
Expand All @@ -913,7 +913,7 @@ def test_func(self, request, suffix): # pylint: disable=unused-argument


def test_json_handler_return_unicode():
test_request = Mock(method="POST", body='["foo", "bar"]')
test_request = Mock(method="POST", body=b'["foo", "bar"]')

@XBlock.json_handler
def test_func(self, request, suffix): # pylint: disable=unused-argument
Expand Down

0 comments on commit dd19b86

Please sign in to comment.