Example #1
0
    def test_chunked (self):
        chunked_start = (
            'HTTP/1.1 200 OK\r\n'
            'Transfer-Encoding: chunked\r\n\r\n'
            'a\r\n'
            'hello worl\r\n'
            '1\r\n'
            'd\r\n'
        )
        sock = FakeSocket(chunked_start + '0\r\n')
        resp = httplib.HTTPResponse(sock, method = "GET")
        resp.begin()
        self.assertEqual(resp.read(), 'hello world')
        resp.close()

        for x in ('', 'foo\r\n'):
            sock = FakeSocket(chunked_start + x)
            resp = httplib.HTTPResponse(sock, method = "GET")
            resp.begin()
            try:
                resp.read()
            except httplib.IncompleteRead, i:
                self.assertEqual(i.partial, 'hello world')
                self.assertEqual(repr(i), 'IncompleteRead(11 bytes read)')
                self.assertEqual(str(i), 'IncompleteRead(11 bytes read)')
            else:
                self.fail('IncompleteRead expected')
            finally:
Example #2
0
    def test_status_lines (self):
        # Test HTTP status lines

        body = "HTTP/1.1 200 Ok\r\n\r\nText"
        sock = FakeSocket(body)
        resp = httplib.HTTPResponse(sock)
        resp.begin()
        self.assertEqual(resp.read(), 'Text')
        self.assertTrue(resp.isclosed())

        body = "HTTP/1.1 400.100 Not Ok\r\n\r\nText"
        sock = FakeSocket(body)
        resp = httplib.HTTPResponse(sock)
        self.assertRaises(httplib.BadStatusLine, resp.begin)
Example #3
0
 def test_overflowing_header_line (self):
     body = (
         'HTTP/1.1 200 OK\r\n'
         'X-Foo: bar' + 'r' * 65536 + '\r\n\r\n'
     )
     resp = httplib.HTTPResponse(FakeSocket(body))
     self.assertRaises(httplib.LineTooLong, resp.begin)
Example #4
0
 def test_filenoattr (self):
     # Just test the fileno attribute in the HTTPResponse Object.
     body = "HTTP/1.1 200 Ok\r\n\r\nText"
     sock = FakeSocket(body)
     resp = httplib.HTTPResponse(sock)
     self.assertTrue(hasattr(resp, 'fileno'),
                     'HTTPResponse should expose a fileno attribute')
Example #5
0
 def test_negative_content_length (self):
     sock = FakeSocket('HTTP/1.1 200 OK\r\n'
                       'Content-Length: -1\r\n\r\nHello\r\n')
     resp = httplib.HTTPResponse(sock, method = "GET")
     resp.begin()
     self.assertEqual(resp.read(), 'Hello\r\n')
     resp.close()
Example #6
0
 def test_overflowing_chunked_line (self):
     body = (
         'HTTP/1.1 200 OK\r\n'
         'Transfer-Encoding: chunked\r\n\r\n'
         + '0' * 65536 + 'a\r\n'
                         'hello world\r\n'
                         '0\r\n'
     )
     resp = httplib.HTTPResponse(FakeSocket(body))
     resp.begin()
     self.assertRaises(httplib.LineTooLong, resp.read)
Example #7
0
 def test_partial_reads (self):
     # if we have a lenght, the system knows when to close itself
     # same behaviour than when we read the whole thing with read()
     body = "HTTP/1.1 200 Ok\r\nContent-Length: 4\r\n\r\nText"
     sock = FakeSocket(body)
     resp = httplib.HTTPResponse(sock)
     resp.begin()
     self.assertEqual(resp.read(2), 'Te')
     self.assertFalse(resp.isclosed())
     self.assertEqual(resp.read(2), 'xt')
     self.assertTrue(resp.isclosed())
Example #8
0
 def test_read_head (self):
     # Test that the library doesn't attempt to read any data
     # from a HEAD request.  (Tickles SF bug #622042.)
     sock = FakeSocket(
         'HTTP/1.1 200 OK\r\n'
         'Content-Length: 14432\r\n'
         '\r\n',
         NoEOFStringIO)
     resp = httplib.HTTPResponse(sock, method = "HEAD")
     resp.begin()
     if resp.read() != "":
         self.fail("Did not expect response from HEAD request")
Example #9
0
 def test_incomplete_read (self):
     sock = FakeSocket('HTTP/1.1 200 OK\r\nContent-Length: 10\r\n\r\nHello\r\n')
     resp = httplib.HTTPResponse(sock, method = "GET")
     resp.begin()
     try:
         resp.read()
     except httplib.IncompleteRead as i:
         self.assertEqual(i.partial, 'Hello\r\n')
         self.assertEqual(repr(i),
                          "IncompleteRead(7 bytes read, 3 more expected)")
         self.assertEqual(str(i),
                          "IncompleteRead(7 bytes read, 3 more expected)")
     else:
         self.fail('IncompleteRead expected')
     finally:
         resp.close()
Example #10
0
 def test_chunked_head (self):
     chunked_start = (
         'HTTP/1.1 200 OK\r\n'
         'Transfer-Encoding: chunked\r\n\r\n'
         'a\r\n'
         'hello world\r\n'
         '1\r\n'
         'd\r\n'
     )
     sock = FakeSocket(chunked_start + '0\r\n')
     resp = httplib.HTTPResponse(sock, method = "HEAD")
     resp.begin()
     self.assertEqual(resp.read(), '')
     self.assertEqual(resp.status, 200)
     self.assertEqual(resp.reason, 'OK')
     self.assertTrue(resp.isclosed())
Example #11
0
 def test_response_headers (self):
     # test response with multiple message headers with the same field name.
     text = ('HTTP/1.1 200 OK\r\n'
             'Set-Cookie: Customer="WILE_E_COYOTE";'
             ' Version="1"; Path="/acme"\r\n'
             'Set-Cookie: Part_Number="Rocket_Launcher_0001"; Version="1";'
             ' Path="/acme"\r\n'
             '\r\n'
             'No body\r\n')
     hdr = ('Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"'
            ', '
            'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"')
     s = FakeSocket(text)
     r = httplib.HTTPResponse(s)
     r.begin()
     cookies = r.getheader("Set-Cookie")
     if cookies != hdr:
         self.fail("multiple headers not combined properly")
Example #12
0
 def test_overflowing_status_line (self):
     self.skipTest("disabled for HTTP 0.9 support")
     body = "HTTP/1.1 200 Ok" + "k" * 65536 + "\r\n"
     resp = httplib.HTTPResponse(FakeSocket(body))
     self.assertRaises((httplib.LineTooLong, httplib.BadStatusLine), resp.begin)