Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7c1692d

Browse files
committed
Replace references to httplib with http.client.
1 parent fdd6dee commit 7c1692d

1 file changed

Lines changed: 27 additions & 27 deletions

File tree

Lib/test/test_httplib.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import http.client as httplib
1+
from http import client
22
import io
33
import socket
44

@@ -21,13 +21,13 @@ def sendall(self, data):
2121

2222
def makefile(self, mode, bufsize=None):
2323
if mode != 'r' and mode != 'rb':
24-
raise httplib.UnimplementedFileMode()
24+
raise client.UnimplementedFileMode()
2525
return self.fileclass(self.text)
2626

2727
class NoEOFStringIO(io.BytesIO):
2828
"""Like StringIO, but raises AssertionError on EOF.
2929
30-
This is used below to test that httplib doesn't try to read
30+
This is used below to test that http.client doesn't try to read
3131
more from the underlying file than it should.
3232
"""
3333
def read(self, n=-1):
@@ -61,7 +61,7 @@ def append(self, item):
6161

6262
for explicit_header in True, False:
6363
for header in 'Content-length', 'Host', 'Accept-encoding':
64-
conn = httplib.HTTPConnection('example.com')
64+
conn = client.HTTPConnection('example.com')
6565
conn.sock = FakeSocket('blahblahblah')
6666
conn._buffer = HeaderCountingBuffer()
6767

@@ -78,22 +78,22 @@ def test_status_lines(self):
7878

7979
body = "HTTP/1.1 200 Ok\r\n\r\nText"
8080
sock = FakeSocket(body)
81-
resp = httplib.HTTPResponse(sock)
81+
resp = client.HTTPResponse(sock)
8282
resp.begin()
8383
self.assertEqual(resp.read(), b"Text")
8484
self.assertTrue(resp.isclosed())
8585

8686
body = "HTTP/1.1 400.100 Not Ok\r\n\r\nText"
8787
sock = FakeSocket(body)
88-
resp = httplib.HTTPResponse(sock)
89-
self.assertRaises(httplib.BadStatusLine, resp.begin)
88+
resp = client.HTTPResponse(sock)
89+
self.assertRaises(client.BadStatusLine, resp.begin)
9090

9191
def test_partial_reads(self):
9292
# if we have a lenght, the system knows when to close itself
9393
# same behaviour than when we read the whole thing with read()
9494
body = "HTTP/1.1 200 Ok\r\nContent-Length: 4\r\n\r\nText"
9595
sock = FakeSocket(body)
96-
resp = httplib.HTTPResponse(sock)
96+
resp = client.HTTPResponse(sock)
9797
resp.begin()
9898
self.assertEqual(resp.read(2), b'Te')
9999
self.assertFalse(resp.isclosed())
@@ -104,14 +104,14 @@ def test_host_port(self):
104104
# Check invalid host_port
105105

106106
for hp in ("www.python.org:abc", "www.python.org:"):
107-
self.assertRaises(httplib.InvalidURL, httplib.HTTPConnection, hp)
107+
self.assertRaises(client.InvalidURL, client.HTTPConnection, hp)
108108

109109
for hp, h, p in (("[fe80::207:e9ff:fe9b]:8000",
110110
"fe80::207:e9ff:fe9b", 8000),
111111
("www.python.org:80", "www.python.org", 80),
112112
("www.python.org", "www.python.org", 80),
113113
("[fe80::207:e9ff:fe9b]", "fe80::207:e9ff:fe9b", 80)):
114-
c = httplib.HTTPConnection(hp)
114+
c = client.HTTPConnection(hp)
115115
self.assertEqual(h, c.host)
116116
self.assertEqual(p, c.port)
117117

@@ -128,7 +128,7 @@ def test_response_headers(self):
128128
', '
129129
'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"')
130130
s = FakeSocket(text)
131-
r = httplib.HTTPResponse(s)
131+
r = client.HTTPResponse(s)
132132
r.begin()
133133
cookies = r.getheader("Set-Cookie")
134134
self.assertEqual(cookies, hdr)
@@ -141,7 +141,7 @@ def test_read_head(self):
141141
'Content-Length: 14432\r\n'
142142
'\r\n',
143143
NoEOFStringIO)
144-
resp = httplib.HTTPResponse(sock, method="HEAD")
144+
resp = client.HTTPResponse(sock, method="HEAD")
145145
resp.begin()
146146
if resp.read():
147147
self.fail("Did not expect response from HEAD request")
@@ -151,7 +151,7 @@ def test_send_file(self):
151151
b'Accept-Encoding: identity\r\nContent-Length:')
152152

153153
body = open(__file__, 'rb')
154-
conn = httplib.HTTPConnection('example.com')
154+
conn = client.HTTPConnection('example.com')
155155
sock = FakeSocket(body)
156156
conn.sock = sock
157157
conn.request('GET', '/foo', body)
@@ -168,18 +168,18 @@ def test_chunked(self):
168168
'd\r\n'
169169
)
170170
sock = FakeSocket(chunked_start + '0\r\n')
171-
resp = httplib.HTTPResponse(sock, method="GET")
171+
resp = client.HTTPResponse(sock, method="GET")
172172
resp.begin()
173173
self.assertEquals(resp.read(), b'hello world')
174174
resp.close()
175175

176176
for x in ('', 'foo\r\n'):
177177
sock = FakeSocket(chunked_start + x)
178-
resp = httplib.HTTPResponse(sock, method="GET")
178+
resp = client.HTTPResponse(sock, method="GET")
179179
resp.begin()
180180
try:
181181
resp.read()
182-
except httplib.IncompleteRead as i:
182+
except client.IncompleteRead as i:
183183
self.assertEquals(i.partial, b'hello world')
184184
self.assertEqual(repr(i),'IncompleteRead(11 bytes read)')
185185
self.assertEqual(str(i),'IncompleteRead(11 bytes read)')
@@ -191,18 +191,18 @@ def test_chunked(self):
191191
def test_negative_content_length(self):
192192
sock = FakeSocket(
193193
'HTTP/1.1 200 OK\r\nContent-Length: -1\r\n\r\nHello\r\n')
194-
resp = httplib.HTTPResponse(sock, method="GET")
194+
resp = client.HTTPResponse(sock, method="GET")
195195
resp.begin()
196196
self.assertEquals(resp.read(), b'Hello\r\n')
197197
resp.close()
198198

199199
def test_incomplete_read(self):
200200
sock = FakeSocket('HTTP/1.1 200 OK\r\nContent-Length: 10\r\n\r\nHello\r\n')
201-
resp = httplib.HTTPResponse(sock, method="GET")
201+
resp = client.HTTPResponse(sock, method="GET")
202202
resp.begin()
203203
try:
204204
resp.read()
205-
except httplib.IncompleteRead as i:
205+
except client.IncompleteRead as i:
206206
self.assertEquals(i.partial, b'Hello\r\n')
207207
self.assertEqual(repr(i),
208208
"IncompleteRead(7 bytes read, 3 more expected)")
@@ -216,7 +216,7 @@ def test_incomplete_read(self):
216216

217217
class OfflineTest(TestCase):
218218
def test_responses(self):
219-
self.assertEquals(httplib.responses[httplib.NOT_FOUND], "Not Found")
219+
self.assertEquals(client.responses[client.NOT_FOUND], "Not Found")
220220

221221
class TimeoutTest(TestCase):
222222
PORT = None
@@ -238,7 +238,7 @@ def testTimeoutAttribute(self):
238238
self.assert_(socket.getdefaulttimeout() is None)
239239
socket.setdefaulttimeout(30)
240240
try:
241-
httpConn = httplib.HTTPConnection(HOST, TimeoutTest.PORT)
241+
httpConn = client.HTTPConnection(HOST, TimeoutTest.PORT)
242242
httpConn.connect()
243243
finally:
244244
socket.setdefaulttimeout(None)
@@ -249,7 +249,7 @@ def testTimeoutAttribute(self):
249249
self.assert_(socket.getdefaulttimeout() is None)
250250
socket.setdefaulttimeout(30)
251251
try:
252-
httpConn = httplib.HTTPConnection(HOST, TimeoutTest.PORT,
252+
httpConn = client.HTTPConnection(HOST, TimeoutTest.PORT,
253253
timeout=None)
254254
httpConn.connect()
255255
finally:
@@ -258,7 +258,7 @@ def testTimeoutAttribute(self):
258258
httpConn.close()
259259

260260
# a value
261-
httpConn = httplib.HTTPConnection(HOST, TimeoutTest.PORT, timeout=30)
261+
httpConn = client.HTTPConnection(HOST, TimeoutTest.PORT, timeout=30)
262262
httpConn.connect()
263263
self.assertEqual(httpConn.sock.gettimeout(), 30)
264264
httpConn.close()
@@ -268,22 +268,22 @@ class HTTPSTimeoutTest(TestCase):
268268

269269
def test_attributes(self):
270270
# simple test to check it's storing it
271-
if hasattr(httplib, 'HTTPSConnection'):
272-
h = httplib.HTTPSConnection(HOST, TimeoutTest.PORT, timeout=30)
271+
if hasattr(client, 'HTTPSConnection'):
272+
h = client.HTTPSConnection(HOST, TimeoutTest.PORT, timeout=30)
273273
self.assertEqual(h.timeout, 30)
274274

275275
class RequestBodyTest(TestCase):
276276
"""Test cases where a request includes a message body."""
277277

278278
def setUp(self):
279-
self.conn = httplib.HTTPConnection('example.com')
279+
self.conn = client.HTTPConnection('example.com')
280280
self.sock = FakeSocket("")
281281
self.conn.sock = self.sock
282282

283283
def get_headers_and_fp(self):
284284
f = io.BytesIO(self.sock.data)
285285
f.readline() # read the request line
286-
message = httplib.parse_headers(f)
286+
message = client.parse_headers(f)
287287
return message, f
288288

289289
def test_manual_content_length(self):

0 commit comments

Comments
 (0)