Back to index

python3.2  3.2.2
Public Member Functions
test.test_httplib.BasicTest Class Reference
Inheritance diagram for test.test_httplib.BasicTest:
Inheritance graph
[legend]
Collaboration diagram for test.test_httplib.BasicTest:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def test_status_lines
def test_bad_status_repr
def test_partial_reads
def test_host_port
def test_response_headers
def test_read_head
def test_send_file
def test_send
def test_send_iter
def test_send_type_error
def test_chunked
def test_chunked_head
def test_negative_content_length
def test_incomplete_read
def test_epipe
def test_overflowing_status_line
def test_overflowing_header_line
def test_overflowing_chunked_line

Detailed Description

Definition at line 129 of file test_httplib.py.


Member Function Documentation

Definition at line 145 of file test_httplib.py.

00145 
00146     def test_bad_status_repr(self):
00147         exc = client.BadStatusLine('')
00148         self.assertEqual(repr(exc), '''BadStatusLine("\'\'",)''')

Here is the call graph for this function:

Definition at line 256 of file test_httplib.py.

00256 
00257     def test_chunked(self):
00258         chunked_start = (
00259             'HTTP/1.1 200 OK\r\n'
00260             'Transfer-Encoding: chunked\r\n\r\n'
00261             'a\r\n'
00262             'hello worl\r\n'
00263             '1\r\n'
00264             'd\r\n'
00265         )
00266         sock = FakeSocket(chunked_start + '0\r\n')
00267         resp = client.HTTPResponse(sock, method="GET")
00268         resp.begin()
00269         self.assertEqual(resp.read(), b'hello world')
00270         resp.close()
00271 
00272         for x in ('', 'foo\r\n'):
00273             sock = FakeSocket(chunked_start + x)
00274             resp = client.HTTPResponse(sock, method="GET")
00275             resp.begin()
00276             try:
00277                 resp.read()
00278             except client.IncompleteRead as i:
00279                 self.assertEqual(i.partial, b'hello world')
00280                 self.assertEqual(repr(i),'IncompleteRead(11 bytes read)')
00281                 self.assertEqual(str(i),'IncompleteRead(11 bytes read)')
00282             else:
00283                 self.fail('IncompleteRead expected')
00284             finally:
00285                 resp.close()

Here is the call graph for this function:

Definition at line 286 of file test_httplib.py.

00286 
00287     def test_chunked_head(self):
00288         chunked_start = (
00289             'HTTP/1.1 200 OK\r\n'
00290             'Transfer-Encoding: chunked\r\n\r\n'
00291             'a\r\n'
00292             'hello world\r\n'
00293             '1\r\n'
00294             'd\r\n'
00295         )
00296         sock = FakeSocket(chunked_start + '0\r\n')
00297         resp = client.HTTPResponse(sock, method="HEAD")
00298         resp.begin()
00299         self.assertEqual(resp.read(), b'')
00300         self.assertEqual(resp.status, 200)
00301         self.assertEqual(resp.reason, 'OK')
00302         self.assertTrue(resp.isclosed())

Here is the call graph for this function:

Definition at line 328 of file test_httplib.py.

00328 
00329     def test_epipe(self):
00330         sock = EPipeSocket(
00331             "HTTP/1.0 401 Authorization Required\r\n"
00332             "Content-type: text/html\r\n"
00333             "WWW-Authenticate: Basic realm=\"example\"\r\n",
00334             b"Content-Length")
00335         conn = client.HTTPConnection("example.com")
00336         conn.sock = sock
00337         self.assertRaises(socket.error,
00338                           lambda: conn.request("PUT", "/url", "body"))
00339         resp = conn.getresponse()
00340         self.assertEqual(401, resp.status)
00341         self.assertEqual("Basic realm=\"example\"",
00342                          resp.getheader("www-authenticate"))

Here is the call graph for this function:

Definition at line 161 of file test_httplib.py.

00161 
00162     def test_host_port(self):
00163         # Check invalid host_port
00164 
00165         for hp in ("www.python.org:abc", "www.python.org:"):
00166             self.assertRaises(client.InvalidURL, client.HTTPConnection, hp)
00167 
00168         for hp, h, p in (("[fe80::207:e9ff:fe9b]:8000",
00169                           "fe80::207:e9ff:fe9b", 8000),
00170                          ("www.python.org:80", "www.python.org", 80),
00171                          ("www.python.org", "www.python.org", 80),
00172                          ("[fe80::207:e9ff:fe9b]", "fe80::207:e9ff:fe9b", 80)):
00173             c = client.HTTPConnection(hp)
00174             self.assertEqual(h, c.host)
00175             self.assertEqual(p, c.port)

Here is the call graph for this function:

Definition at line 311 of file test_httplib.py.

00311 
00312     def test_incomplete_read(self):
00313         sock = FakeSocket('HTTP/1.1 200 OK\r\nContent-Length: 10\r\n\r\nHello\r\n')
00314         resp = client.HTTPResponse(sock, method="GET")
00315         resp.begin()
00316         try:
00317             resp.read()
00318         except client.IncompleteRead as i:
00319             self.assertEqual(i.partial, b'Hello\r\n')
00320             self.assertEqual(repr(i),
00321                              "IncompleteRead(7 bytes read, 3 more expected)")
00322             self.assertEqual(str(i),
00323                              "IncompleteRead(7 bytes read, 3 more expected)")
00324         else:
00325             self.fail('IncompleteRead expected')
00326         finally:
00327             resp.close()

Here is the call graph for this function:

Definition at line 303 of file test_httplib.py.

00303 
00304     def test_negative_content_length(self):
00305         sock = FakeSocket(
00306             'HTTP/1.1 200 OK\r\nContent-Length: -1\r\n\r\nHello\r\n')
00307         resp = client.HTTPResponse(sock, method="GET")
00308         resp.begin()
00309         self.assertEqual(resp.read(), b'Hello\r\n')
00310         resp.close()

Here is the call graph for this function:

Definition at line 358 of file test_httplib.py.

00358 
00359     def test_overflowing_chunked_line(self):
00360         body = (
00361             'HTTP/1.1 200 OK\r\n'
00362             'Transfer-Encoding: chunked\r\n\r\n'
00363             + '0' * 65536 + 'a\r\n'
00364             'hello world\r\n'
00365             '0\r\n'
00366         )
00367         resp = client.HTTPResponse(FakeSocket(body))
00368         resp.begin()
00369         self.assertRaises(client.LineTooLong, resp.read)

Here is the call graph for this function:

Definition at line 350 of file test_httplib.py.

00350 
00351     def test_overflowing_header_line(self):
00352         body = (
00353             'HTTP/1.1 200 OK\r\n'
00354             'X-Foo: bar' + 'r' * 65536 + '\r\n\r\n'
00355         )
00356         resp = client.HTTPResponse(FakeSocket(body))
00357         self.assertRaises(client.LineTooLong, resp.begin)

Here is the call graph for this function:

Definition at line 345 of file test_httplib.py.

00345 
00346     def test_overflowing_status_line(self):
00347         body = "HTTP/1.1 200 Ok" + "k" * 65536 + "\r\n"
00348         resp = client.HTTPResponse(FakeSocket(body))
00349         self.assertRaises((client.LineTooLong, client.BadStatusLine), resp.begin)

Here is the call graph for this function:

Definition at line 149 of file test_httplib.py.

00149 
00150     def test_partial_reads(self):
00151         # if we have a lenght, the system knows when to close itself
00152         # same behaviour than when we read the whole thing with read()
00153         body = "HTTP/1.1 200 Ok\r\nContent-Length: 4\r\n\r\nText"
00154         sock = FakeSocket(body)
00155         resp = client.HTTPResponse(sock)
00156         resp.begin()
00157         self.assertEqual(resp.read(2), b'Te')
00158         self.assertFalse(resp.isclosed())
00159         self.assertEqual(resp.read(2), b'xt')
00160         self.assertTrue(resp.isclosed())

Here is the call graph for this function:

Definition at line 194 of file test_httplib.py.

00194 
00195     def test_read_head(self):
00196         # Test that the library doesn't attempt to read any data
00197         # from a HEAD request.  (Tickles SF bug #622042.)
00198         sock = FakeSocket(
00199             'HTTP/1.1 200 OK\r\n'
00200             'Content-Length: 14432\r\n'
00201             '\r\n',
00202             NoEOFStringIO)
00203         resp = client.HTTPResponse(sock, method="HEAD")
00204         resp.begin()
00205         if resp.read():
00206             self.fail("Did not expect response from HEAD request")

Here is the call graph for this function:

Definition at line 176 of file test_httplib.py.

00176 
00177     def test_response_headers(self):
00178         # test response with multiple message headers with the same field name.
00179         text = ('HTTP/1.1 200 OK\r\n'
00180                 'Set-Cookie: Customer="WILE_E_COYOTE"; '
00181                 'Version="1"; Path="/acme"\r\n'
00182                 'Set-Cookie: Part_Number="Rocket_Launcher_0001"; Version="1";'
00183                 ' Path="/acme"\r\n'
00184                 '\r\n'
00185                 'No body\r\n')
00186         hdr = ('Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"'
00187                ', '
00188                'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"')
00189         s = FakeSocket(text)
00190         r = client.HTTPResponse(s)
00191         r.begin()
00192         cookies = r.getheader("Set-Cookie")
00193         self.assertEqual(cookies, hdr)

Here is the call graph for this function:

Definition at line 219 of file test_httplib.py.

00219 
00220     def test_send(self):
00221         expected = b'this is a test this is only a test'
00222         conn = client.HTTPConnection('example.com')
00223         sock = FakeSocket(None)
00224         conn.sock = sock
00225         conn.send(expected)
00226         self.assertEqual(expected, sock.data)
00227         sock.data = b''
00228         conn.send(array.array('b', expected))
00229         self.assertEqual(expected, sock.data)
00230         sock.data = b''
00231         conn.send(io.BytesIO(expected))
00232         self.assertEqual(expected, sock.data)

Here is the call graph for this function:

Definition at line 207 of file test_httplib.py.

00207 
00208     def test_send_file(self):
00209         expected = (b'GET /foo HTTP/1.1\r\nHost: example.com\r\n'
00210                     b'Accept-Encoding: identity\r\nContent-Length:')
00211 
00212         with open(__file__, 'rb') as body:
00213             conn = client.HTTPConnection('example.com')
00214             sock = FakeSocket(body)
00215             conn.sock = sock
00216             conn.request('GET', '/foo', body)
00217             self.assertTrue(sock.data.startswith(expected), '%r != %r' %
00218                     (sock.data[:len(expected)], expected))

Here is the call graph for this function:

Definition at line 233 of file test_httplib.py.

00233 
00234     def test_send_iter(self):
00235         expected = b'GET /foo HTTP/1.1\r\nHost: example.com\r\n' \
00236                    b'Accept-Encoding: identity\r\nContent-Length: 11\r\n' \
00237                    b'\r\nonetwothree'
00238 
00239         def body():
00240             yield b"one"
00241             yield b"two"
00242             yield b"three"
00243 
00244         conn = client.HTTPConnection('example.com')
00245         sock = FakeSocket("")
00246         conn.sock = sock
00247         conn.request('GET', '/foo', body(), {'Content-Length': '11'})
00248         self.assertEqual(sock.data, expected)

Here is the call graph for this function:

Definition at line 249 of file test_httplib.py.

00249 
00250     def test_send_type_error(self):
00251         # See: Issue #12676
00252         conn = client.HTTPConnection('example.com')
00253         conn.sock = FakeSocket('')
00254         with self.assertRaises(TypeError):
00255             conn.request('POST', 'test', conn)

Here is the call graph for this function:

Definition at line 130 of file test_httplib.py.

00130 
00131     def test_status_lines(self):
00132         # Test HTTP status lines
00133 
00134         body = "HTTP/1.1 200 Ok\r\n\r\nText"
00135         sock = FakeSocket(body)
00136         resp = client.HTTPResponse(sock)
00137         resp.begin()
00138         self.assertEqual(resp.read(), b"Text")
00139         self.assertTrue(resp.isclosed())
00140 
00141         body = "HTTP/1.1 400.100 Not Ok\r\n\r\nText"
00142         sock = FakeSocket(body)
00143         resp = client.HTTPResponse(sock)
00144         self.assertRaises(client.BadStatusLine, resp.begin)

Here is the call graph for this function:


The documentation for this class was generated from the following file: