1 # Copyright 2010, Google Inc. |
|
2 # All rights reserved. |
|
3 # |
|
4 # Redistribution and use in source and binary forms, with or without |
|
5 # modification, are permitted provided that the following conditions are |
|
6 # met: |
|
7 # |
|
8 # * Redistributions of source code must retain the above copyright |
|
9 # notice, this list of conditions and the following disclaimer. |
|
10 # * Redistributions in binary form must reproduce the above |
|
11 # copyright notice, this list of conditions and the following disclaimer |
|
12 # in the documentation and/or other materials provided with the |
|
13 # distribution. |
|
14 # * Neither the name of Google Inc. nor the names of its |
|
15 # contributors may be used to endorse or promote products derived from |
|
16 # this software without specific prior written permission. |
|
17 |
|
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
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. |
|
29 import unittest |
|
30 import socket |
|
31 |
|
32 import httpplus |
|
33 |
|
34 # relative import to ease embedding the library |
|
35 import util |
|
36 |
|
37 |
|
38 def make_preloaded_socket(data, close=False): |
|
39 """Make a socket pre-loaded with data so it can be read during connect. |
|
40 |
|
41 Useful for https proxy tests because we have to read from the |
|
42 socket during _connect rather than later on. |
|
43 """ |
|
44 def s(*args, **kwargs): |
|
45 sock = util.MockSocket(*args, **kwargs) |
|
46 sock.early_data = data[:] |
|
47 sock.close_on_empty = close |
|
48 return sock |
|
49 return s |
|
50 |
|
51 |
|
52 class ProxyHttpTest(util.HttpTestBase, unittest.TestCase): |
|
53 |
|
54 def _run_simple_test(self, host, server_data, expected_req, expected_data): |
|
55 con = httpplus.HTTPConnection(host) |
|
56 con._connect() |
|
57 con.sock.data = server_data |
|
58 con.request('GET', '/') |
|
59 |
|
60 self.assertEqual(expected_req, con.sock.sent) |
|
61 self.assertEqual(expected_data, con.getresponse().read()) |
|
62 |
|
63 def testSimpleRequest(self): |
|
64 con = httpplus.HTTPConnection('1.2.3.4:80', |
|
65 proxy_hostport=('magicproxy', 4242)) |
|
66 con._connect() |
|
67 con.sock.data = ['HTTP/1.1 200 OK\r\n', |
|
68 'Server: BogusServer 1.0\r\n', |
|
69 'MultiHeader: Value\r\n' |
|
70 'MultiHeader: Other Value\r\n' |
|
71 'MultiHeader: One More!\r\n' |
|
72 'Content-Length: 10\r\n', |
|
73 '\r\n' |
|
74 '1234567890' |
|
75 ] |
|
76 con.request('GET', '/') |
|
77 |
|
78 expected_req = ('GET http://1.2.3.4/ HTTP/1.1\r\n' |
|
79 'Host: 1.2.3.4\r\n' |
|
80 'accept-encoding: identity\r\n\r\n') |
|
81 |
|
82 self.assertEqual(('127.0.0.42', 4242), con.sock.sa) |
|
83 self.assertStringEqual(expected_req, con.sock.sent) |
|
84 resp = con.getresponse() |
|
85 self.assertEqual('1234567890', resp.read()) |
|
86 self.assertEqual(['Value', 'Other Value', 'One More!'], |
|
87 resp.headers.getheaders('multiheader')) |
|
88 self.assertEqual(['BogusServer 1.0'], |
|
89 resp.headers.getheaders('server')) |
|
90 |
|
91 def testSSLRequest(self): |
|
92 con = httpplus.HTTPConnection('1.2.3.4:443', |
|
93 proxy_hostport=('magicproxy', 4242)) |
|
94 socket.socket = make_preloaded_socket( |
|
95 ['HTTP/1.1 200 OK\r\n', |
|
96 'Server: BogusServer 1.0\r\n', |
|
97 'Content-Length: 10\r\n', |
|
98 '\r\n' |
|
99 '1234567890']) |
|
100 con._connect() |
|
101 con.sock.data = ['HTTP/1.1 200 OK\r\n', |
|
102 'Server: BogusServer 1.0\r\n', |
|
103 'Content-Length: 10\r\n', |
|
104 '\r\n' |
|
105 '1234567890' |
|
106 ] |
|
107 connect_sent = con.sock.sent |
|
108 con.sock.sent = '' |
|
109 con.request('GET', '/') |
|
110 |
|
111 expected_connect = ('CONNECT 1.2.3.4:443 HTTP/1.0\r\n' |
|
112 'Host: 1.2.3.4\r\n' |
|
113 'accept-encoding: identity\r\n' |
|
114 '\r\n') |
|
115 expected_request = ('GET / HTTP/1.1\r\n' |
|
116 'Host: 1.2.3.4\r\n' |
|
117 'accept-encoding: identity\r\n\r\n') |
|
118 |
|
119 self.assertEqual(('127.0.0.42', 4242), con.sock.sa) |
|
120 self.assertStringEqual(expected_connect, connect_sent) |
|
121 self.assertStringEqual(expected_request, con.sock.sent) |
|
122 resp = con.getresponse() |
|
123 self.assertEqual(resp.status, 200) |
|
124 self.assertEqual('1234567890', resp.read()) |
|
125 self.assertEqual(['BogusServer 1.0'], |
|
126 resp.headers.getheaders('server')) |
|
127 |
|
128 def testSSLRequestNoConnectBody(self): |
|
129 con = httpplus.HTTPConnection('1.2.3.4:443', |
|
130 proxy_hostport=('magicproxy', 4242)) |
|
131 socket.socket = make_preloaded_socket( |
|
132 ['HTTP/1.1 200 OK\r\n', |
|
133 'Server: BogusServer 1.0\r\n', |
|
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, |
|
170 con.request, 'GET', '/') |
|
171 # no-check-code |
|