Example #1
0
    def file_byte_response(self, stream_id=5, header_filename=""):
        frame_factory = FrameFactory()

        headers = self.example_response_headers + [
            ("content-type", "image/png")
        ]

        if header_filename:
            headers.append(
                (
                    "content-disposition",
                    'inline; filename="{}"'.format(header_filename),
                ),
            )

        f = frame_factory.build_headers_frame(
            headers=headers, stream_id=stream_id
        )

        body = self._load_byte_response("tests/data/file_response")

        data = frame_factory.build_data_frame(
            data=body,
            stream_id=stream_id,
            flags=['END_STREAM']
        )

        return f.serialize() + data.serialize()
Example #2
0
    def sync_byte_response(self):
        frame_factory = FrameFactory()

        f = frame_factory.build_headers_frame(
            headers=self.example_response_headers, stream_id=3)

        body = self._load_byte_response("tests/data/sync.json")

        data = frame_factory.build_data_frame(data=body,
                                              stream_id=3,
                                              flags=['END_STREAM'])

        return f.serialize() + data.serialize()
Example #3
0
    def get_avatar_byte_response(self, avatar_url, stream_id=5):
        frame_factory = FrameFactory()

        f = frame_factory.build_headers_frame(
            headers=self.example_response_headers, stream_id=stream_id)

        body = json.dumps({"avatar_url": avatar_url}).encode("utf-8")

        data = frame_factory.build_data_frame(data=body,
                                              stream_id=stream_id,
                                              flags=['END_STREAM'])

        return f.serialize() + data.serialize()
Example #4
0
    def empty_response(self, stream_id=5):
        frame_factory = FrameFactory()

        f = frame_factory.build_headers_frame(
            headers=self.example_response_headers, stream_id=stream_id)

        body = b"{}"

        data = frame_factory.build_data_frame(data=body,
                                              stream_id=stream_id,
                                              flags=['END_STREAM'])

        return f.serialize() + data.serialize()
Example #5
0
    def room_id_response(self, stream_id=5, room_id=TEST_ROOM_ID):
        frame_factory = FrameFactory()

        f = frame_factory.build_headers_frame(
            headers=self.example_response_headers, stream_id=stream_id)

        body = json.dumps({"room_id": room_id}).encode()

        data = frame_factory.build_data_frame(data=body,
                                              stream_id=stream_id,
                                              flags=['END_STREAM'])

        return f.serialize() + data.serialize()
Example #6
0
    def file_byte_response(self, stream_id=5):
        frame_factory = FrameFactory()

        headers = self.example_response_headers + [
            ("content-type", "image/png")
        ]

        f = frame_factory.build_headers_frame(headers=headers,
                                              stream_id=stream_id)

        body = self._load_byte_response("tests/data/file_response")

        data = frame_factory.build_data_frame(data=body,
                                              stream_id=stream_id,
                                              flags=['END_STREAM'])

        return f.serialize() + data.serialize()
Example #7
0
    def login_byte_response(self):
        frame_factory = FrameFactory()

        f = frame_factory.build_headers_frame(
            headers=self.example_response_headers, stream_id=1)

        login_body = json.dumps({
            "user_id": "@ephemeral:example.org",
            "access_token": "ABCD",
            "device_id": "DEVICEID",
        }).encode("utf-8")

        data = frame_factory.build_data_frame(data=login_body,
                                              stream_id=1,
                                              flags=['END_STREAM'])

        return f.serialize() + data.serialize()
Example #8
0
from gethy import HTTP2Protocol
from gethy.event import RequestEvent, MoreDataToSendEvent
from gethy.http2protocol import Stream

from helpers import FrameFactory

# 因为我们的测试很少,所以全局变量也OK
frame_factory = FrameFactory()
protocol = HTTP2Protocol()
protocol.receive(frame_factory.preamble())  # h2 建立连接时必要的字段
headers = [
    (':method', 'GET'),
    (':path', '/'),
    (':scheme', 'https'),
    (':authority', 'example.com'),
]


def test_receive_headers_only():
    """
	able to receive headers with no data
	"""
    # 客户端发起的 session 的 stream id 是单数
    # 服务器发起的 session 的 stream id 是双数
    # 一个 session 包含一对 request/response
    # id 0 代表整个 connection
    stream_id = 1

    # 在这里手动生成一个 client 的 request frame,来模拟客户端请求
    frame_from_client = frame_factory.build_headers_frame(headers,
                                                          stream_id=stream_id,