@@ -45,8 +45,8 @@ def test_event_pipeline_upstream(event_class):
4545 assert "ce-id" in new_headers
4646 assert "ce-time" in new_headers
4747 assert "content-type" in new_headers
48- assert isinstance (body , str )
49- assert data .body == body
48+ assert isinstance (body , bytes )
49+ assert data .body == body . decode ( "utf-8" )
5050
5151
5252def test_extensions_are_set_upstream ():
@@ -76,8 +76,8 @@ def test_event_pipeline_v01():
7676 m = marshaller .NewHTTPMarshaller ([structured .NewJSONHTTPCloudEventConverter ()])
7777
7878 _ , body = m .ToRequest (event , converters .TypeStructured , lambda x : x )
79- assert isinstance (body , io . BytesIO )
80- new_headers = json .load ( io . TextIOWrapper ( body , encoding = "utf-8" ) )
79+ assert isinstance (body , bytes )
80+ new_headers = json .loads ( body )
8181 assert new_headers is not None
8282 assert "cloudEventsVersion" in new_headers
8383 assert "eventType" in new_headers
@@ -86,3 +86,41 @@ def test_event_pipeline_v01():
8686 assert "eventTime" in new_headers
8787 assert "contentType" in new_headers
8888 assert data .body == new_headers ["data" ]
89+
90+ def test_binary_event_v1 ():
91+ event = (
92+ v1 .Event ()
93+ .SetContentType ("application/octet-stream" )
94+ .SetData (b'\x00 \x01 ' )
95+ )
96+ m = marshaller .NewHTTPMarshaller ([structured .NewJSONHTTPCloudEventConverter ()])
97+
98+ _ , body = m .ToRequest (event , converters .TypeStructured , lambda x : x )
99+ assert isinstance (body , bytes )
100+ content = json .loads (body )
101+ assert "data" not in content
102+ assert content ["data_base64" ] == "AAE=" , f"Content is: { content } "
103+
104+ def test_object_event_v1 ():
105+ event = (
106+ v1 .Event ()
107+ .SetContentType ("application/json" )
108+ .SetData ({"name" : "john" })
109+ )
110+
111+ m = marshaller .NewDefaultHTTPMarshaller ()
112+
113+ _ , structuredBody = m .ToRequest (event )
114+ assert isinstance (structuredBody , bytes )
115+ structuredObj = json .loads (structuredBody )
116+ errorMsg = f"Body was { structuredBody } , obj is { structuredObj } "
117+ assert isinstance (structuredObj , dict ), errorMsg
118+ assert isinstance (structuredObj ["data" ], dict ), errorMsg
119+ assert len (structuredObj ["data" ]) == 1 , errorMsg
120+ assert structuredObj ["data" ]["name" ] == "john" , errorMsg
121+
122+ headers , binaryBody = m .ToRequest (event , converters .TypeBinary )
123+ assert isinstance (headers , dict )
124+ assert isinstance (binaryBody , bytes )
125+ assert headers ["content-type" ] == "application/json"
126+ assert binaryBody == b'{"name": "john"}' , f"Binary is { binaryBody !r} "
0 commit comments