diff --git a/common/djangoapps/terrain/stubs/http.py b/common/djangoapps/terrain/stubs/http.py index 3b041b0bb5..24ed0e7194 100644 --- a/common/djangoapps/terrain/stubs/http.py +++ b/common/djangoapps/terrain/stubs/http.py @@ -16,7 +16,6 @@ import six.moves.urllib.request # pylint: disable=import-error from lazy import lazy from six.moves.BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer # pylint: disable=import-error from six.moves.socketserver import ThreadingMixIn # pylint: disable=import-error - LOGGER = getLogger(__name__) @@ -105,7 +104,7 @@ class StubHttpRequestHandler(BaseHTTPRequestHandler, object): Retrieve the request POST parameters from the client as a dictionary. If no POST parameters can be interpreted, return an empty dict. """ - contents = self.request_content + contents = self.request_content.decode() # The POST dict will contain a list of values for each key. # None of our parameters are lists, however, so we map [val] --> val @@ -159,13 +158,6 @@ class StubHttpRequestHandler(BaseHTTPRequestHandler, object): if len(self.post_dict) > 0: for key, value in six.iteritems(self.post_dict): - # Decode the params as UTF-8 - try: - key = six.text_type(key) - value = six.text_type(value) - except UnicodeDecodeError: - self.log_message("Could not decode request params as UTF-8") - self.log_message(u"Set config '{0}' to '{1}'".format(key, value)) try: @@ -209,6 +201,8 @@ class StubHttpRequestHandler(BaseHTTPRequestHandler, object): self.end_headers() if content is not None: + if not six.PY2 and isinstance(content, six.text_type): + content = content.encode('utf-8') self.wfile.write(content) def send_json_response(self, content): diff --git a/common/djangoapps/terrain/stubs/lti.py b/common/djangoapps/terrain/stubs/lti.py index fd4f2cfed2..ff0f430c34 100644 --- a/common/djangoapps/terrain/stubs/lti.py +++ b/common/djangoapps/terrain/stubs/lti.py @@ -266,7 +266,7 @@ class StubLtiHandler(StubHttpRequestHandler): # Calculate and encode body hash. See http://oauth.googlecode.com/svn/spec/ext/body_hash/1.0/oauth-bodyhash.html sha1 = hashlib.sha1() - sha1.update(body) + sha1.update(body.encode('utf-8')) oauth_body_hash = six.text_type(base64.b64encode(sha1.digest())) mock_request = mock.Mock( uri=six.text_type(six.moves.urllib.parse.unquote(url)), diff --git a/common/djangoapps/terrain/stubs/tests/test_lti_stub.py b/common/djangoapps/terrain/stubs/tests/test_lti_stub.py index 43e3dc0044..90c2fa2c55 100644 --- a/common/djangoapps/terrain/stubs/tests/test_lti_stub.py +++ b/common/djangoapps/terrain/stubs/tests/test_lti_stub.py @@ -49,7 +49,7 @@ class StubLtiServiceTest(unittest.TestCase): """ self.launch_uri = self.uri + 'wrong_lti_endpoint' response = requests.post(self.launch_uri, data=self.payload) - self.assertIn('Invalid request URL', response.content) + self.assertIn(b'Invalid request URL', response.content) def test_wrong_signature(self): """ @@ -57,7 +57,7 @@ class StubLtiServiceTest(unittest.TestCase): path and responses with incorrect signature. """ response = requests.post(self.launch_uri, data=self.payload) - self.assertIn('Wrong LTI signature', response.content) + self.assertIn(b'Wrong LTI signature', response.content) @patch('terrain.stubs.lti.signature.verify_hmac_sha1', return_value=True) def test_success_response_launch_lti(self, check_oauth): @@ -65,34 +65,34 @@ class StubLtiServiceTest(unittest.TestCase): Success lti launch. """ response = requests.post(self.launch_uri, data=self.payload) - self.assertIn('This is LTI tool. Success.', response.content) + self.assertIn(b'This is LTI tool. Success.', response.content) @patch('terrain.stubs.lti.signature.verify_hmac_sha1', return_value=True) def test_send_graded_result(self, verify_hmac): # pylint: disable=unused-argument response = requests.post(self.launch_uri, data=self.payload) - self.assertIn('This is LTI tool. Success.', response.content) + self.assertIn(b'This is LTI tool. Success.', response.content) grade_uri = self.uri + 'grade' with patch('terrain.stubs.lti.requests.post') as mocked_post: mocked_post.return_value = Mock(content='Test response', status_code=200) - response = six.moves.urllib.request.urlopen(grade_uri, data='') - self.assertIn('Test response', response.read()) + response = six.moves.urllib.request.urlopen(grade_uri, data=b'') + self.assertIn(b'Test response', response.read()) @patch('terrain.stubs.lti.signature.verify_hmac_sha1', return_value=True) def test_lti20_outcomes_put(self, verify_hmac): # pylint: disable=unused-argument response = requests.post(self.launch_uri, data=self.payload) - self.assertIn('This is LTI tool. Success.', response.content) + self.assertIn(b'This is LTI tool. Success.', response.content) grade_uri = self.uri + 'lti2_outcome' with patch('terrain.stubs.lti.requests.put') as mocked_put: mocked_put.return_value = Mock(status_code=200) - response = six.moves.urllib.request.urlopen(grade_uri, data='') - self.assertIn('LTI consumer (edX) responded with HTTP 200', response.read()) + response = six.moves.urllib.request.urlopen(grade_uri, data=b'') + self.assertIn(b'LTI consumer (edX) responded with HTTP 200', response.read()) @patch('terrain.stubs.lti.signature.verify_hmac_sha1', return_value=True) def test_lti20_outcomes_put_like_delete(self, verify_hmac): # pylint: disable=unused-argument response = requests.post(self.launch_uri, data=self.payload) - self.assertIn('This is LTI tool. Success.', response.content) + self.assertIn(b'This is LTI tool. Success.', response.content) grade_uri = self.uri + 'lti2_delete' with patch('terrain.stubs.lti.requests.put') as mocked_put: mocked_put.return_value = Mock(status_code=200) - response = six.moves.urllib.request.urlopen(grade_uri, data='') - self.assertIn('LTI consumer (edX) responded with HTTP 200', response.read()) + response = six.moves.urllib.request.urlopen(grade_uri, data=b'') + self.assertIn(b'LTI consumer (edX) responded with HTTP 200', response.read())