@@ -100,6 +100,14 @@ def raw_pubsub_request():
100
100
}
101
101
102
102
103
+ @pytest .fixture
104
+ def raw_pubsub_request_noattributes ():
105
+ return {
106
+ "subscription" : "projects/sample-project/subscriptions/gcf-test-sub" ,
107
+ "message" : {"data" : "eyJmb28iOiJiYXIifQ==" , "messageId" : "1215011316659232" },
108
+ }
109
+
110
+
103
111
@pytest .fixture
104
112
def marshalled_pubsub_request ():
105
113
return {
@@ -121,6 +129,27 @@ def marshalled_pubsub_request():
121
129
}
122
130
123
131
132
+ @pytest .fixture
133
+ def marshalled_pubsub_request_noattr ():
134
+ return {
135
+ "data" : {
136
+ "@type" : "type.googleapis.com/google.pubsub.v1.PubsubMessage" ,
137
+ "data" : "eyJmb28iOiJiYXIifQ==" ,
138
+ "attributes" : {},
139
+ },
140
+ "context" : {
141
+ "eventId" : "1215011316659232" ,
142
+ "eventType" : "google.pubsub.topic.publish" ,
143
+ "resource" : {
144
+ "name" : "projects/sample-project/topics/gcf-test" ,
145
+ "service" : "pubsub.googleapis.com" ,
146
+ "type" : "type.googleapis.com/google.pubsub.v1.PubsubMessage" ,
147
+ },
148
+ "timestamp" : "2021-04-17T07:21:18.249Z" ,
149
+ },
150
+ }
151
+
152
+
124
153
@pytest .fixture
125
154
def raw_pubsub_cloud_event_output (marshalled_pubsub_request ):
126
155
event = PUBSUB_CLOUD_EVENT .copy ()
@@ -343,6 +372,24 @@ def test_marshal_background_event_data_without_topic_in_path(
343
372
assert payload == marshalled_pubsub_request
344
373
345
374
375
+ def test_marshal_background_event_data_without_topic_in_path_no_attr (
376
+ raw_pubsub_request_noattributes , marshalled_pubsub_request_noattr
377
+ ):
378
+ req = flask .Request .from_values (
379
+ json = raw_pubsub_request_noattributes , path = "/myfunc/"
380
+ )
381
+ payload = event_conversion .marshal_background_event_data (req )
382
+
383
+ # Remove timestamps as they get generates on the fly
384
+ del marshalled_pubsub_request_noattr ["context" ]["timestamp" ]
385
+ del payload ["context" ]["timestamp" ]
386
+
387
+ # Resource name is set to empty string when it cannot be parsed from the request path
388
+ marshalled_pubsub_request_noattr ["context" ]["resource" ]["name" ] = ""
389
+
390
+ assert payload == marshalled_pubsub_request_noattr
391
+
392
+
346
393
def test_marshal_background_event_data_with_topic_path (
347
394
raw_pubsub_request , marshalled_pubsub_request
348
395
):
0 commit comments