python - Unit testing Django JSON View -


i'm trying write unit tests django json_view views , i'm having trouble passing json_string view. posted related question yesterday passing json string django view js, issue in js passing json string needed passing string attribute of object, because failing string being taken key resulting query dict. i'm having similar problem again except time form django unit test django view. here simplified version of code produces same result.

class mytestcase(testcase):     def setup(self):         self.u = user.objects.create_user('test','test','test')         self.u.is_active = true         self.u.save()         self.client.login(username='test',password='test')      def test_create_object_from_form(self):         """test creation of instance form data."""         import json         json_string json.dumps({'resource':{'type':'book','author':'john doe'}})         print(json_string)         response = self.client.post(reverse('ajax_view'),                                     {'form':json_string},'json')         self.assetnotcontains(response,'error') 

and view looks this

@json_view def ajax_view(request):     """process incoming form data."""     if request.method == 'post':         print(request.post)         form_data = json.loads(request.post['form'])         resource_data = form_data['resource']         form = myuserform(resource_data)          if form.is_valid():         ... 

here 2 print statements produce when test run. json_string is

{"resource": {"type": "book", "author": "john doe"}} 

and query dict looks like

<querydict: {u'{\'form\': \'{"resource": {"type": "book", "author": "john doe"}}\'}': [u'']}> 

i'm total newbie js , ajax, don't worry hurting pride, answer close jump , bite me.

final edit

i stated header http_x_requested_with='xmlhttprequest' necessary in post call false while in tests. header necessary csrf middleware csrf disabled in tests. however, still believe practice put in test if middleware disables csrf since javascript library pass header default when doing ajax. also, if piece of code not disabled ever use is_ajax method, won't need debug unittest hours figure out header missing.

the problem content-type because when django gets value in there different text/html, doesn't use default post data handling format data in query: type=book&author=johndoe example.

then fixed code is:

response = self.client.post(reverse('ajax_view'),                             {'form':json_string},                              http_x_requested_with='xmlhttprequest') 

here's how i'm using myself:

post_data = {      "jsonrpc" : "2.0", "method": method, "params" : params, "id" : id } return client.post('/api/json/',                      json.dumps(post_data), "text/json",                                 http_x_requested_with='xmlhttprequest') 

to json-rpc. notice since pass different content-type default value, data passed in post request.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -