Hi,<div><br></div><div>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.</div><div><br></div><div>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...</div>
<div><br></div><div><div>import urllib</div><div><br></div><div>from twisted.internet import reactor</div><div>from twisted.web import resource, server, client, error</div><div>from twisted.trial import unittest</div></div>
<div><br></div><div><div>class TestResource(resource.Resource):</div><div><br></div><div> def render_POST(self, request):</div><div> return str(request.args)</div><div><br></div><div><br></div><div>class SimpleResourceTest(unittest.TestCase):</div>
<div> </div><div> def _listen(self, site):</div><div> return reactor.listenTCP(0, site, interface="<a href="http://127.0.0.1">127.0.0.1</a>")</div><div> </div><div> def setUp(self):</div><div>
r = resource.Resource()</div><div> r.putChild("test", TestResource())</div><div> site = server.Site(r, timeout=None)</div><div> self.port = self._listen(site)</div><div> self.portno = self.port.getHost().port</div>
<div><br></div><div> def tearDown(self):</div><div> if self.port:</div><div> return self.port.stopListening()</div><div><br></div><div> def getURL(self, path):</div><div> return "http://127.0.0.1:%d/%s" % (self.portno, path)</div>
<div><br></div><div> def test_postdata(self):</div><div> d = client.getPage(self.getURL("test"), method="POST",</div><div> postdata=urllib.urlencode({"test": "data"}))</div>
<div> d.addCallback(self.failIfEqual, "{}")</div><div> return d</div></div><div><div><br></div><div><br></div><div>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.</div>
<div><br></div><div>Regards,</div><div><br></div><div>Teemu</div><br>-- <br>Teemu Harju<br><br>email/jabber: <a href="mailto:teemu.harju@gmail.com">teemu.harju@gmail.com</a><br>blog: <a href="http://www.teemuharju.net">http://www.teemuharju.net</a><br>
<br>~~ "A computer is like air conditioning: it becomes useless when you open windows." ~~
</div>