Ejemplo n.º 1
0
def test_none_data_cloudevent(specversion):
    event = CloudEvent(
        {
            "source": "<my-url>",
            "type": "issue.example",
            "specversion": specversion,
        }
    )
    to_binary_http(event)
    to_structured_http(event)
Ejemplo n.º 2
0
def test_create_structured_image():
    # Create image and turn image into bytes
    attributes = {
        "type": "com.example.string",
        "source": "https://example.com/event-producer",
    }

    # Create CloudEvent
    event = CloudEvent(attributes, image_bytes)

    # Create http headers/body content
    headers, body = to_structured_http(event)

    # Structured has cloudevent attributes marshalled inside the body. For this
    # reason we must load the byte object to create the python dict containing
    # the cloudevent attributes
    data = json.loads(body)

    # Test cloudevent extension from http fields and data
    assert isinstance(data, dict)
    assert base64.b64decode(data["data_base64"]) == image_bytes

    # Unmarshall CloudEvent and re-create image
    reconstruct_event = from_http(body,
                                  headers,
                                  data_unmarshaller=lambda x: io.BytesIO(x))

    # reconstruct_event.data is an io.BytesIO object due to data_unmarshaller
    restore_image = Image.open(reconstruct_event.data)
    assert restore_image.size == image_expected_shape
Ejemplo n.º 3
0
def test_to_structured_extensions(specversion):
    event = CloudEvent(test_attributes, test_data)
    headers, body = to_structured_http(event)

    body = json.loads(body)

    assert "ext1" in body
    assert "extensions" not in body
Ejemplo n.º 4
0
def test_structured_request(client):
    # This data defines a binary cloudevent
    attributes = {
        "type": "com.example.sampletype2",
        "source": "https://example.com/event-producer",
    }
    data = {"message": "Hello World!"}

    event = CloudEvent(attributes, data)
    headers, body = to_structured_http(event)

    r = client.post("/", headers=headers, data=body)
    assert r.status_code == 204
Ejemplo n.º 5
0
def send_structured_cloud_event(url):
    # This data defines a binary cloudevent
    attributes = {
        "type": "com.example.sampletype2",
        "source": "https://example.com/event-producer",
    }
    data = {"message": "Hello World!"}

    event = CloudEvent(attributes, data)
    headers, body = to_structured_http(event)

    # send and print event
    requests.post(url, headers=headers, data=body)
    print(f"Sent {event['id']} from {event['source']} with " f"{event.data}")
Ejemplo n.º 6
0
def send_structured_cloud_event(url: str):
    # Create cloudevent
    attributes = {
        "type": "com.example.base64",
        "source": "https://example.com/event-producer",
    }

    event = CloudEvent(attributes, image_bytes)

    # Create cloudevent HTTP headers and content
    # Note that to_structured_http will create a data_base64 data field in
    # specversion 1.0 (default specversion) if given
    # an event whose data field is of type bytes.
    headers, body = to_structured_http(event)

    # Send cloudevent
    requests.post(url, headers=headers, data=body)
    print(f"Sent {event['id']} of type {event['type']}")
Ejemplo n.º 7
0
def test_server_structured(client):
    attributes = {
        "type": "com.example.base64",
        "source": "https://example.com/event-producer",
    }

    event = CloudEvent(attributes, image_bytes)

    # Create cloudevent HTTP headers and content
    # Note that to_structured_http will create a data_base64 data field in
    # specversion 1.0 (default specversion) if given
    # an event whose data field is of type bytes.
    headers, body = to_structured_http(event)

    # Send cloudevent
    r = client.post("/", headers=headers, data=body)
    assert r.status_code == 200
    assert r.data.decode() == f"Found image of size {image_expected_shape}"
Ejemplo n.º 8
0
def test_structured_to_request(specversion):
    attributes = {
        "specversion": specversion,
        "type": "word.found.name",
        "id": "96fb5f0b-001e-0108-6dfe-da6e2806f124",
        "source": "pytest",
    }
    data = {"message": "Hello World!"}

    event = CloudEvent(attributes, data)
    headers, body_bytes = to_structured_http(event)
    assert isinstance(body_bytes, bytes)
    body = json.loads(body_bytes)

    assert headers["content-type"] == "application/cloudevents+json"
    for key in attributes:
        assert body[key] == attributes[key]
    assert body["data"] == data, f"|{body_bytes}|| {body}"
Ejemplo n.º 9
0
def test_roundtrip_non_json_event(converter, specversion):
    input_data = io.BytesIO()
    for i in range(100):
        for j in range(20):
            assert 1 == input_data.write(j.to_bytes(1, byteorder="big"))
    compressed_data = bz2.compress(input_data.getvalue())
    attrs = {"source": "test", "type": "t"}

    event = CloudEvent(attrs, compressed_data)

    if converter == converters.TypeStructured:
        headers, data = to_structured_http(event, data_marshaller=lambda x: x)
    elif converter == converters.TypeBinary:
        headers, data = to_binary_http(event, data_marshaller=lambda x: x)

    headers["binary-payload"] = "true"  # Decoding hint for server
    _, r = app.test_client.post("/event", headers=headers, data=data)

    assert r.status_code == 200
    for key in attrs:
        assert r.headers[key] == attrs[key]
    assert compressed_data == r.body, r.body
def test_to_structured_http_deprecated(event):
    with pytest.deprecated_call():
        assert to_structured(event) == to_structured_http(event)