예제 #1
0
def test_message_input_eq_works_as_expected():
    """
    Verify message equality
    """
    m1 = restserver.Message({"foo": "bar"})
    m2 = restserver.Message({"foo": "bar"})
    m3 = restserver.Message({"foo": "bar"}, type="message")
    assert m1 == m2
    assert m1 != m3
    assert m1 != object()
예제 #2
0
def test_message_input_accepts_expected_constructor_values():
    """
    Verify that message constructor accepts expected inputs.
    """
    message = restserver.Message("foo", type="message", id=123, retry=100)
    assert message.data == "foo"
    assert message.type == "message"
    assert message.id == 123
    assert message.retry == 100
예제 #3
0
def test_message_with_simple_data():
    """
    Verify that message works with simple data.
    """
    message = restserver.Message("foo")
    assert message.data == "foo"
    assert message.type is None
    assert message.id is None
    assert message.retry is None
    assert str(message) == "data:foo\n\n"
예제 #4
0
def test_message_with_multiline_data():
    """
    Verify that message works with multiline data.
    """
    message = restserver.Message("foo\nbar")
    assert message.data == "foo\nbar"
    assert message.type is None
    assert message.id is None
    assert message.retry is None
    assert str(message) == "data:foo\ndata:bar\n\n"
예제 #5
0
def test_sse_complex_messages_are_streamed_correctly(client):
    """
    Verify that Messages containing structured data are streamed correctly.
    """
    msg = restserver.Message({"foo": "bar"}, type="message", id=123)

    with mock.patch(
            "oet.procedure.application.restserver.ServerSentEventsBlueprint.messages"
    ) as mock_messages:
        mock_messages.return_value = [msg]
        response = client.get("/api/v1.0/stream")

        assert isinstance(response, flask.Response)
        assert response.mimetype == "text/event-stream"
        assert response.status_code == 200
        assert response.is_streamed
        output = response.get_data(as_text=True)
        assert output == '\nevent:message\ndata:{"foo": "bar"}\nid:123\n\n'
예제 #6
0
def test_sse_string_messages_are_streamed_correctly(client):
    """
    Verify that simple Messages are streamed as SSE events correctly.
    """
    msg = restserver.Message("foo", type="message")

    with mock.patch(
            "oet.procedure.application.restserver.ServerSentEventsBlueprint.messages"
    ) as mock_messages:
        mock_messages.return_value = [msg]
        response = client.get("/api/v1.0/stream")

        assert isinstance(response, flask.Response)
        assert response.mimetype == "text/event-stream"
        assert response.status_code == 200
        assert response.is_streamed
        output = response.get_data(as_text=True)
        assert output == "\nevent:message\ndata:foo\n\n"
예제 #7
0
def test_sse_messages_returns_pubsub_messages(client):
    """
    Test that pypubsub messages are returned by SSE blueprint's messages method.
    """
    def publish():
        # sleep long enough for generator to start running
        time.sleep(0.1)
        pub.sendMessage(topics.scan.lifecycle.start,
                        msg_src="foo",
                        sb_id="bar")

    t = threading.Thread(target=publish)

    bp = client.application.blueprints["sse"]
    gen = bp.messages()
    assert isinstance(gen, types.GeneratorType)

    t.start()

    output = next(gen)
    assert output == restserver.Message(
        dict(topic="scan.lifecycle.start", msg_src="foo", sb_id="bar"))
예제 #8
0
def test_message_raise_exception_on_empty():
    """
    Verify that empty message() raise exception
    """
    with pytest.raises(TypeError):
        restserver.Message()
예제 #9
0
def test_message_str():
    """
    Verify that the str string for a Message is correctly formatted.
    """
    message = restserver.Message("foo", type="message", id=123, retry=100)
    assert str(message) == "event:message\ndata:foo\nid:123\nretry:100\n\n"