27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 import unittest |
29 import unittest |
30 import socket |
30 import socket |
31 |
31 |
32 import http |
32 import httpplus |
33 |
33 |
34 # relative import to ease embedding the library |
34 # relative import to ease embedding the library |
35 import util |
35 import util |
36 |
36 |
37 |
37 |
38 def make_preloaded_socket(data): |
38 def make_preloaded_socket(data, close=False): |
39 """Make a socket pre-loaded with data so it can be read during connect. |
39 """Make a socket pre-loaded with data so it can be read during connect. |
40 |
40 |
41 Useful for https proxy tests because we have to read from the |
41 Useful for https proxy tests because we have to read from the |
42 socket during _connect rather than later on. |
42 socket during _connect rather than later on. |
43 """ |
43 """ |
44 def s(*args, **kwargs): |
44 def s(*args, **kwargs): |
45 sock = util.MockSocket(*args, **kwargs) |
45 sock = util.MockSocket(*args, **kwargs) |
46 sock.early_data = data[:] |
46 sock.early_data = data[:] |
|
47 sock.close_on_empty = close |
47 return sock |
48 return sock |
48 return s |
49 return s |
49 |
50 |
50 |
51 |
51 class ProxyHttpTest(util.HttpTestBase, unittest.TestCase): |
52 class ProxyHttpTest(util.HttpTestBase, unittest.TestCase): |
52 |
53 |
53 def _run_simple_test(self, host, server_data, expected_req, expected_data): |
54 def _run_simple_test(self, host, server_data, expected_req, expected_data): |
54 con = http.HTTPConnection(host) |
55 con = httpplus.HTTPConnection(host) |
55 con._connect() |
56 con._connect() |
56 con.sock.data = server_data |
57 con.sock.data = server_data |
57 con.request('GET', '/') |
58 con.request('GET', '/') |
58 |
59 |
59 self.assertEqual(expected_req, con.sock.sent) |
60 self.assertEqual(expected_req, con.sock.sent) |
60 self.assertEqual(expected_data, con.getresponse().read()) |
61 self.assertEqual(expected_data, con.getresponse().read()) |
61 |
62 |
62 def testSimpleRequest(self): |
63 def testSimpleRequest(self): |
63 con = http.HTTPConnection('1.2.3.4:80', |
64 con = httpplus.HTTPConnection('1.2.3.4:80', |
64 proxy_hostport=('magicproxy', 4242)) |
65 proxy_hostport=('magicproxy', 4242)) |
65 con._connect() |
66 con._connect() |
66 con.sock.data = ['HTTP/1.1 200 OK\r\n', |
67 con.sock.data = ['HTTP/1.1 200 OK\r\n', |
67 'Server: BogusServer 1.0\r\n', |
68 'Server: BogusServer 1.0\r\n', |
68 'MultiHeader: Value\r\n' |
69 'MultiHeader: Value\r\n' |
86 resp.headers.getheaders('multiheader')) |
87 resp.headers.getheaders('multiheader')) |
87 self.assertEqual(['BogusServer 1.0'], |
88 self.assertEqual(['BogusServer 1.0'], |
88 resp.headers.getheaders('server')) |
89 resp.headers.getheaders('server')) |
89 |
90 |
90 def testSSLRequest(self): |
91 def testSSLRequest(self): |
91 con = http.HTTPConnection('1.2.3.4:443', |
92 con = httpplus.HTTPConnection('1.2.3.4:443', |
92 proxy_hostport=('magicproxy', 4242)) |
93 proxy_hostport=('magicproxy', 4242)) |
93 socket.socket = make_preloaded_socket( |
94 socket.socket = make_preloaded_socket( |
94 ['HTTP/1.1 200 OK\r\n', |
95 ['HTTP/1.1 200 OK\r\n', |
95 'Server: BogusServer 1.0\r\n', |
96 'Server: BogusServer 1.0\r\n', |
96 'Content-Length: 10\r\n', |
97 'Content-Length: 10\r\n', |
122 self.assertEqual(resp.status, 200) |
123 self.assertEqual(resp.status, 200) |
123 self.assertEqual('1234567890', resp.read()) |
124 self.assertEqual('1234567890', resp.read()) |
124 self.assertEqual(['BogusServer 1.0'], |
125 self.assertEqual(['BogusServer 1.0'], |
125 resp.headers.getheaders('server')) |
126 resp.headers.getheaders('server')) |
126 |
127 |
127 def testSSLProxyFailure(self): |
128 def testSSLRequestNoConnectBody(self): |
128 con = http.HTTPConnection('1.2.3.4:443', |
129 con = httpplus.HTTPConnection('1.2.3.4:443', |
129 proxy_hostport=('magicproxy', 4242)) |
130 proxy_hostport=('magicproxy', 4242)) |
130 socket.socket = make_preloaded_socket( |
131 socket.socket = make_preloaded_socket( |
131 ['HTTP/1.1 407 Proxy Authentication Required\r\n\r\n']) |
132 ['HTTP/1.1 200 OK\r\n', |
132 self.assertRaises(http.HTTPProxyConnectFailedException, con._connect) |
133 'Server: BogusServer 1.0\r\n', |
133 self.assertRaises(http.HTTPProxyConnectFailedException, |
134 '\r\n']) |
|
135 con._connect() |
|
136 con.sock.data = ['HTTP/1.1 200 OK\r\n', |
|
137 'Server: BogusServer 1.0\r\n', |
|
138 'Content-Length: 10\r\n', |
|
139 '\r\n' |
|
140 '1234567890' |
|
141 ] |
|
142 connect_sent = con.sock.sent |
|
143 con.sock.sent = '' |
|
144 con.request('GET', '/') |
|
145 |
|
146 expected_connect = ('CONNECT 1.2.3.4:443 HTTP/1.0\r\n' |
|
147 'Host: 1.2.3.4\r\n' |
|
148 'accept-encoding: identity\r\n' |
|
149 '\r\n') |
|
150 expected_request = ('GET / HTTP/1.1\r\n' |
|
151 'Host: 1.2.3.4\r\n' |
|
152 'accept-encoding: identity\r\n\r\n') |
|
153 |
|
154 self.assertEqual(('127.0.0.42', 4242), con.sock.sa) |
|
155 self.assertStringEqual(expected_connect, connect_sent) |
|
156 self.assertStringEqual(expected_request, con.sock.sent) |
|
157 resp = con.getresponse() |
|
158 self.assertEqual(resp.status, 200) |
|
159 self.assertEqual('1234567890', resp.read()) |
|
160 self.assertEqual(['BogusServer 1.0'], |
|
161 resp.headers.getheaders('server')) |
|
162 |
|
163 def testSSLProxyFailure(self): |
|
164 con = httpplus.HTTPConnection('1.2.3.4:443', |
|
165 proxy_hostport=('magicproxy', 4242)) |
|
166 socket.socket = make_preloaded_socket( |
|
167 ['HTTP/1.1 407 Proxy Authentication Required\r\n\r\n'], close=True) |
|
168 self.assertRaises(httpplus.HTTPProxyConnectFailedException, con._connect) |
|
169 self.assertRaises(httpplus.HTTPProxyConnectFailedException, |
134 con.request, 'GET', '/') |
170 con.request, 'GET', '/') |
135 # no-check-code |
171 # no-check-code |