Mercurial > public > mercurial-scm > hg
comparison mercurial/httpclient/tests/test_proxy_support.py @ 14243:861f28212398
Import new http library as mercurial.httpclient.
This is revision a4229f13c374 of
http://py-nonblocking-http.googlecode.com/ with a no-check-code
comment added to the end of each file using `for fi in $(hg manifest |
grep mercurial/httpclient/) ; echo '# no-check-code' >> $fi`.
author | Augie Fackler <durin42@gmail.com> |
---|---|
date | Fri, 06 May 2011 09:57:55 -0500 |
parents | |
children | 5c3de67e7402 |
comparison
equal
deleted
inserted
replaced
14242:5ee1309f7edb | 14243:861f28212398 |
---|---|
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 http | |
33 | |
34 # relative import to ease embedding the library | |
35 import util | |
36 | |
37 | |
38 def make_preloaded_socket(data): | |
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.data = data[:] | |
47 return sock | |
48 return s | |
49 | |
50 | |
51 class ProxyHttpTest(util.HttpTestBase, unittest.TestCase): | |
52 | |
53 def _run_simple_test(self, host, server_data, expected_req, expected_data): | |
54 con = http.HTTPConnection(host) | |
55 con._connect() | |
56 con.sock.data = server_data | |
57 con.request('GET', '/') | |
58 | |
59 self.assertEqual(expected_req, con.sock.sent) | |
60 self.assertEqual(expected_data, con.getresponse().read()) | |
61 | |
62 def testSimpleRequest(self): | |
63 con = http.HTTPConnection('1.2.3.4:80', | |
64 proxy_hostport=('magicproxy', 4242)) | |
65 con._connect() | |
66 con.sock.data = ['HTTP/1.1 200 OK\r\n', | |
67 'Server: BogusServer 1.0\r\n', | |
68 'MultiHeader: Value\r\n' | |
69 'MultiHeader: Other Value\r\n' | |
70 'MultiHeader: One More!\r\n' | |
71 'Content-Length: 10\r\n', | |
72 '\r\n' | |
73 '1234567890' | |
74 ] | |
75 con.request('GET', '/') | |
76 | |
77 expected_req = ('GET http://1.2.3.4/ HTTP/1.1\r\n' | |
78 'Host: 1.2.3.4\r\n' | |
79 'accept-encoding: identity\r\n\r\n') | |
80 | |
81 self.assertEqual(('127.0.0.42', 4242), con.sock.sa) | |
82 self.assertStringEqual(expected_req, con.sock.sent) | |
83 resp = con.getresponse() | |
84 self.assertEqual('1234567890', resp.read()) | |
85 self.assertEqual(['Value', 'Other Value', 'One More!'], | |
86 resp.headers.getheaders('multiheader')) | |
87 self.assertEqual(['BogusServer 1.0'], | |
88 resp.headers.getheaders('server')) | |
89 | |
90 def testSSLRequest(self): | |
91 con = http.HTTPConnection('1.2.3.4:443', | |
92 proxy_hostport=('magicproxy', 4242)) | |
93 socket.socket = make_preloaded_socket( | |
94 ['HTTP/1.1 200 OK\r\n', | |
95 'Server: BogusServer 1.0\r\n', | |
96 'Content-Length: 10\r\n', | |
97 '\r\n' | |
98 '1234567890']) | |
99 con._connect() | |
100 con.sock.data = ['HTTP/1.1 200 OK\r\n', | |
101 'Server: BogusServer 1.0\r\n', | |
102 'Content-Length: 10\r\n', | |
103 '\r\n' | |
104 '1234567890' | |
105 ] | |
106 con.request('GET', '/') | |
107 | |
108 expected_req = ('CONNECT 1.2.3.4:443 HTTP/1.0\r\n' | |
109 'Host: 1.2.3.4\r\n' | |
110 'accept-encoding: identity\r\n' | |
111 '\r\n' | |
112 'GET / HTTP/1.1\r\n' | |
113 'Host: 1.2.3.4\r\n' | |
114 'accept-encoding: identity\r\n\r\n') | |
115 | |
116 self.assertEqual(('127.0.0.42', 4242), con.sock.sa) | |
117 self.assertStringEqual(expected_req, con.sock.sent) | |
118 resp = con.getresponse() | |
119 self.assertEqual(resp.status, 200) | |
120 self.assertEqual('1234567890', resp.read()) | |
121 self.assertEqual(['BogusServer 1.0'], | |
122 resp.headers.getheaders('server')) | |
123 | |
124 def testSSLProxyFailure(self): | |
125 con = http.HTTPConnection('1.2.3.4:443', | |
126 proxy_hostport=('magicproxy', 4242)) | |
127 socket.socket = make_preloaded_socket( | |
128 ['HTTP/1.1 407 Proxy Authentication Required\r\n\r\n']) | |
129 self.assertRaises(http.HTTPProxyConnectFailedException, con._connect) | |
130 self.assertRaises(http.HTTPProxyConnectFailedException, | |
131 con.request, 'GET', '/') | |
132 # no-check-code |