Skip to content

Commit 4834dff

Browse files
authored
Samples image test server (cloudevents#79)
* fixed none data issue Signed-off-by: Curtis Mason <cumason@google.com> * added none data test for marshalling Signed-off-by: Curtis Mason <cumason@google.com> * lint fix Signed-off-by: Curtis Mason <cumason@google.com> * added http server test in image sample Signed-off-by: Curtis Mason <cumason@google.com> * Removed print statements from test Signed-off-by: Curtis Mason <cumason@google.com> * removed requests from test Signed-off-by: Curtis Mason <cumason@google.com>
1 parent c704120 commit 4834dff

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

requirements/test.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ sanic
1010
aiohttp
1111
Pillow
1212
requests
13-
13+
Flask

samples/http-image-cloudevents/server.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# under the License.
1414
import io
1515

16-
from flask import Flask, request
16+
from flask import Flask, Response, request
1717
from PIL import Image
1818

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

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

4141

4242
if __name__ == "__main__":

samples/http-image-cloudevents/test_image_sample.py

+47-6
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
import io
33
import json
44

5-
import requests
5+
import pytest
6+
from client import image_bytes
67
from PIL import Image
8+
from server import app
79

810
from cloudevents.sdk.http import (
911
CloudEvent,
@@ -12,15 +14,16 @@
1214
to_structured_http,
1315
)
1416

15-
resp = requests.get(
16-
"https://raw.githubusercontent.com/cncf/artwork/master/projects/cloudevents/horizontal/color/cloudevents-horizontal-color.png"
17-
)
18-
image_bytes = resp.content
19-
2017
image_fileobj = io.BytesIO(image_bytes)
2118
image_expected_shape = (1880, 363)
2219

2320

21+
@pytest.fixture
22+
def client():
23+
app.testing = True
24+
return app.test_client()
25+
26+
2427
def test_create_binary_image():
2528
# Create image and turn image into bytes
2629
attributes = {
@@ -80,6 +83,44 @@ def test_create_structured_image():
8083
assert restore_image.size == image_expected_shape
8184

8285

86+
def test_server_structured(client):
87+
attributes = {
88+
"type": "com.example.base64",
89+
"source": "https://example.com/event-producer",
90+
}
91+
92+
event = CloudEvent(attributes, image_bytes)
93+
94+
# Create cloudevent HTTP headers and content
95+
# Note that to_structured_http will create a data_base64 data field in
96+
# specversion 1.0 (default specversion) if given
97+
# an event whose data field is of type bytes.
98+
headers, body = to_structured_http(event)
99+
100+
# Send cloudevent
101+
r = client.post("/", headers=headers, data=body)
102+
assert r.status_code == 200
103+
assert r.data.decode() == f"Found image of size {image_expected_shape}"
104+
105+
106+
def test_server_binary(client):
107+
# Create cloudevent
108+
attributes = {
109+
"type": "com.example.string",
110+
"source": "https://example.com/event-producer",
111+
}
112+
113+
event = CloudEvent(attributes, image_bytes)
114+
115+
# Create cloudevent HTTP headers and content
116+
headers, body = to_binary_http(event)
117+
118+
# Send cloudevent
119+
r = client.post("/", headers=headers, data=body)
120+
assert r.status_code == 200
121+
assert r.data.decode() == f"Found image of size {image_expected_shape}"
122+
123+
83124
def test_image_content():
84125
# Get image and check size
85126
im = Image.open(image_fileobj)

0 commit comments

Comments
 (0)