Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix format of Django API view response data #608

Merged
merged 1 commit into from
Jan 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions chatterbot/ext/django_chatterbot/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,6 @@ def post(self, request, *args, **kwargs):
response = self.chatterbot.get_response(input_data, chat_session.id_string)
response_data = response.serialize()

try:
unicode = unicode
except NameError:
# Must be python 3
unicode = str
basestring = (str, bytes)

if isinstance(response_data, basestring):
response_data = response_data.encode('utf8')
else:
response_data = unicode(response_data).encode('utf8')

response_data = {'text': str(response_data)}

return JsonResponse(response_data, status=200)

def get(self, request, *args, **kwargs):
Expand Down
45 changes: 28 additions & 17 deletions tests_django/test_api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
import json
from django.test import TestCase
from django.core.urlresolvers import reverse
Expand All @@ -13,6 +14,9 @@ def test_post(self):
"""
Test that a response is returned.
"""
from datetime import datetime
from dateutil.parser import parse

data = {
'text': 'How are you?'
}
Expand All @@ -23,25 +27,23 @@ def test_post(self):
format='json'
)

content = json.loads(response.content.decode('utf-8'))

self.assertEqual(response.status_code, 200)
self.assertIn('text', str(response.content))
self.assertIn('in_response_to', str(response.content))
self.assertIn('text', content)
self.assertGreater(len(content['text']), 1)
self.assertIn('in_response_to', content)
self.assertIn('created_at', content)
self.assertTrue(
isinstance(parse(content['created_at']), datetime)
)

def test_response_is_in_json(self):
def test_post_unicode(self):
"""
Test Response is in JSON
Test that a response is returned.
"""
def is_json(content):
try:
if isinstance(content, bytes):
content = content.decode(encoding='utf-8')
json_object = json.loads(content)
except ValueError:
return False
return True

data = {
'text': 'Is this JSON Data?'
'text': u'سلام'
}
response = self.client.post(
self.api_url,
Expand All @@ -50,15 +52,19 @@ def is_json(content):
format='json'
)

content = json.loads(response.content.decode('utf-8'))

self.assertEqual(response.status_code, 200)
self.assertTrue(is_json(response.content))
self.assertIn('text', content)
self.assertGreater(len(content['text']), 1)
self.assertIn('in_response_to', content)

def test_unicode_post(self):
def test_escaped_unicode_post(self):
"""
Test that unicode reponse
"""
data = {
'text' : u'\u2013'
'text' : '\u2013'
}
response = self.client.post(
self.api_url,
Expand Down Expand Up @@ -100,6 +106,11 @@ def test_patch(self):

self.assertEqual(response.status_code, 405)

def test_put(self):
response = self.client.put(self.api_url)

self.assertEqual(response.status_code, 405)

def test_delete(self):
response = self.client.delete(self.api_url)

Expand Down