我正在构建一个 REST API,Django
在某些地方我需要发送带有许多参数的 HTTP GET。因此,我决定在 request.body 中将它们作为 JSON 发送。现在,该应用程序可以正常工作,但不能进行单元测试。我似乎找不到使用 self.client.get() 在正文中发送 JSON 参数的方法。这就是我正在做的事情:
import json
from django.test import TestCase
from django.core.urlresolvers import reverse
class RestApiTests(TestCase):
def test_analysis():
extra = {'CONTENT_TYPE': 'application/json'}
resp = self.client.get(reverse('restapi_analysis'), data="{'analysisID': 41}", **extra)
self.assertEqual(json.loads(resp.content)['statusText'], 'Analysis fetched successfully')
使用它并运行单元测试我收到以下错误:
Traceback (most recent call last):
File "/home/anascu/svn/django-tc-reporting/tcsite/tcapp/tests/test_restapi.py", line 151, in test_analysis
resp = self.client.get(reverse('restapi_analysis'), data="{'analysisID': 41}", **extra)
File "/home/anascu/virtenv/tc-tracker/local/lib/python2.7/site-packages/django/test/client.py", line 439, in get
response = super(Client, self).get(path, data=data, **extra)
File "/home/anascu/virtenv/tc-tracker/local/lib/python2.7/site-packages/django/test/client.py", line 240, in get
'QUERY_STRING': urlencode(data, doseq=True) or parsed[4],
File "/home/anascu/virtenv/tc-tracker/local/lib/python2.7/site-packages/django/utils/http.py", line 75, in urlencode
for k, v in query],
ValueError: need more than 1 value to unpack
POST 工作正常,但在这种情况下我需要使用 GET。甚至可能吗?Django 版本是 1.4.5。