@@ -170,6 +170,40 @@ describe("onMessagePublished", () => {
170170 expect ( json ) . to . deep . equal ( { hello : "world" } ) ;
171171 } ) ;
172172
173+ it ( "should construct a CloudEvent with the correct context" , async ( ) => {
174+ const publishTime = new Date ( ) . toISOString ( ) ;
175+ const message = {
176+ messageId : "uuid" ,
177+ data : Buffer . from ( JSON . stringify ( { hello : "world" } ) ) . toString ( "base64" ) ,
178+ publishTime,
179+ } ;
180+ const data : pubsub . MessagePublishedData = {
181+ message : message as any ,
182+ subscription : "projects/aProject/subscriptions/aSubscription" ,
183+ } ;
184+ const event : CloudEvent < pubsub . MessagePublishedData > = {
185+ specversion : "1.0" ,
186+ id : "uuid" ,
187+ time : publishTime ,
188+ type : "google.cloud.pubsub.topic.v1.messagePublished" ,
189+ source : "//pubsub.googleapis.com/projects/aProject/topics/topic" ,
190+ data,
191+ } ;
192+
193+ let receivedEvent : CloudEvent < pubsub . MessagePublishedData < any > > ;
194+ const func = pubsub . onMessagePublished ( "topic" , ( e ) => {
195+ receivedEvent = e ;
196+ } ) ;
197+
198+ await func ( event ) ;
199+
200+ expect ( receivedEvent . id ) . to . equal ( "uuid" ) ;
201+ expect ( receivedEvent . time ) . to . equal ( publishTime ) ;
202+ expect ( receivedEvent . type ) . to . equal ( "google.cloud.pubsub.topic.v1.messagePublished" ) ;
203+ expect ( receivedEvent . source ) . to . equal ( "//pubsub.googleapis.com/projects/aProject/topics/topic" ) ;
204+ expect ( receivedEvent . data . message . json ) . to . deep . equal ( { hello : "world" } ) ;
205+ } ) ;
206+
173207 // These tests pass if the transpiler works
174208 it ( "allows desirable syntax" , ( ) => {
175209 pubsub . onMessagePublished < string > (
@@ -193,4 +227,103 @@ describe("onMessagePublished", () => {
193227 ( event : CloudEvent < pubsub . MessagePublishedData > ) => undefined
194228 ) ;
195229 } ) ;
230+
231+ it ( "should not modify a CloudEvent that already has a context" , async ( ) => {
232+ const publishTime = new Date ( ) . toISOString ( ) ;
233+ const message = {
234+ messageId : "uuid" ,
235+ data : Buffer . from ( JSON . stringify ( { hello : "world" } ) ) . toString ( "base64" ) ,
236+ publishTime,
237+ } ;
238+ const data : pubsub . MessagePublishedData = {
239+ message : message as any ,
240+ subscription : "projects/aProject/subscriptions/aSubscription" ,
241+ } ;
242+ const existingContext = {
243+ eventId : "custom-id" ,
244+ timestamp : publishTime ,
245+ eventType : "custom.type" ,
246+ resource : "custom/resource" ,
247+ params : { } ,
248+ } ;
249+ const event : CloudEvent < pubsub . MessagePublishedData > = {
250+ specversion : "1.0" ,
251+ id : "uuid" ,
252+ time : publishTime ,
253+ type : "google.cloud.pubsub.topic.v1.messagePublished" ,
254+ source : "//pubsub.googleapis.com/projects/aProject/topics/topic" ,
255+ data,
256+ context : existingContext as any ,
257+ } ;
258+
259+ let receivedEvent : CloudEvent < pubsub . MessagePublishedData < any > > ;
260+ const func = pubsub . onMessagePublished ( "topic" , ( e ) => {
261+ receivedEvent = e ;
262+ } ) ;
263+
264+ await func ( event ) ;
265+
266+ expect ( receivedEvent . context ) . to . deep . equal ( existingContext ) ;
267+ } ) ;
268+
269+ it ( "should use GCLOUD_PROJECT as fallback for resource name" , async ( ) => {
270+ const publishTime = new Date ( ) . toISOString ( ) ;
271+ const message = {
272+ messageId : "uuid" ,
273+ data : Buffer . from ( JSON . stringify ( { hello : "world" } ) ) . toString ( "base64" ) ,
274+ publishTime,
275+ } ;
276+ const data : pubsub . MessagePublishedData = {
277+ message : message as any ,
278+ subscription : "projects/aProject/subscriptions/aSubscription" ,
279+ } ;
280+ const event : CloudEvent < pubsub . MessagePublishedData > = {
281+ specversion : "1.0" ,
282+ id : "uuid" ,
283+ time : publishTime ,
284+ type : "google.cloud.pubsub.topic.v1.messagePublished" ,
285+ source : "//pubsub.googleapis.com/topics/topic" , // Malformed source
286+ data,
287+ } ;
288+
289+ let receivedEvent : CloudEvent < pubsub . MessagePublishedData < any > > ;
290+ const func = pubsub . onMessagePublished ( "topic" , ( e ) => {
291+ receivedEvent = e ;
292+ } ) ;
293+
294+ await func ( event ) ;
295+
296+ expect ( receivedEvent . context . resource . name ) . to . equal ( "projects/aProject/topics/topic" ) ;
297+ } ) ;
298+
299+ it ( "should use 'unknown-project' as fallback for resource name" , async ( ) => {
300+ delete process . env . GCLOUD_PROJECT ;
301+ const publishTime = new Date ( ) . toISOString ( ) ;
302+ const message = {
303+ messageId : "uuid" ,
304+ data : Buffer . from ( JSON . stringify ( { hello : "world" } ) ) . toString ( "base64" ) ,
305+ publishTime,
306+ } ;
307+ const data : pubsub . MessagePublishedData = {
308+ message : message as any ,
309+ subscription : "projects/aProject/subscriptions/aSubscription" ,
310+ } ;
311+ const event : CloudEvent < pubsub . MessagePublishedData > = {
312+ specversion : "1.0" ,
313+ id : "uuid" ,
314+ time : publishTime ,
315+ type : "google.cloud.pubsub.topic.v1.messagePublished" ,
316+ source : "//pubsub.googleapis.com/topics/topic" , // Malformed source
317+ data,
318+ } ;
319+
320+ let receivedEvent : CloudEvent < pubsub . MessagePublishedData < any > > ;
321+ const func = pubsub . onMessagePublished ( "topic" , ( e ) => {
322+ receivedEvent = e ;
323+ } ) ;
324+
325+ await func ( event ) ;
326+
327+ expect ( receivedEvent . context . resource . name ) . to . equal ( "project/unknown-project/topics/topic" ) ;
328+ } ) ;
196329} ) ;
0 commit comments