[Twisted-Python] Problem with unittesting HTTP POST resource
Hi, I'm kind of a newbie in what comes to using Twisted and I'm stuck with a problem that I cannot seem to solve. 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... import urllib from twisted.internet import reactor from twisted.web import resource, server, client, error from twisted.trial import unittest class TestResource(resource.Resource): def render_POST(self, request): return str(request.args) class SimpleResourceTest(unittest.TestCase): def _listen(self, site): return reactor.listenTCP(0, site, interface="127.0.0.1") def setUp(self): r = resource.Resource() r.putChild("test", TestResource()) site = server.Site(r, timeout=None) self.port = self._listen(site) self.portno = self.port.getHost().port def tearDown(self): if self.port: return self.port.stopListening() def getURL(self, path): return "http://127.0.0.1:%d/%s" % (self.portno, path) def test_postdata(self): d = client.getPage(self.getURL("test"), method="POST", postdata=urllib.urlencode({"test": "data"})) d.addCallback(self.failIfEqual, "{}") return d I used some of the unittests in Twisted Web as an example and at least I think I've got this right. However, as you can see if you run this, the test fails. Which means that the request.args dictionary is empty. However, there should be something since I've set the postdata parameter on the client.getPage() method. I would really appreciate all the help here since I cannot figure out where the postdata disappears. Regards, Teemu -- Teemu Harju email/jabber: teemu.harju@gmail.com blog: http://www.teemuharju.net ~~ "A computer is like air conditioning: it becomes useless when you open windows." ~~
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
Thanks a lot for the quick reply! I was kind of struggling in my mind about whether to post to twisted-python or twisted-web list and I guess I chose the wrong one because I assumed that the problem was in they way I was using trial. Anyways, thanks again and Twisted is awesome. ;-) - Teemu On Wed, Apr 9, 2008 at 12:12 AM, Grant Baillie <grant@osafoundation.org> wrote:
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
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
-- Teemu Harju email/jabber: teemu.harju@gmail.com blog: http://www.teemuharju.net ~~ "A computer is like air conditioning: it becomes useless when you open windows." ~~
participants (2)
-
Grant Baillie -
Teemu Harju