Ticket #3160: test_client_raw_post_v2.patch

File test_client_raw_post_v2.patch, 4.6 KB (added by anonymous, 18 years ago)

Revised version of raw_post patch, includes tests and docs

  • django/test/client.py

     
    176187
    177188        return self.request(**r)
    178189
     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
    179206    def login(self, path, username, password, **extra):
    180207        """
    181208        A specialized sequence of GET and POST to log into a view that
  • tests/modeltests/test_client/views.py

     
     1from xml.dom.minidom import parseString
    12from django.template import Context, Template
    23from django.http import HttpResponse, HttpResponseRedirect
    34from django.contrib.auth.decorators import login_required
     
    2223       
    2324    return HttpResponse(t.render(c))
    2425   
     26def 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
    2541def redirect_view(request):
    2642    "A view that redirects all requests to the GET view"
    2743    return HttpResponseRedirect('/test_client/get_view/')
  • tests/modeltests/test_client/models.py

     
    6666        self.assertEqual(response.template.name, 'POST Template')
    6767        self.failUnless('Data received' in response.content)
    6868       
     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
    6977    def test_redirect(self):
    7078        "GET a URL that redirects elsewhere"
    7179        response = self.client.get('/test_client/redirect_view/')
  • tests/modeltests/test_client/urls.py

     
    44urlpatterns = patterns('',
    55    (r'^get_view/$', views.get_view),
    66    (r'^post_view/$', views.post_view),
     7    (r'^raw_post_view/$', views.raw_post_view),
    78    (r'^redirect_view/$', views.redirect_view),
    89    (r'^login_protected_view/$', views.login_protected_view),
    910)
  • docs/testing.txt

     
    242242    file name), and `attachment_file` (containing the file data). Note that you
    243243    need to manually close the file after it has been provided to the POST.
    244244
     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
    245255``login(path, username, password)``
    246256    In a production site, it is likely that some views will be protected with
    247257    the @login_required URL provided by ``django.contrib.auth``. Interacting
Back to Top