[Twisted-Python] Problem with unittesting HTTP POST resource

Grant Baillie grant at osafoundation.org
Tue Apr 8 15:12:18 MDT 2008


On 8 Apr, 2008, at 04:28, Teemu Harju wrote:

> ...
>

> My problem is that I'm experimenting with "trial" to be able to test  
> my code. (I've heard that it is a wise thing to do ;-)). What I have  
> is a simple HTTP resource that allows POSTing data. I made this  
> example to illustrate my problem...
>
> ...

>

>     def test_postdata(self):
>         d = client.getPage(self.getURL("test"), method="POST",
>                            postdata=urllib.urlencode({"test":  
> "data"}))
>         d.addCallback(self.failIfEqual, "{}")
>         return d
>

You need to specify a Content-Type in your POST request, e.g.:

         d = client.getPage(self.getURL("test"), method="POST",
                            postdata=urllib.urlencode({"test": "data"}),
                            headers={"Content-Type": "application/x- 
www-form-urlencoded"})

Otherwise, your POST request is sent as text/html, and the HTTP server  
code won't know to parse args out of the request body.

(Probably this should have gone to twisted-web rather than twisted- 
python, since in the end it wasn't really about trial, but http ;).

--Grant






More information about the Twisted-Python mailing list