2013-01-18 01:50:31 +00:00
|
|
|
import socket, time
|
2012-07-15 20:42:45 +00:00
|
|
|
from netlib import tcp
|
2013-01-06 03:44:12 +00:00
|
|
|
from libpathod import pathoc
|
2011-03-05 22:21:31 +00:00
|
|
|
import tutils
|
|
|
|
|
2012-06-10 01:17:18 +00:00
|
|
|
"""
|
|
|
|
Note that the choice of response code in these tests matters more than you
|
|
|
|
might think. libcurl treats a 304 response code differently from, say, a
|
|
|
|
200 response code - it will correctly terminate a 304 response with no
|
|
|
|
content-length header, whereas it will block forever waiting for content
|
|
|
|
for a 200 response.
|
|
|
|
"""
|
|
|
|
|
2012-06-14 21:20:10 +00:00
|
|
|
class SanityMixin:
|
2011-03-05 22:21:31 +00:00
|
|
|
def test_http(self):
|
2012-06-10 01:17:18 +00:00
|
|
|
assert self.pathod("304").status_code == 304
|
2012-07-08 22:58:28 +00:00
|
|
|
assert self.master.state.view
|
2011-03-05 22:21:31 +00:00
|
|
|
|
2012-06-13 06:16:47 +00:00
|
|
|
def test_large(self):
|
2012-06-14 21:20:10 +00:00
|
|
|
assert len(self.pathod("200:b@50k").content) == 1024*50
|
2012-06-13 06:16:47 +00:00
|
|
|
|
2012-07-08 23:03:55 +00:00
|
|
|
def test_replay(self):
|
|
|
|
assert self.pathod("304").status_code == 304
|
|
|
|
assert len(self.master.state.view) == 1
|
|
|
|
l = self.master.state.view[0]
|
|
|
|
assert l.response.code == 304
|
|
|
|
l.request.path = "/p/305"
|
2012-07-10 11:29:33 +00:00
|
|
|
rt = self.master.replay_request(l, block=True)
|
2012-07-08 23:03:55 +00:00
|
|
|
assert l.response.code == 305
|
|
|
|
|
2012-07-08 23:18:03 +00:00
|
|
|
# Disconnect error
|
|
|
|
l.request.path = "/p/305:d0"
|
2012-07-10 11:29:33 +00:00
|
|
|
rt = self.master.replay_request(l, block=True)
|
2012-07-08 23:18:03 +00:00
|
|
|
assert l.error
|
|
|
|
|
|
|
|
# Port error
|
|
|
|
l.request.port = 1
|
2012-07-15 20:42:59 +00:00
|
|
|
self.master.replay_request(l, block=True)
|
2012-07-08 23:18:03 +00:00
|
|
|
assert l.error
|
|
|
|
|
2011-03-05 22:21:31 +00:00
|
|
|
|
2012-06-14 21:20:10 +00:00
|
|
|
class TestHTTP(tutils.HTTPProxTest, SanityMixin):
|
2012-06-30 03:59:42 +00:00
|
|
|
def test_invalid_http(self):
|
|
|
|
t = tcp.TCPClient("127.0.0.1", self.proxy.port)
|
|
|
|
t.connect()
|
|
|
|
t.wfile.write("invalid\n\n")
|
|
|
|
t.wfile.flush()
|
|
|
|
assert "Bad Request" in t.rfile.readline()
|
|
|
|
|
|
|
|
def test_invalid_connect(self):
|
|
|
|
t = tcp.TCPClient("127.0.0.1", self.proxy.port)
|
|
|
|
t.connect()
|
|
|
|
t.wfile.write("CONNECT invalid\n\n")
|
|
|
|
t.wfile.flush()
|
|
|
|
assert "Bad Request" in t.rfile.readline()
|
2012-06-09 00:13:01 +00:00
|
|
|
|
2013-01-06 03:44:12 +00:00
|
|
|
def test_upstream_ssl_error(self):
|
|
|
|
p = self.pathoc()
|
|
|
|
ret = p.request("get:'https://localhost:%s/'"%self.server.port)
|
|
|
|
assert ret[1] == 400
|
|
|
|
|
|
|
|
def test_http(self):
|
|
|
|
f = self.pathod("304")
|
|
|
|
assert f.status_code == 304
|
|
|
|
|
|
|
|
l = self.master.state.view[0]
|
|
|
|
assert l.request.client_conn.address
|
|
|
|
assert "host" in l.request.headers
|
|
|
|
assert l.response.code == 304
|
|
|
|
|
2012-06-09 00:13:01 +00:00
|
|
|
|
2012-06-14 21:20:10 +00:00
|
|
|
class TestHTTPS(tutils.HTTPProxTest, SanityMixin):
|
2012-06-09 00:13:01 +00:00
|
|
|
ssl = True
|
2013-01-18 04:08:30 +00:00
|
|
|
# FIXME: Instrument pathod to actually test that client cert is being sent
|
|
|
|
# correctly.
|
|
|
|
clientcerts = True
|
2012-06-09 00:13:01 +00:00
|
|
|
|
|
|
|
|
2012-06-14 21:20:10 +00:00
|
|
|
class TestReverse(tutils.ReverseProxTest, SanityMixin):
|
2012-06-09 08:41:28 +00:00
|
|
|
reverse = True
|
|
|
|
|
|
|
|
|
2012-06-14 21:47:04 +00:00
|
|
|
class TestTransparent(tutils.TransparentProxTest, SanityMixin):
|
2012-06-14 21:20:10 +00:00
|
|
|
transparent = True
|
|
|
|
|
|
|
|
|
|
|
|
class TestProxy(tutils.HTTPProxTest):
|
2011-03-05 22:21:31 +00:00
|
|
|
def test_http(self):
|
2012-06-10 01:17:18 +00:00
|
|
|
f = self.pathod("304")
|
|
|
|
assert f.status_code == 304
|
2012-01-20 23:43:00 +00:00
|
|
|
|
2012-07-08 22:58:28 +00:00
|
|
|
l = self.master.state.view[0]
|
|
|
|
assert l.request.client_conn.address
|
|
|
|
assert "host" in l.request.headers
|
|
|
|
assert l.response.code == 304
|
2013-01-17 15:32:56 +00:00
|
|
|
|
|
|
|
def test_response_timestamps(self):
|
|
|
|
# test that we notice at least 2 sec delay between timestamps
|
|
|
|
# in response object
|
|
|
|
f = self.pathod("304:b@1k:p50,2")
|
|
|
|
assert f.status_code == 304
|
|
|
|
|
|
|
|
response = self.master.state.view[0].response
|
|
|
|
assert 2 <= response.timestamp_end - response.timestamp_start <= 2.2
|
|
|
|
|
|
|
|
def test_request_timestamps(self):
|
|
|
|
# test that we notice at least 2 sec delay between timestamps
|
|
|
|
# in request object
|
|
|
|
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
connection.connect(("127.0.0.1", self.proxy.port))
|
|
|
|
|
|
|
|
# call pathod server, wait a second to complete the request
|
|
|
|
connection.send("GET http://localhost:%d/p/304:b@1k HTTP/1.1\r\n"%self.server.port)
|
2013-01-18 01:52:19 +00:00
|
|
|
time.sleep(0.1)
|
2013-01-17 15:32:56 +00:00
|
|
|
connection.send("\r\n");
|
|
|
|
connection.recv(50000)
|
|
|
|
connection.close()
|
|
|
|
|
|
|
|
request, response = self.master.state.view[0].request, self.master.state.view[0].response
|
|
|
|
assert response.code == 304 # sanity test for our low level request
|
2013-01-18 01:52:19 +00:00
|
|
|
assert 0 <= request.timestamp_end - request.timestamp_start <= 0.2
|
2013-01-17 15:32:56 +00:00
|
|
|
|
|
|
|
def test_request_timestamps_not_affected_by_client_time(self):
|
|
|
|
# test that don't include user wait time in request's timestamps
|
|
|
|
|
|
|
|
f = self.pathod("304:b@10k")
|
|
|
|
assert f.status_code == 304
|
|
|
|
f = self.pathod("304:b@10k")
|
|
|
|
assert f.status_code == 304
|
|
|
|
|
|
|
|
request = self.master.state.view[0].request
|
|
|
|
assert request.timestamp_end - request.timestamp_start <= 0.1
|
|
|
|
|
|
|
|
request = self.master.state.view[1].request
|
|
|
|
assert request.timestamp_end - request.timestamp_start <= 0.1
|