Hi,<div><br></div><div>I&#39;m kind of a newbie in what comes to using Twisted and I&#39;m stuck with a problem that I cannot seem to solve.</div><div><br></div><div>My problem is that I&#39;m experimenting with &quot;trial&quot; to be able to test my code. (I&#39;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>&nbsp;&nbsp; &nbsp;def render_POST(self, request):</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;return str(request.args)</div><div><br></div><div><br></div><div>class SimpleResourceTest(unittest.TestCase):</div>
<div>&nbsp;&nbsp; &nbsp;</div><div>&nbsp;&nbsp; &nbsp;def _listen(self, site):</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;return reactor.listenTCP(0, site, interface=&quot;<a href="http://127.0.0.1">127.0.0.1</a>&quot;)</div><div>&nbsp;&nbsp; &nbsp;</div><div>&nbsp;&nbsp; &nbsp;def setUp(self):</div><div>
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;r = resource.Resource()</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;r.putChild(&quot;test&quot;, TestResource())</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;site = server.Site(r, timeout=None)</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;self.port = self._listen(site)</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;self.portno = self.port.getHost().port</div>
<div><br></div><div>&nbsp;&nbsp; &nbsp;def tearDown(self):</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if self.port:</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return self.port.stopListening()</div><div><br></div><div>&nbsp;&nbsp; &nbsp;def getURL(self, path):</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;return &quot;http://127.0.0.1:%d/%s&quot; % (self.portno, path)</div>
<div><br></div><div>&nbsp;&nbsp; &nbsp;def test_postdata(self):</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;d = client.getPage(self.getURL(&quot;test&quot;), method=&quot;POST&quot;,</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; postdata=urllib.urlencode({&quot;test&quot;: &quot;data&quot;}))</div>
<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;d.addCallback(self.failIfEqual, &quot;{}&quot;)</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;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&#39;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&#39;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>~~ &quot;A computer is like air conditioning: it becomes useless when you open windows.&quot; ~~
</div>