示例#1
0
 def test_100_continue(self):
     # Run through a 100-continue interaction by hand:
     # When given Expect: 100-continue, we get a 100 response after the
     # headers, and then the real response after the body.
     stream = IOStream(socket.socket(), io_loop=self.io_loop)
     stream.connect(("127.0.0.1", self.get_http_port()), callback=self.stop)
     self.wait()
     stream.write(b"\r\n".join([
         b"POST /hello HTTP/1.1", b"Content-Length: 1024",
         b"Expect: 100-continue", b"Connection: close", b"\r\n"
     ]),
                  callback=self.stop)
     self.wait()
     stream.read_until(b"\r\n\r\n", self.stop)
     data = self.wait()
     self.assertTrue(data.startswith(b"HTTP/1.1 100 "), data)
     stream.write(b"a" * 1024)
     stream.read_until(b"\r\n", self.stop)
     first_line = self.wait()
     self.assertTrue(first_line.startswith(b"HTTP/1.1 200"), first_line)
     stream.read_until(b"\r\n\r\n", self.stop)
     header_data = self.wait()
     headers = HTTPHeaders.parse(native_str(header_data.decode('latin1')))
     stream.read_bytes(int(headers["Content-Length"]), self.stop)
     body = self.wait()
     self.assertEqual(body, b"Got 1024 bytes in POST")
     stream.close()
示例#2
0
 def test_string(self):
     headers = HTTPHeaders()
     headers.add("Foo", "1")
     headers.add("Foo", "2")
     headers.add("Foo", "3")
     headers2 = HTTPHeaders.parse(str(headers))
     self.assertEquals(headers, headers2)
示例#3
0
 def read_headers(self):
     self.stream.read_until(b'\r\n', self.stop)
     first_line = self.wait()
     self.assertTrue(first_line.startswith(b'HTTP/1.1 200'), first_line)
     self.stream.read_until(b'\r\n\r\n', self.stop)
     header_bytes = self.wait()
     headers = HTTPHeaders.parse(header_bytes.decode('latin1'))
     return headers
示例#4
0
 def test_unix_socket(self):
     self.stream.write(b"GET /hello HTTP/1.0\r\n\r\n")
     self.stream.read_until(b"\r\n", self.stop)
     response = self.wait()
     self.assertEqual(response, b"HTTP/1.1 200 OK\r\n")
     self.stream.read_until(b"\r\n\r\n", self.stop)
     headers = HTTPHeaders.parse(self.wait().decode('latin1'))
     self.stream.read_bytes(int(headers["Content-Length"]), self.stop)
     body = self.wait()
     self.assertEqual(body, b"Hello world")
示例#5
0
 def test_optional_cr(self):
     # Both CRLF and LF should be accepted as separators. CR should not be
     # part of the data when followed by LF, but it is a normal char
     # otherwise (or should bare CR be an error?)
     headers = HTTPHeaders.parse(
         'CRLF: crlf\r\nLF: lf\nCR: cr\rMore: more\r\n')
     self.assertEqual(sorted(headers.get_all()),
                      [('Cr', 'cr\rMore: more'),
                       ('Crlf', 'crlf'),
                       ('Lf', 'lf'),
                       ])
示例#6
0
 def test_future_interface(self):
     """Basic test of IOStream's ability to return Futures."""
     stream = self._make_client_iostream()
     connect_result = yield stream.connect(
         ("127.0.0.1", self.get_http_port()))
     self.assertIs(connect_result, stream)
     yield stream.write(b"GET / HTTP/1.0\r\n\r\n")
     first_line = yield stream.read_until(b"\r\n")
     self.assertEqual(first_line, b"HTTP/1.1 200 OK\r\n")
     # callback=None is equivalent to no callback.
     header_data = yield stream.read_until(b"\r\n\r\n", callback=None)
     headers = HTTPHeaders.parse(header_data.decode('latin1'))
     content_length = int(headers['Content-Length'])
     body = yield stream.read_bytes(content_length)
     self.assertEqual(body, b'Hello')
     stream.close()
示例#7
0
 def test_unicode_newlines(self):
     # Ensure that only \r\n is recognized as a header separator, and not
     # the other newline-like unicode characters.
     # Characters that are likely to be problematic can be found in
     # http://unicode.org/standard/reports/tr13/tr13-5.html
     # and cpython's unicodeobject.c (which defines the implementation
     # of unicode_type.splitlines(), and uses a different list than TR13).
     newlines = [
         u'\u001b',  # VERTICAL TAB
         u'\u001c',  # FILE SEPARATOR
         u'\u001d',  # GROUP SEPARATOR
         u'\u001e',  # RECORD SEPARATOR
         u'\u0085',  # NEXT LINE
         u'\u2028',  # LINE SEPARATOR
         u'\u2029',  # PARAGRAPH SEPARATOR
     ]
     for newline in newlines:
         # Try the utf8 and latin1 representations of each newline
         for encoding in ['utf8', 'latin1']:
             try:
                 try:
                     encoded = newline.encode(encoding)
                 except UnicodeEncodeError:
                     # Some chars cannot be represented in latin1
                     continue
                 data = b'Cookie: foo=' + encoded + b'bar'
                 # parse() wants a native_str, so decode through latin1
                 # in the same way the real parser does.
                 headers = HTTPHeaders.parse(
                     native_str(data.decode('latin1')))
                 expected = [('Cookie', 'foo=' +
                              native_str(encoded.decode('latin1')) + 'bar')]
                 self.assertEqual(
                     expected, list(headers.get_all()))
             except Exception:
                 gen_log.warning("failed while trying %r in %s",
                                 newline, encoding)
                 raise
示例#8
0
    def test_multi_line(self):
        # Lines beginning with whitespace are appended to the previous line
        # with any leading whitespace replaced by a single space.
        # Note that while multi-line headers are a part of the HTTP spec,
        # their use is strongly discouraged.
        data = """\
Foo: bar
 baz
Asdf: qwer
\tzxcv
Foo: even
     more
     lines
""".replace("\n", "\r\n")
        headers = HTTPHeaders.parse(data)
        self.assertEqual(headers["asdf"], "qwer zxcv")
        self.assertEqual(headers.get_list("asdf"), ["qwer zxcv"])
        self.assertEqual(headers["Foo"], "bar baz,even more lines")
        self.assertEqual(headers.get_list("foo"), ["bar baz", "even more lines"])
        self.assertEqual(sorted(list(headers.get_all())),
                         [("Asdf", "qwer zxcv"),
                          ("Foo", "bar baz"),
                          ("Foo", "even more lines")])