Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(339)

Delta Between Two Patch Sets: Lib/test/test_httplib.py

Issue 7776: http.client.HTTPConnection tunneling is broken
Left Patch Set: Created 6 years ago
Right Patch Set: Created 5 years, 11 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/http/client.py ('k') | Misc/NEWS » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 import errno 1 import errno
2 from http import client 2 from http import client
3 import io 3 import io
4 import os 4 import os
5 import array 5 import array
6 import socket 6 import socket
7 7
8 import unittest 8 import unittest
9 TestCase = unittest.TestCase 9 TestCase = unittest.TestCase
10 10
(...skipping 12 matching lines...) Expand all
23 class FakeSocket: 23 class FakeSocket:
24 def __init__(self, text, fileclass=io.BytesIO, host=None, port=None): 24 def __init__(self, text, fileclass=io.BytesIO, host=None, port=None):
25 if isinstance(text, str): 25 if isinstance(text, str):
26 text = text.encode("ascii") 26 text = text.encode("ascii")
27 self.text = text 27 self.text = text
28 self.fileclass = fileclass 28 self.fileclass = fileclass
29 self.data = b'' 29 self.data = b''
30 self.sendall_calls = 0 30 self.sendall_calls = 0
31 self.host = host 31 self.host = host
32 self.port = port 32 self.port = port
33 33
34 def sendall(self, data): 34 def sendall(self, data):
35 self.sendall_calls += 1 35 self.sendall_calls += 1
36 self.data += data 36 self.data += data
37 37
38 def makefile(self, mode, bufsize=None): 38 def makefile(self, mode, bufsize=None):
39 if mode != 'r' and mode != 'rb': 39 if mode != 'r' and mode != 'rb':
40 raise client.UnimplementedFileMode() 40 raise client.UnimplementedFileMode()
41 return self.fileclass(self.text) 41 return self.fileclass(self.text)
42 42
43 def close(self): 43 def close(self):
44 pass 44 pass
45 45
46 class EPipeSocket(FakeSocket): 46 class EPipeSocket(FakeSocket):
47 47
48 def __init__(self, text, pipe_trigger): 48 def __init__(self, text, pipe_trigger):
49 # When sendall() is called with pipe_trigger, raise EPIPE. 49 # When sendall() is called with pipe_trigger, raise EPIPE.
50 FakeSocket.__init__(self, text) 50 FakeSocket.__init__(self, text)
51 self.pipe_trigger = pipe_trigger 51 self.pipe_trigger = pipe_trigger
52 52
53 def sendall(self, data): 53 def sendall(self, data):
54 if self.pipe_trigger in data: 54 if self.pipe_trigger in data:
55 raise OSError(errno.EPIPE, "gotcha") 55 raise OSError(errno.EPIPE, "gotcha")
(...skipping 913 matching lines...) Expand 10 before | Expand all | Expand 10 after
969 969
970 def test_getting_nonexistent_header_without_default(self): 970 def test_getting_nonexistent_header_without_default(self):
971 header = self.resp.getheader('No-Such-Header') 971 header = self.resp.getheader('No-Such-Header')
972 self.assertEqual(header, None) 972 self.assertEqual(header, None)
973 973
974 def test_getting_header_defaultint(self): 974 def test_getting_header_defaultint(self):
975 header = self.resp.getheader('No-Such-Header',default=42) 975 header = self.resp.getheader('No-Such-Header',default=42)
976 self.assertEqual(header, 42) 976 self.assertEqual(header, 42)
977 977
978 class TunnelTests(TestCase): 978 class TunnelTests(TestCase):
979 979
980 def test_connect(self): 980 def test_connect(self):
981 response_text = ( 981 response_text = (
982 'HTTP/1.0 200 OK\r\n\r\n' # Reply to CONNECT 982 'HTTP/1.0 200 OK\r\n\r\n' # Reply to CONNECT
983 'HTTP/1.1 200 OK\r\n' # Reply to HEAD 983 'HTTP/1.1 200 OK\r\n' # Reply to HEAD
984 'Content-Length: 42\r\n\r\n' 984 'Content-Length: 42\r\n\r\n'
985 ) 985 )
986 986
987 def create_connection(address, timeout=None, source_address=None): 987 def create_connection(address, timeout=None, source_address=None):
988 return FakeSocket(response_text, host=address[0], 988 return FakeSocket(response_text, host=address[0],
989 port=address[1]) 989 port=address[1])
990 990
991 conn = client.HTTPConnection('proxy.com') 991 conn = client.HTTPConnection('proxy.com')
992 conn._create_connection = create_connection 992 conn._create_connection = create_connection
993
994 # Once connected, we shouldn't be able to tunnel anymore
995 conn.connect()
996 self.assertRaises(RuntimeError, conn.set_tunnel,
997 'destination.com')
998
999 # But if we close the connection, we're good
1000 conn.close()
993 conn.set_tunnel('destination.com') 1001 conn.set_tunnel('destination.com')
994 conn.request('HEAD', '/', '') 1002 conn.request('HEAD', '/', '')
995 1003
996 self.assertEqual(conn.sock.host, 'proxy.com') 1004 self.assertEqual(conn.sock.host, 'proxy.com')
997 self.assertEqual(conn.sock.port, 80) 1005 self.assertEqual(conn.sock.port, 80)
998 self.assertTrue(b'CONNECT destination.com' in conn.sock.data) 1006 self.assertTrue(b'CONNECT destination.com' in conn.sock.data)
999 self.assertTrue(b'Host: destination.com' in conn.sock.data) 1007 self.assertTrue(b'Host: destination.com' in conn.sock.data)
1000 1008
1001 # This test should be removed if CONNECT ever gets 1009 # This test should be removed if CONNECT ever gets
1002 # HTTP/1.1 blessing 1010 # HTTP/1.1 blessing
1003 self.assertTrue(b'Host: proxy.com' not in conn.sock.data) 1011 self.assertTrue(b'Host: proxy.com' not in conn.sock.data)
1004 1012
1005 conn.close() 1013 conn.close()
1006 conn.request('PUT', '/', '') 1014 conn.request('PUT', '/', '')
1007 self.assertEqual(conn.sock.host, 'proxy.com') 1015 self.assertEqual(conn.sock.host, 'proxy.com')
1008 self.assertEqual(conn.sock.port, 80) 1016 self.assertEqual(conn.sock.port, 80)
1009 self.assertTrue(b'CONNECT destination.com' in conn.sock.data) 1017 self.assertTrue(b'CONNECT destination.com' in conn.sock.data)
1010 self.assertTrue(b'Host: destination.com' in conn.sock.data) 1018 self.assertTrue(b'Host: destination.com' in conn.sock.data)
1011 1019
1012
1013 def test_main(verbose=None): 1020 def test_main(verbose=None):
1014 support.run_unittest(HeaderTests, OfflineTest, BasicTest, TimeoutTest, 1021 support.run_unittest(HeaderTests, OfflineTest, BasicTest, TimeoutTest,
1015 HTTPSTest, RequestBodyTest, SourceAddressTest, 1022 HTTPSTest, RequestBodyTest, SourceAddressTest,
1016 HTTPResponseTest, TunnelTests) 1023 HTTPResponseTest, TunnelTests)
1017 1024
1018 if __name__ == '__main__': 1025 if __name__ == '__main__':
1019 test_main() 1026 test_main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+