def test_none_data_cloudevent(specversion): event = CloudEvent( { "source": "<my-url>", "type": "issue.example", "specversion": specversion, } ) to_binary_http(event) to_structured_http(event)
def test_create_binary_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_binary_http(event) # 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 # # Test cloudevent extension from http fields and data assert isinstance(body, bytes) assert body == image_bytes
def test_binary_request(client): # This data defines a binary cloudevent attributes = { "type": "com.example.sampletype1", "source": "https://example.com/event-producer", } data = {"message": "Hello World!"} event = CloudEvent(attributes, data) headers, body = to_binary_http(event) r = client.post("/", headers=headers, data=body) assert r.status_code == 204
def send_binary_cloud_event(url): # This data defines a binary cloudevent attributes = { "type": "com.example.sampletype1", "source": "https://example.com/event-producer", } data = {"message": "Hello World!"} event = CloudEvent(attributes, data) headers, body = to_binary_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}")
def send_binary_cloud_event(url: str): # Create cloudevent attributes = { "type": "com.example.string", "source": "https://example.com/event-producer", } event = CloudEvent(attributes, image_bytes) # Create cloudevent HTTP headers and content headers, body = to_binary_http(event) # Send cloudevent requests.post(url, headers=headers, data=body) print(f"Sent {event['id']} of type {event['type']}")
def test_server_binary(client): # Create cloudevent attributes = { "type": "com.example.string", "source": "https://example.com/event-producer", } event = CloudEvent(attributes, image_bytes) # Create cloudevent HTTP headers and content headers, body = to_binary_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}"
def test_binary_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_binary_http(event) body = json.loads(body_bytes) for key in data: assert body[key] == data[key] for key in attributes: assert attributes[key] == headers["ce-" + key]
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_binary_extensions(specversion): event = CloudEvent(test_attributes, test_data) headers, body = to_binary_http(event) assert "ce-ext1" in headers assert headers.get("ce-ext1") == test_attributes["ext1"]
def test_to_binary_http_deprecated(event): with pytest.deprecated_call(): assert to_binary(event) == to_binary_http(event)