65
65
66
66
BACKGROUND_RESOURCE_STRING = "projects/_/buckets/some-bucket/objects/folder/Test.cs"
67
67
68
+ PUBSUB_CLOUD_EVENT = {
69
+ "specversion" : "1.0" ,
70
+ "id" : "1215011316659232" ,
71
+ "source" : "//pubsub.googleapis.com/projects/sample-project/topics/gcf-test" ,
72
+ "time" : "2020-05-18T12:13:19Z" ,
73
+ "type" : "google.cloud.pubsub.topic.v1.messagePublished" ,
74
+ "datacontenttype" : "application/json" ,
75
+ "data" : {
76
+ "message" : {
77
+ "data" : "10" ,
78
+ },
79
+ },
80
+ }
81
+
68
82
69
83
@pytest .fixture
70
84
def pubsub_cloudevent_output ():
71
- event = {
72
- "specversion" : "1.0" ,
73
- "id" : "1215011316659232" ,
74
- "source" : "//pubsub.googleapis.com/projects/sample-project/topics/gcf-test" ,
75
- "time" : "2020-05-18T12:13:19Z" ,
76
- "type" : "google.cloud.pubsub.topic.v1.messagePublished" ,
77
- "datacontenttype" : "application/json" ,
85
+ return from_json (json .dumps (PUBSUB_CLOUD_EVENT ))
86
+
87
+ @pytest .fixture
88
+ def raw_pubsub_request ():
89
+ return {
90
+ "subscription" : "projects/sample-project/subscriptions/gcf-test-sub" ,
91
+ "message" : {
92
+ "data" : "eyJmb28iOiJiYXIifQ==" ,
93
+ "messageId" : "1215011316659232" ,
94
+ "attributes" : {
95
+ "test" : "123"
96
+ }
97
+ }
98
+ }
99
+
100
+ @pytest .fixture
101
+ def marshalled_pubsub_request ():
102
+ return {
78
103
"data" : {
79
- "message" : {
80
- "data" : "10" ,
81
- },
104
+ "@type" : "type.googleapis.com/google.pubsub.v1.PubsubMessage" ,
105
+ "data" : "eyJmb28iOiJiYXIifQ==" ,
106
+ "attributes" : {
107
+ "test" : "123"
108
+ }
82
109
},
110
+ "context" : {
111
+ "eventId" : "1215011316659232" ,
112
+ "eventType" : "google.pubsub.topic.publish" ,
113
+ "resource" : {
114
+ "name" : "projects/sample-project/topics/gcf-test" ,
115
+ "service" : "pubsub.googleapis.com" ,
116
+ "type" : "type.googleapis.com/google.pubsub.v1.PubsubMessage"
117
+ },
118
+ "timestamp" : "2021-04-17T07:21:18.249Z" ,
119
+ }
83
120
}
84
121
122
+ @pytest .fixture
123
+ def raw_pubsub_cloudevent_output (marshalled_pubsub_request ):
124
+ event = PUBSUB_CLOUD_EVENT .copy ()
125
+ # the data payload is more complex for the raw pubsub request
126
+ event ["data" ] = {
127
+ "message" : marshalled_pubsub_request ["data" ]
128
+ }
85
129
return from_json (json .dumps (event ))
86
130
87
131
@@ -212,3 +256,64 @@ def test_split_resource_no_resource_regex_match():
212
256
with pytest .raises (EventConversionException ) as exc_info :
213
257
event_conversion ._split_resource (context )
214
258
assert "Resource regex did not match" in exc_info .value .args [0 ]
259
+
260
+
261
+ def test_marshal_background_event_data_without_topic_in_path (
262
+ raw_pubsub_request , marshalled_pubsub_request
263
+ ):
264
+ req = flask .Request .from_values (json = raw_pubsub_request , path = "/myfunc/" )
265
+ payload = event_conversion .marshal_background_event_data (req )
266
+
267
+ # Remove timestamps as they get generates on the fly
268
+ del marshalled_pubsub_request ["context" ]["timestamp" ]
269
+ del payload ["context" ]["timestamp" ]
270
+
271
+ # Resource name is set to None when it cannot be parsed from the request path
272
+ marshalled_pubsub_request ["context" ]["resource" ]["name" ] = None
273
+
274
+ assert payload == marshalled_pubsub_request
275
+
276
+ def test_marshal_background_event_data_with_topic_path (
277
+ raw_pubsub_request , marshalled_pubsub_request
278
+ ):
279
+ req = flask .Request .from_values (
280
+ json = raw_pubsub_request , path = "x/projects/sample-project/topics/gcf-test?pubsub_trigger=true"
281
+ )
282
+ payload = event_conversion .marshal_background_event_data (req )
283
+
284
+ # Remove timestamps as they are generated on the fly.
285
+ del marshalled_pubsub_request ["context" ]["timestamp" ]
286
+ del payload ["context" ]["timestamp" ]
287
+
288
+ assert payload == marshalled_pubsub_request
289
+
290
+ def test_pubsub_emulator_request_to_cloudevent (raw_pubsub_request , raw_pubsub_cloudevent_output ):
291
+ req = flask .Request .from_values (
292
+ json = raw_pubsub_request ,
293
+ path = "x/projects/sample-project/topics/gcf-test?pubsub_trigger=true"
294
+ )
295
+ cloudevent = event_conversion .background_event_to_cloudevent (req )
296
+
297
+ # Remove timestamps as they are generated on the fly.
298
+ del raw_pubsub_cloudevent_output ['time' ]
299
+ del cloudevent ['time' ]
300
+
301
+ assert cloudevent == raw_pubsub_cloudevent_output
302
+
303
+
304
+ def test_pubsub_emulator_request_to_cloudevent_without_topic_path (
305
+ raw_pubsub_request , raw_pubsub_cloudevent_output
306
+ ):
307
+ req = flask .Request .from_values (
308
+ json = raw_pubsub_request , path = "/"
309
+ )
310
+ cloudevent = event_conversion .background_event_to_cloudevent (req )
311
+
312
+ # Remove timestamps as they are generated on the fly.
313
+ del raw_pubsub_cloudevent_output ['time' ]
314
+ del cloudevent ['time' ]
315
+
316
+ # Default to the service name, when the topic is not configured subscription's pushEndpoint.
317
+ raw_pubsub_cloudevent_output ['source' ] = "//pubsub.googleapis.com/"
318
+
319
+ assert cloudevent == raw_pubsub_cloudevent_output
0 commit comments