2013-01-06 03:44:12 +00:00
|
|
|
from libmproxy import proxy, flow
|
2012-06-09 01:42:43 +00:00
|
|
|
import tutils
|
2013-01-06 03:44:12 +00:00
|
|
|
from libpathod import test
|
2013-01-28 22:35:57 +00:00
|
|
|
from netlib import http, tcp
|
2013-01-28 08:59:03 +00:00
|
|
|
import mock
|
2010-02-16 04:09:07 +00:00
|
|
|
|
2012-06-10 04:49:59 +00:00
|
|
|
|
2012-07-08 22:58:28 +00:00
|
|
|
def test_proxy_error():
|
|
|
|
p = proxy.ProxyError(111, "msg")
|
|
|
|
assert str(p)
|
2010-02-16 04:09:07 +00:00
|
|
|
|
2012-06-18 22:42:55 +00:00
|
|
|
|
2012-07-08 22:58:28 +00:00
|
|
|
def test_app_registry():
|
|
|
|
ar = proxy.AppRegistry()
|
|
|
|
ar.add("foo", "domain", 80)
|
2012-06-18 22:42:55 +00:00
|
|
|
|
2012-07-08 22:58:28 +00:00
|
|
|
r = tutils.treq()
|
|
|
|
r.host = "domain"
|
|
|
|
r.port = 80
|
|
|
|
assert ar.get(r)
|
2012-06-18 22:42:55 +00:00
|
|
|
|
2012-07-08 22:58:28 +00:00
|
|
|
r.port = 81
|
|
|
|
assert not ar.get(r)
|
2012-07-10 03:53:53 +00:00
|
|
|
|
|
|
|
r = tutils.treq()
|
|
|
|
r.host = "domain2"
|
|
|
|
r.port = 80
|
|
|
|
assert not ar.get(r)
|
|
|
|
r.headers["host"] = ["domain"]
|
|
|
|
assert ar.get(r)
|
2013-01-06 03:44:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestServerConnection:
|
|
|
|
def setUp(self):
|
|
|
|
self.d = test.Daemon()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.d.shutdown()
|
|
|
|
|
|
|
|
def test_simple(self):
|
2013-02-24 09:24:21 +00:00
|
|
|
sc = proxy.ServerConnection(proxy.ProxyConfig(), "http", self.d.IFACE, self.d.port, "host.com")
|
|
|
|
sc.connect()
|
2013-01-06 03:44:12 +00:00
|
|
|
r = tutils.treq()
|
|
|
|
r.path = "/p/200:da"
|
|
|
|
sc.send(r)
|
|
|
|
assert http.read_response(sc.rfile, r.method, 1000)
|
|
|
|
assert self.d.last_log()
|
|
|
|
|
|
|
|
r.content = flow.CONTENT_MISSING
|
|
|
|
tutils.raises("incomplete request", sc.send, r)
|
|
|
|
|
2013-01-28 08:59:03 +00:00
|
|
|
sc.terminate()
|
|
|
|
|
|
|
|
def test_terminate_error(self):
|
2013-02-24 09:24:21 +00:00
|
|
|
sc = proxy.ServerConnection(proxy.ProxyConfig(), "http", self.d.IFACE, self.d.port, "host.com")
|
|
|
|
sc.connect()
|
2013-01-28 08:59:03 +00:00
|
|
|
sc.connection = mock.Mock()
|
|
|
|
sc.connection.close = mock.Mock(side_effect=IOError)
|
|
|
|
sc.terminate()
|
2013-01-28 22:35:57 +00:00
|
|
|
|