Ticket #3160: test_client_raw_post_v2.patch
File test_client_raw_post_v2.patch, 4.6 KB (added by , 18 years ago) |
---|
-
django/test/client.py
176 187 177 188 return self.request(**r) 178 189 190 def raw_post(self, path, data, type, **extra): 191 """ 192 Request a response from the server using POST. Raw post data and a 193 content type argument are supplied and passed unmodified. 194 """ 195 r = { 196 'CONTENT_LENGTH': len(data), 197 'CONTENT_TYPE': type, 198 'PATH_INFO': path, 199 'REQUEST_METHOD': 'POST', 200 'wsgi.input': StringIO(data), 201 } 202 r.update(extra) 203 204 return self.request(**r) 205 179 206 def login(self, path, username, password, **extra): 180 207 """ 181 208 A specialized sequence of GET and POST to log into a view that -
tests/modeltests/test_client/views.py
1 from xml.dom.minidom import parseString 1 2 from django.template import Context, Template 2 3 from django.http import HttpResponse, HttpResponseRedirect 3 4 from django.contrib.auth.decorators import login_required … … 22 23 23 24 return HttpResponse(t.render(c)) 24 25 26 def raw_post_view(request): 27 """A view which expects raw XML to be posted and returns content extracted 28 from the XML""" 29 if request.POST: 30 root = parseString(request.raw_post_data) 31 first_book = root.firstChild.firstChild 32 title, author = [n.firstChild.nodeValue for n in first_book.childNodes] 33 t = Template("{{ title }} - {{ author }}", name="Book template") 34 c = Context({"title": title, "author": author}) 35 else: 36 t = Template("GET request.", name="Book GET template") 37 c = Context() 38 39 return HttpResponse(t.render(c)) 40 25 41 def redirect_view(request): 26 42 "A view that redirects all requests to the GET view" 27 43 return HttpResponseRedirect('/test_client/get_view/') -
tests/modeltests/test_client/models.py
66 66 self.assertEqual(response.template.name, 'POST Template') 67 67 self.failUnless('Data received' in response.content) 68 68 69 def test_raw_post_view(self): 70 test_doc = """<?xml version="1.0" encoding="utf-8"?><library><book><title>Blink</title><author>Malcolm Gladwell</author></book></library>""" 71 response = self.client.raw_post("/test_client/raw_post_view/", test_doc, 72 "text/xml") 73 self.assertEqual(response.status_code, 200) 74 self.assertEqual(response.template.name, "Book template") 75 self.assertEqual(response.content, "Blink - Malcolm Gladwell") 76 69 77 def test_redirect(self): 70 78 "GET a URL that redirects elsewhere" 71 79 response = self.client.get('/test_client/redirect_view/') -
tests/modeltests/test_client/urls.py
4 4 urlpatterns = patterns('', 5 5 (r'^get_view/$', views.get_view), 6 6 (r'^post_view/$', views.post_view), 7 (r'^raw_post_view/$', views.raw_post_view), 7 8 (r'^redirect_view/$', views.redirect_view), 8 9 (r'^login_protected_view/$', views.login_protected_view), 9 10 ) -
docs/testing.txt
242 242 file name), and `attachment_file` (containing the file data). Note that you 243 243 need to manually close the file after it has been provided to the POST. 244 244 245 ``raw_post(path, data, type)`` 246 Make a POST request on the provided ``path``. The supplied data is sent 247 unmodified. Type is used as the value of the HTTP ``Content-Type`` header. 248 This can be used to send non-standard requests to a view, such as posting 249 XML to test SOAP views, or JSON, etc. For example:: 250 251 c = Client() 252 book_data = '<book><title>Blink</title><author>Malcolm Gladwell</author></book>' 253 c.raw_post('/api/book/create/', book_data, "text/xml") 254 245 255 ``login(path, username, password)`` 246 256 In a production site, it is likely that some views will be protected with 247 257 the @login_required URL provided by ``django.contrib.auth``. Interacting