예제 #1
0
def test_send_payload_doesnt_log_token():
    conn = GatewayConnection(wslib=MockWebsocket, logging=MockLogger)
    conn.open()

    identify_payload = '{"op": 2, "d": {"token": "SENSITIVE_INFO", "properties": {"$os": "linux", "$browser": "Charlotte", "$device": "Charlotte"}}, "s": null, "t": null}'

    def send(packet):
        assert Payload.from_packet(packet) == Payload.from_packet(
            identify_payload)

    conn.ws.send = send

    payload = Payload.from_packet(identify_payload)
    conn.send_payload(payload)

    resume_payload = '{"op": 6, "d": {"token": "SENSITIVE_INFO", "properties": {"$os": "linux", "$browser": "Charlotte", "$device": "Charlotte"}}, "s": null, "t": null}'

    def send(packet):
        assert Payload.from_packet(packet) == Payload.from_packet(
            resume_payload)

    conn.ws.send = send

    payload = Payload.from_packet(resume_payload)
    conn.send_payload(payload)
예제 #2
0
def test_receive_payload_empty_packet():
    conn = GatewayConnection(wslib=MockWebsocket)
    conn.open()

    def recv():
        return ""

    conn.ws.recv = recv

    try:
        payload = conn.receive_payload()
        assert False, "Should have raised a disconnection error on empty packet."
    except DisconnectionException:
        pass
예제 #3
0
def test_receive_payload_success():
    conn = GatewayConnection(wslib=MockWebsocket)
    conn.open()

    hello_payload = '{"t":"hello1","s":3,"op":10,"d":{"heartbeat_interval":41250,"_trace":["gateway-prd-main-jptr"]}}'

    def recv():
        return hello_payload

    conn.ws.recv = recv

    payload = conn.receive_payload()
    assert payload == Payload.HELLO
    assert payload.data == {
        "heartbeat_interval": 41250,
        "_trace": ["gateway-prd-main-jptr"]
    }
    assert payload.seq_number == 3
    assert conn.last_seq == 3
    assert payload.event_name == "hello1"
예제 #4
0
def test_connected_same_as_websocket():
    conn = GatewayConnection(wslib=MockWebsocket)

    conn.open()
    assert conn.ws.connected == True
    assert conn.connected == True

    conn.close()
    assert conn.ws.closed
    assert conn.ws.connected == False
    assert conn.connected == False
예제 #5
0
def test_open_connection_already_open():
    conn = GatewayConnection(wslib=MockWebsocket)

    conn.open()
    assert conn.ws.connection_endpoint == conn.endpoint

    try:
        conn.open()
        assert False, "Should have raised a value error as the connection is already open."
    except ValueError:
        pass
예제 #6
0
def test_send_payload_success():
    conn = GatewayConnection(wslib=MockWebsocket)
    conn.open()

    hello_payload = '{"t":"hello1","s":3,"op":10,"d":{"heartbeat_interval":41250,"_trace":["gateway-prd-main-jptr"]}}'

    def send(packet):
        assert Payload.from_packet(packet) == Payload.from_packet(
            hello_payload)

    conn.ws.send = send

    payload = Payload.from_packet(hello_payload)
    conn.send_payload(payload)
예제 #7
0
def test_receive_payload_no_seq_number():
    conn = GatewayConnection(wslib=MockWebsocket)
    conn.open()

    hello_payload = '{"t":"hello1","s":3,"op":10,"d":{"heartbeat_interval":41250,"_trace":["gateway-prd-main-jptr"]}}'

    def recv():
        return hello_payload

    conn.ws.recv = recv
    payload = conn.receive_payload()
    assert conn.last_seq == 3

    hello_payload = '{"t":"hello2","s": null,"op":10,"d":{"heartbeat_interval":41250,"_trace":["gateway-prd-main-jptr"]}}'

    def recv():
        return hello_payload

    conn.ws.recv = recv
    payload = conn.receive_payload()
    assert conn.last_seq == 3
예제 #8
0
def test_context():
    with GatewayConnection(wslib=MockWebsocket) as conn:
        assert conn.connected
    assert not conn.connected
예제 #9
0
def test_close_connection():
    conn = GatewayConnection(wslib=MockWebsocket)
    try:
        conn.close()
    except:
        assert False, "Should not raise an error if connection is not open yet, not a big deal."

    conn = GatewayConnection(wslib=MockWebsocket)
    conn.open()
    assert conn.ws.connection_endpoint == conn.endpoint
    assert not conn.ws.closed

    conn.close()
    assert conn.ws.closed

    try:
        conn.close()
    except:
        assert False, "Should not raise an error if connection is already closed, not a big deal."
예제 #10
0
def test_open_connection_success():
    conn = GatewayConnection(wslib=MockWebsocket)

    conn.open()
    assert conn.ws.connection_endpoint == conn.endpoint