Skip to content

Samples image test server #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion requirements/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ sanic
aiohttp
Pillow
requests

Flask
4 changes: 2 additions & 2 deletions samples/http-image-cloudevents/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# under the License.
import io

from flask import Flask, request
from flask import Flask, Response, request
from PIL import Image

from cloudevents.sdk.http import from_http
Expand All @@ -36,7 +36,7 @@ def home():

# Print
print(f"Found event {event['id']} with image of size {image.size}")
return "", 204
return f"Found image of size {image.size}", 200


if __name__ == "__main__":
Expand Down
53 changes: 47 additions & 6 deletions samples/http-image-cloudevents/test_image_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
import io
import json

import requests
import pytest
from client import image_bytes
from PIL import Image
from server import app

from cloudevents.sdk.http import (
CloudEvent,
Expand All @@ -12,15 +14,16 @@
to_structured_http,
)

resp = requests.get(
"https://raw.githubusercontent.com/cncf/artwork/master/projects/cloudevents/horizontal/color/cloudevents-horizontal-color.png"
)
image_bytes = resp.content

image_fileobj = io.BytesIO(image_bytes)
image_expected_shape = (1880, 363)


@pytest.fixture
def client():
app.testing = True
return app.test_client()


def test_create_binary_image():
# Create image and turn image into bytes
attributes = {
Expand Down Expand Up @@ -80,6 +83,44 @@ def test_create_structured_image():
assert restore_image.size == image_expected_shape


def test_send_structured_request(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}"


def test_send_binary_request(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_image_content():
# Get image and check size
im = Image.open(image_fileobj)
Expand Down