1818
1919import pytest
2020from client import image_bytes
21- from cloudevents_v1 .conversion import to_binary , to_structured
22- from cloudevents_v1 .http import CloudEvent , from_http
2321from image_sample_server import app
2422from PIL import Image
2523
24+ from cloudevents .core .bindings .http import (
25+ CloudEvent ,
26+ from_http_event ,
27+ to_binary_event ,
28+ to_structured_event ,
29+ )
30+
2631image_fileobj = io .BytesIO (image_bytes )
2732image_expected_shape = (1880 , 363 )
2833
@@ -36,6 +41,8 @@ def client():
3641def test_create_binary_image ():
3742 # Create image and turn image into bytes
3843 attributes = {
44+ "id" : "123" ,
45+ "specversion" : "1.0" ,
3946 "type" : "com.example.string" ,
4047 "source" : "https://example.com/event-producer" ,
4148 }
@@ -44,25 +51,24 @@ def test_create_binary_image():
4451 event = CloudEvent (attributes , image_bytes )
4552
4653 # Create http headers/body content
47- headers , body = to_binary (event )
54+ http_message = to_binary_event (event )
4855
4956 # Unmarshall CloudEvent and re-create image
50- reconstruct_event = from_http (
51- headers , body , data_unmarshaller = lambda x : io .BytesIO (x )
52- )
57+ reconstruct_event = from_http_event (http_message )
5358
54- # reconstruct_event.data is an io.BytesIO object due to data_unmarshaller
55- restore_image = Image .open (reconstruct_event .data )
59+ restore_image = Image .open (io .BytesIO (reconstruct_event .get_data ()))
5660 assert restore_image .size == image_expected_shape
5761
5862 # # Test cloudevent extension from http fields and data
59- assert isinstance (body , bytes )
60- assert body == image_bytes
63+ assert isinstance (http_message . body , bytes )
64+ assert http_message . body == image_bytes
6165
6266
6367def test_create_structured_image ():
6468 # Create image and turn image into bytes
6569 attributes = {
70+ "id" : "123" ,
71+ "specversion" : "1.0" ,
6672 "type" : "com.example.string" ,
6773 "source" : "https://example.com/event-producer" ,
6874 }
@@ -71,29 +77,28 @@ def test_create_structured_image():
7177 event = CloudEvent (attributes , image_bytes )
7278
7379 # Create http headers/body content
74- headers , body = to_structured (event )
80+ http_message = to_structured_event (event )
7581
7682 # Structured has cloudevent attributes marshalled inside the body. For this
7783 # reason we must load the byte object to create the python dict containing
7884 # the cloudevent attributes
79- data = json .loads (body )
85+ data = json .loads (http_message . body . decode () )
8086
8187 # Test cloudevent extension from http fields and data
8288 assert isinstance (data , dict )
8389 assert base64 .b64decode (data ["data_base64" ]) == image_bytes
8490
8591 # Unmarshall CloudEvent and re-create image
86- reconstruct_event = from_http (
87- headers , body , data_unmarshaller = lambda x : io .BytesIO (x )
88- )
92+ reconstruct_event = from_http_event (http_message )
8993
90- # reconstruct_event.data is an io.BytesIO object due to data_unmarshaller
91- restore_image = Image .open (reconstruct_event .data )
94+ restore_image = Image .open (io .BytesIO (reconstruct_event .get_data ()))
9295 assert restore_image .size == image_expected_shape
9396
9497
9598def test_server_structured (client ):
9699 attributes = {
100+ "id" : "123" ,
101+ "specversion" : "1.0" ,
97102 "type" : "com.example.base64" ,
98103 "source" : "https://example.com/event-producer" ,
99104 }
@@ -104,28 +109,30 @@ def test_server_structured(client):
104109 # Note that to_structured will create a data_base64 data field in
105110 # specversion 1.0 (default specversion) if given
106111 # an event whose data field is of type bytes.
107- headers , body = to_structured (event )
112+ http_message = to_structured_event (event )
108113
109114 # Send cloudevent
110- r = client .post ("/" , headers = headers , data = body )
115+ r = client .post ("/" , headers = http_message . headers , data = http_message . body )
111116 assert r .status_code == 200
112117 assert r .data .decode () == f"Found image of size { image_expected_shape } "
113118
114119
115120def test_server_binary (client ):
116121 # Create cloudevent
117122 attributes = {
123+ "id" : "123" ,
124+ "specversion" : "1.0" ,
118125 "type" : "com.example.string" ,
119126 "source" : "https://example.com/event-producer" ,
120127 }
121128
122129 event = CloudEvent (attributes , image_bytes )
123130
124131 # Create cloudevent HTTP headers and content
125- headers , body = to_binary (event )
132+ http_message = to_binary_event (event )
126133
127134 # Send cloudevent
128- r = client .post ("/" , headers = headers , data = body )
135+ r = client .post ("/" , headers = http_message . headers , data = http_message . body )
129136 assert r .status_code == 200
130137 assert r .data .decode () == f"Found image of size { image_expected_shape } "
131138
0 commit comments