Ejemplo n.º 1
0
def test_transcripts_multi(connection):
    t0 = RecordedWSTranscript.from_connection(connection)
    t0.append(RecordedWSMessage('hi', Sender.Server))
    t1 = RecordedWSTranscript.from_connection(connection)
    t1.append(RecordedWSMessage('ho', Sender.Server))
    t0.close = t1.close = (Sender.Server, 1000)
    t0.accepted = t1.accepted = True
    transcripts = RecordedWSTranscripts([t0, t1])
    with raises(AssertionError):
        transcripts.assert_not_requested()
    transcripts.assert_requested()
    with raises(AssertionError):
        transcripts.assert_requested_once()
    transcripts.assert_requested_with(
        ExpectedWSTranscript([..., Sender.Server('ho')]))
    with raises(AssertionError):
        transcripts.assert_requested_with(
            ExpectedWSTranscript([..., Sender.Server('hi')]))
    with raises(AssertionError):
        transcripts.assert_requested_once_with(ExpectedWSTranscript([...]))
    transcripts.assert_any_request(
        ExpectedWSTranscript([..., Sender.Server('hi')]))
    transcripts.assert_any_request(
        ExpectedWSTranscript([..., Sender.Server('ho')]))
    with raises(AssertionError):
        transcripts.assert_any_request(
            ExpectedWSTranscript([..., Sender.Server('hee')]))
Ejemplo n.º 2
0
def test_transcript_mismatches_scope(connection):
    connection.headers.raw = [('a', '1'), ('b', '1'), ('a', '2')]
    connection.url.path = '/foobar'
    connection.path_params = {'x': 15}
    connection.query_params.multi_items.return_value = [('s', '1'), ('r', '1'),
                                                        ('s', '2')]
    transcript = RecordedWSTranscript.from_connection(connection)
    transcript.extend([
        RecordedWSMessage('sir if I may be so bold?', Sender.Server),
        RecordedWSMessage('go ahead Jeeves', Sender.Client),
        RecordedWSMessage(b'crunch', Sender.Server),
        RecordedWSMessage('Jeeves! That is bold', Sender.Client),
    ])
    transcript.close = True
    transcript.accepted = True

    expected = ExpectedWSTranscript([...], headers_submap=dict(a={'1', '3'}))
    assert not expected.matches(transcript)
    expected = ExpectedWSTranscript(
        [..., Sender.Server(...),
         Sender.Client('go ahead Jeeves')], )
    assert not expected.matches(transcript)
    expected = ExpectedWSTranscript([
        Sender.Server(re.compile(b'crun[ct]h')),
        Sender.Client('Jeeves! That is bold'), ...
    ], )
    assert not expected.matches(transcript)
    expected = ExpectedWSTranscript([
        Sender.Client('go ahead Jeeves'),
        Sender.Server(re.compile(b'crun[ct]h')),
        Sender.Client('Jeeves! That is bold')
    ], )
    assert not expected.matches(transcript)
Ejemplo n.º 3
0
def test_repr_transcript(connection):
    transcript = RecordedWSTranscript.from_connection(connection)
    transcript.extend([
        RecordedWSMessage('sir if I may be so bold?', Sender.Server),
        RecordedWSMessage('go ahead Jeeves', Sender.Client),
        RecordedWSMessage(b'crunch', Sender.Server),
        RecordedWSMessage('Jeeves! That is bold', Sender.Client),
    ])
    assert repr(transcript) \
           == "[RecordedWSMessage('sir if I may be so bold?', Sender.Server)," \
              " RecordedWSMessage('go ahead Jeeves', Sender.Client)," \
              " RecordedWSMessage(b'crunch', Sender.Server)," \
              " RecordedWSMessage('Jeeves! That is bold', Sender.Client)]"
Ejemplo n.º 4
0
def test_transcript_matches(close, expected_close, accepted, expected_accepted,
                            connection):
    transcript = RecordedWSTranscript.from_connection(connection)
    transcript.extend([
        RecordedWSMessage('sir if I may be so bold?', Sender.Server),
        RecordedWSMessage('go ahead Jeeves', Sender.Client),
        RecordedWSMessage(b'crunch', Sender.Server),
        RecordedWSMessage('Jeeves! That is bold', Sender.Client),
    ])
    transcript.accepted = accepted
    transcript.close = close

    expected = ExpectedWSTranscript([
        ...,
        Sender.Client('go ahead Jeeves'),
        Sender.Server(re.compile(b'crun[ct]h')), ...
    ],
                                    close=expected_close,
                                    accepted=expected_accepted)
    assert expected.matches(transcript)
    expected = ExpectedWSTranscript(
        [Sender.Server(...),
         Sender.Client('go ahead Jeeves'), ...],
        close=expected_close,
        accepted=expected_accepted)
    assert expected.matches(transcript)
    expected = ExpectedWSTranscript([
        ...,
        Sender.Server(re.compile(b'crun[ct]h')),
        Sender.Client('Jeeves! That is bold')
    ],
                                    close=expected_close,
                                    accepted=expected_accepted)
    assert expected.matches(transcript)
    expected = ExpectedWSTranscript([
        Sender.Server(...),
        Sender.Client('go ahead Jeeves'),
        Sender.Server(re.compile(b'crun[ct]h')),
        Sender.Client('Jeeves! That is bold')
    ],
                                    close=expected_close,
                                    accepted=expected_accepted)
    assert expected.matches(transcript)
    expected = ExpectedWSTranscript([...],
                                    close=expected_close,
                                    accepted=expected_accepted)
    assert expected.matches(transcript)
Ejemplo n.º 5
0
def test_ws_capture_client_close(server, ws_client_factory):
    @server.add_ws_endpoint
    @ws_endpoint('/bar')
    async def bar(websocket: WebSocket):
        await websocket.accept()
        await websocket.send_text('do you like warhammer?')
        msg = await websocket.receive()
        assert msg['type'] == 'websocket.disconnect'

    with bar.capture_calls() as transcripts:
        ws_client = ws_client_factory('/bar')
        assert ws_client.recv() == 'do you like warhammer?'
        ws_client.close()

    sleep(0.1)  # give the server time to record the closing
    transcript, = transcripts
    assert list(transcript) == [
        RecordedWSMessage('do you like warhammer?', Sender.Server),
    ]
    assert transcript.accepted
    assert transcript.close == (Sender.Client, 1000)
Ejemplo n.º 6
0
def test_ws_capture_empties(server, ws_client_factory):
    @server.add_ws_endpoint
    @ws_endpoint('/bar')
    async def bar(websocket: WebSocket):
        await websocket.accept()
        while True:
            msg = await websocket.receive()
            if msg['type'] == 'websocket.disconnect':
                return
            if 'text' in msg:
                await websocket.send_text(msg['text'])
            else:
                await websocket.send_bytes(msg['bytes'])

    with bar.capture_calls() as transcripts:
        ws_client = ws_client_factory('/bar')
        ws_client.send_binary(b'a')
        assert ws_client.recv() == b'a'
        ws_client.send_binary(b'')
        assert ws_client.recv() == b''
        ws_client.send('')
        assert ws_client.recv() == ''
        ws_client.send('a')
        assert ws_client.recv() == 'a'
        ws_client.close()

    t, = transcripts
    assert list(t) == [
        RecordedWSMessage(b'a', Sender.Client),
        RecordedWSMessage(b'a', Sender.Server),
        RecordedWSMessage(b'', Sender.Client),
        RecordedWSMessage(b'', Sender.Server),
        RecordedWSMessage('', Sender.Client),
        RecordedWSMessage('', Sender.Server),
        RecordedWSMessage('a', Sender.Client),
        RecordedWSMessage('a', Sender.Server),
    ]
Ejemplo n.º 7
0
def test_ws_calc_capture_calls(server, ws_client_factory, ws_calc):
    with ws_calc.capture_calls() as transcripts:
        ws_client = ws_client_factory('/12/calc?tee=goo')
        assert json.loads(ws_client.recv()) == 12
        ws_client.send('{"op":"add", "value": 3}')
        assert json.loads(ws_client.recv()) == 15
        ws_client.send('{"op":"mul", "value": 10}')
        assert json.loads(ws_client.recv()) == 150
        ws_client.send('{"op":"done"}')
        assert_ws_closed(ws_client, 1000)

    transcript, = transcripts
    assert list(transcript) == [
        RecordedWSMessage('12', Sender.Server),
        RecordedWSMessage('{"op":"add", "value": 3}', Sender.Client),
        RecordedWSMessage('15', Sender.Server),
        RecordedWSMessage('{"op":"mul", "value": 10}', Sender.Client),
        RecordedWSMessage('150', Sender.Server),
        RecordedWSMessage('{"op":"done"}', Sender.Client),
    ]
    assert transcript.accepted
    assert transcript.close == (Sender.Server, 1000)