Esempio n. 1
0
 def test_with_body(self):
     bytes = HTTP2StateProtocol(self.c, is_server=True).assemble_response(http.Response(
         b"HTTP/2.0",
         200,
         b'',
         http.Headers(foo=b"bar"),
         b'foobar'
     ))
     assert len(bytes) == 2
     assert bytes[0] ==\
         codecs.decode('00000901040000000288408294e7838c767f', 'hex_codec')
     assert bytes[1] ==\
         codecs.decode('000006000100000002666f6f626172', 'hex_codec')
Esempio n. 2
0
    def test_absolute_form(self):
        c = tcp.TCPClient(("127.0.0.1", self.port))
        with c.connect():
            c.convert_to_tls()
            protocol = HTTP2StateProtocol(c, is_server=True)
            protocol.connection_preface_performed = True

            req = protocol.read_request(NotImplemented)

            assert req.first_line_format == "absolute"
            assert req.scheme == "http"
            assert req.host == "address"
            assert req.port == 22
Esempio n. 3
0
    def test_create_headers_multiple_frames(self):
        headers = http.Headers([(b':method', b'GET'), (b':path', b'/'),
                                (b':scheme', b'https'), (b'foo', b'bar'),
                                (b'server', b'version')])

        protocol = HTTP2StateProtocol(self.c)
        protocol.http2_settings[
            hyperframe.frame.SettingsFrame.MAX_FRAME_SIZE] = 8
        data = protocol._create_headers(headers, 1, end_stream=True)
        assert len(data) == 3
        assert data[0] == bytes.fromhex("000008010100000001828487408294e783")
        assert data[1] == bytes.fromhex("0000080900000000018c767f7685ee5b10")
        assert data[2] == bytes.fromhex("00000209040000000163d5")
Esempio n. 4
0
 def test_simple(self):
     data = HTTP2StateProtocol(self.c, is_server=True).assemble_response(
         http.Response(
             http_version=b"HTTP/2.0",
             status_code=200,
             reason=b"",
             headers=(),
             content=b"",
             trailers=None,
             timestamp_start=0,
             timestamp_end=0,
         ))
     assert len(data) == 1
     assert data[0] == bytes.fromhex("00000101050000000288")
 def test_request_simple(self):
     bytes = HTTP2StateProtocol(self.c).assemble_request(http.Request(
         b'',
         b'GET',
         b'https',
         b'',
         b'',
         b'/',
         b"HTTP/2.0",
         (),
         None,
     ))
     assert len(bytes) == 1
     assert bytes[0] == codecs.decode('00000d0105000000018284874188089d5c0b8170dc07', 'hex_codec')
Esempio n. 6
0
 def test_with_body(self):
     data = HTTP2StateProtocol(self.c, is_server=True).assemble_response(http.Response(
         http_version=b"HTTP/2.0",
         status_code=200,
         reason=b'',
         headers=http.Headers(foo=b"bar"),
         content=b'foobar',
         trailers=None,
         timestamp_start=0,
         timestamp_end=0,
     ))
     assert len(data) == 2
     assert data[0] == bytes.fromhex("00000901040000000288408294e7838c767f")
     assert data[1] == bytes.fromhex("000006000100000002666f6f626172")
    def test_create_headers_multiple_frames(self):
        headers = http.Headers([
            (b':method', b'GET'),
            (b':path', b'/'),
            (b':scheme', b'https'),
            (b'foo', b'bar'),
            (b'server', b'version')])

        protocol = HTTP2StateProtocol(self.c)
        protocol.http2_settings[hyperframe.frame.SettingsFrame.MAX_FRAME_SIZE] = 8
        bytes = protocol._create_headers(headers, 1, end_stream=True)
        assert len(bytes) == 3
        assert bytes[0] == codecs.decode('000008010100000001828487408294e783', 'hex_codec')
        assert bytes[1] == codecs.decode('0000080900000000018c767f7685ee5b10', 'hex_codec')
        assert bytes[2] == codecs.decode('00000209040000000163d5', 'hex_codec')
    def test_read_empty_response(self):
        c = tcp.TCPClient(("127.0.0.1", self.port))
        with c.connect():
            c.convert_to_ssl()
            protocol = HTTP2StateProtocol(c)
            protocol.connection_preface_performed = True

            resp = protocol.read_response(NotImplemented, stream_id=42)

            assert resp.stream_id == 42
            assert resp.http_version == "HTTP/2.0"
            assert resp.status_code == 200
            assert resp.reason == ''
            assert resp.headers.fields == ((b':status', b'200'), (b'etag', b'foobar'))
            assert resp.content == b''
    def test_read_request(self):
        c = tcp.TCPClient(("127.0.0.1", self.port))
        with c.connect():
            c.convert_to_ssl()
            protocol = HTTP2StateProtocol(c, is_server=True)
            protocol.connection_preface_performed = True

            req = protocol.read_request(NotImplemented)

            assert req.stream_id
            assert req.headers.fields == ()
            assert req.method == "GET"
            assert req.path == "/"
            assert req.scheme == "https"
            assert req.content == b'foobar'
 def test_request_with_stream_id(self):
     req = http.Request(
         b'',
         b'GET',
         b'https',
         b'',
         b'',
         b'/',
         b"HTTP/2.0",
         (),
         None,
     )
     req.stream_id = 0x42
     bytes = HTTP2StateProtocol(self.c).assemble_request(req)
     assert len(bytes) == 1
     assert bytes[0] == codecs.decode('00000d0105000000428284874188089d5c0b8170dc07', 'hex_codec')
Esempio n. 11
0
 def test_request_simple(self):
     data = HTTP2StateProtocol(self.c).assemble_request(http.Request(
         host="",
         port=0,
         method=b'GET',
         scheme=b'https',
         authority=b'',
         path=b'/',
         http_version=b"HTTP/2.0",
         headers=(),
         content=None,
         trailers=None,
         timestamp_start=0,
         timestamp_end=0
     ))
     assert len(data) == 1
     assert data[0] == bytes.fromhex('00000d0105000000018284874188089d5c0b8170dc07')
 def test_request_with_body(self):
     bytes = HTTP2StateProtocol(self.c).assemble_request(http.Request(
         b'',
         b'GET',
         b'https',
         b'',
         b'',
         b'/',
         b"HTTP/2.0",
         http.Headers([(b'foo', b'bar')]),
         b'foobar',
     ))
     assert len(bytes) == 2
     assert bytes[0] ==\
         codecs.decode('0000150104000000018284874188089d5c0b8170dc07408294e7838c767f', 'hex_codec')
     assert bytes[1] ==\
         codecs.decode('000006000100000001666f6f626172', 'hex_codec')
Esempio n. 13
0
 def test_request_with_body(self):
     data = HTTP2StateProtocol(self.c).assemble_request(http.Request(
         host="",
         port=0,
         method=b'GET',
         scheme=b'https',
         authority=b'',
         path=b'/',
         http_version=b"HTTP/2.0",
         headers=http.Headers([(b'foo', b'bar')]),
         content=b'foobar',
         trailers=None,
         timestamp_start=0,
         timestamp_end=None,
     ))
     assert len(data) == 2
     assert data[0] == bytes.fromhex("0000150104000000018284874188089d5c0b8170dc07408294e7838c767f")
     assert data[1] == bytes.fromhex("000006000100000001666f6f626172")
Esempio n. 14
0
    def test_connect(self):
        c = tcp.TCPClient(("127.0.0.1", self.port))
        with c.connect():
            c.convert_to_ssl()
            protocol = HTTP2StateProtocol(c, is_server=True)
            protocol.connection_preface_performed = True

            req = protocol.read_request(NotImplemented)
            assert req.first_line_format == "authority"
            assert req.method == "CONNECT"
            assert req.host == "address"
            assert req.port == 22

            req = protocol.read_request(NotImplemented)
            assert req.first_line_format == "authority"
            assert req.method == "CONNECT"
            assert req.host == "example.com"
            assert req.port == 443
    def test_apply_settings(self):
        c = tcp.TCPClient(("127.0.0.1", self.port))
        with c.connect():
            c.convert_to_ssl()
            protocol = HTTP2StateProtocol(c)

            protocol._apply_settings({
                hyperframe.frame.SettingsFrame.ENABLE_PUSH: 'foo',
                hyperframe.frame.SettingsFrame.MAX_CONCURRENT_STREAMS: 'bar',
                hyperframe.frame.SettingsFrame.INITIAL_WINDOW_SIZE: 'deadbeef',
            })

            assert c.rfile.safe_read(2) == b"OK"

            assert protocol.http2_settings[
                hyperframe.frame.SettingsFrame.ENABLE_PUSH] == 'foo'
            assert protocol.http2_settings[
                hyperframe.frame.SettingsFrame.MAX_CONCURRENT_STREAMS] == 'bar'
            assert protocol.http2_settings[
                hyperframe.frame.SettingsFrame.INITIAL_WINDOW_SIZE] == 'deadbeef'
Esempio n. 16
0
def default_settings():
    return language.Settings(
        request_host="foo.com",
        protocol=HTTP2StateProtocol(tcp.TCPClient(('localhost', 1234)))
    )
 def test_check_alpn(self):
     c = tcp.TCPClient(("127.0.0.1", self.port))
     with c.connect():
         c.convert_to_ssl(alpn_protos=[b'h2'])
         protocol = HTTP2StateProtocol(c)
         assert protocol.check_alpn()
Esempio n. 18
0
 def test_create_body_single_frame(self):
     protocol = HTTP2StateProtocol(self.c)
     data = protocol._create_body(b'foobar', 1)
     assert b''.join(data) == bytes.fromhex("000006000100000001666f6f626172")
 def test_wrapped(self):
     h = TCPHandler(rfile='foo', wfile='bar')
     p = HTTP2StateProtocol(h)
     assert p.tcp_handler.rfile == 'foo'
     assert p.tcp_handler.wfile == 'bar'
 def test_direct(self):
     p = HTTP2StateProtocol(rfile='foo', wfile='bar')
     assert isinstance(p.tcp_handler, TCPHandler)
     assert p.tcp_handler.rfile == 'foo'
     assert p.tcp_handler.wfile == 'bar'
 def test_create_body_empty(self):
     protocol = HTTP2StateProtocol(self.c)
     bytes = protocol._create_body(b'', 1)
     assert b''.join(bytes) == b''
 def test_create_body_single_frame(self):
     protocol = HTTP2StateProtocol(self.c)
     bytes = protocol._create_body(b'foobar', 1)
     assert b''.join(bytes) == codecs.decode('000006000100000001666f6f626172', 'hex_codec')