def test_plain(self): mock = Mock() mock.headers = {HTTP_CONTENT_TYPE: 'text/plain'} mock.content = 'sdfsdf' mock.get = mock.headers.get factory = ResponseModelFactory(mock) body, content = factory.body() self.assertFalse(body) self.assertEqual(content, mock.content)
def test_plain(self): mock = Mock() mock._headers = {HTTP_CONTENT_TYPE: 'text/plain'} mock.content = 'sdfsdf' mock.get = mock._headers.get factory = ResponseModelFactory(mock) body, content = factory.body() self.assertFalse(body) self.assertEqual(content, mock.content)
def test_utf_plain(self): mock = Mock() mock._headers = {HTTP_CONTENT_TYPE: 'text/plain; charset=UTF-8'} mock.content = u'语' mock.get = mock._headers.get factory = ResponseModelFactory(mock) body, content = factory.body() self.assertFalse(body) self.assertEqual(content, mock.content)
def test_invalid_encoding_json(self): mock = Mock() mock._headers = {HTTP_CONTENT_TYPE: 'application/json; charset=asdas-8'} d = {'x': u'语'} mock.content = json.dumps(d).encode('UTF-8') mock.get = mock._headers.get factory = ResponseModelFactory(mock) body, content = factory.body() self.assertDictEqual(json.loads(body, encoding='UTF-8'), d) self.assertEqual(mock.content.decode('UTF-8'), content)
def test_utf_json_not_encoded(self): mock = Mock() mock._headers = {HTTP_CONTENT_TYPE: 'application/json; charset=UTF-8'} d = {'x': u'语'} mock.content = json.dumps(d) mock.get = mock._headers.get factory = ResponseModelFactory(mock) body, content = factory.body() self.assertDictEqual(json.loads(body), d) self.assertEqual(content, mock.content)
def test_utf_json_encoded(self): mock = Mock() mock._headers = {HTTP_CONTENT_TYPE: 'application/json; charset=UTF-8'} d = {'x': u'语'} mock.content = json.dumps(d) mock.get = mock._headers.get factory = ResponseModelFactory(mock) body, content = factory.body() self.assertDictEqual(json.loads(body), d) self.assertEqual(content, mock.content)
def test_utf_json_encoded_no_charset(self): """default to UTF-8""" mock = Mock() mock._headers = {HTTP_CONTENT_TYPE: 'application/json'} d = {'x': u'语'} mock.content = json.dumps(d).encode('UTF-8') mock.get = mock._headers.get factory = ResponseModelFactory(mock) body, content = factory.body() self.assertDictEqual(json.loads(body), d) self.assertEqual(content, mock.content.decode('UTF-8'))
def test_bytes_compat(self): """ Test ResponseModelFactory formats json with bytes content """ mock = Mock() mock.headers = {HTTP_CONTENT_TYPE: 'application/json;'} d = {'k': 'v'} mock.content = bytes(json.dumps(d), 'utf-8') mock.get = mock.headers.get factory = ResponseModelFactory(mock) body, content = factory.body() self.assertDictEqual(json.loads(body), d)
def test_invalid_encoding_json(self): mock = Mock() mock._headers = { HTTP_CONTENT_TYPE: 'application/json; charset=asdas-8' } d = {'x': u'语'} mock.content = json.dumps(d).encode('UTF-8') mock.get = mock._headers.get factory = ResponseModelFactory(mock) body, content = factory.body() self.assertDictEqual(json.loads(body, encoding='UTF-8'), d) self.assertEqual(mock.content.decode('UTF-8'), content)
def test_python2_unicode_content_compat(self): """ Test ResponseModelFactory formats json for unicode """ if sys.version_info < (3, 0, 0): mock = Mock() mock._headers = {HTTP_CONTENT_TYPE: 'application/json;'} d = u'{"k": "v"}' mock.content = d mock.get = mock._headers.get factory = ResponseModelFactory(mock) body, content = factory.body() self.assertDictEqual(json.loads(body), json.loads(d))
def test_python2_str_content_compat(self): """ Test ResponseModelFactory formats json for str """ if sys.version_info < (3, 0, 0): mock = Mock() mock._headers = {HTTP_CONTENT_TYPE: 'application/json;'} d = {'k': 'v'} mock.content = str(json.dumps(d)) mock.get = mock._headers.get factory = ResponseModelFactory(mock) body, content = factory.body() self.assertDictEqual(json.loads(body), d)
def test_python2_bytes_content_compat(self): """ Test that ResponseModelFactory returns correct json string for a bytestring content """ if sys.version_info < (3, 0, 0): mock = Mock() mock._headers = {HTTP_CONTENT_TYPE: 'application/json;'} d = {'k': 'v'} mock.content = bytes(json.dumps(d)) mock.get = mock._headers.get factory = ResponseModelFactory(mock) body, content = factory.body() self.assertDictEqual(json.loads(body), d)