Skip to content

Instantly share code, notes, and snippets.

@jerith
Created January 13, 2016 13:07
Show Gist options
  • Select an option

  • Save jerith/c063dd4d500b0de66253 to your computer and use it in GitHub Desktop.

Select an option

Save jerith/c063dd4d500b0de66253 to your computer and use it in GitHub Desktop.
from twisted.internet import reactor
from twisted.internet.defer import inlineCallbacks
from twisted.internet.endpoints import serverFromString, clientFromString
from twisted.trial import unittest
from twisted.test import proto_helpers
from twisted.web.client import ProxyAgent, readBody
from vumi_http_proxy.http_proxy import ProxyFactory
class CheckProxyRequestTestCase(unittest.TestCase):
def test_artisanal(self):
factory = ProxyFactory()
proto = factory.buildProtocol(('0.0.0.0', 0))
tr = proto_helpers.StringTransport()
proto.makeConnection(tr)
proto.dataReceived("\r\n".join([
"GET http://zombo.com/ HTTP/1.1",
"Host: zombo.com",
"",
"",
]))
self.assertEqual(tr.value(), "\r\n".join([
"HTTP/1.1 200 OK",
"Transfer-Encoding: chunked",
"",
"13",
"<html>Denied</html>",
"0",
"",
"",
]))
@inlineCallbacks
def test_with_client(self):
server_endpoint = serverFromString(reactor, "tcp:0:interface=127.0.0.1")
server = yield server_endpoint.listen(ProxyFactory())
self.addCleanup(server.stopListening)
port = server.getHost().port
client_endpoint = clientFromString(
reactor, "tcp:host=localhost:port=%s" % (port,))
agent = ProxyAgent(client_endpoint)
response = yield agent.request("GET", "http://zombo.com/")
response_body = yield readBody(response)
self.assertEqual(response.code, 200) # TODO: This should be 400.
self.assertEqual(response_body, "<html>Denied</html>")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment