Skip to content

Commit

Permalink
fix(google-pubsub): Make event data optional
Browse files Browse the repository at this point in the history
  • Loading branch information
gnarea committed Aug 22, 2023
1 parent 868e557 commit 6e21f20
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
18 changes: 11 additions & 7 deletions src/lib/googlePubSub.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,16 +305,20 @@ describe('convertGooglePubSubMessage', () => {
expect(event.data).toMatchObject(EVENT.data!);
});

test('Message should be refused if data is missing', () => {
const invalidRequestBody = {
test('Should be empty if data is missing', () => {
const body = {
...requestBody,
message: { ...requestBody.message, data: undefined },

message: {
...requestBody.message,
data: undefined,
},
};
const invalidRequestBodySerialised = jsonSerialise(invalidRequestBody);
const bodySerialised = jsonSerialise(body);

expect(() =>
convertGooglePubSubMessage(headers, invalidRequestBodySerialised),
).toThrowWithMessage(Error, 'Request body is not a valid PubSub message');
const event = convertGooglePubSubMessage(headers, bodySerialised);

expect(event.data).toBeUndefined();
});
});

Expand Down
5 changes: 3 additions & 2 deletions src/lib/googlePubSub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const pubSubBody = {
publishTime: { type: 'string' },
},

required: ['attributes', 'data', 'messageId', 'publishTime'],
required: ['attributes', 'messageId', 'publishTime'],
},
},

Expand Down Expand Up @@ -125,6 +125,7 @@ export function convertGooglePubSubMessage(_headers: Headers, body: Buffer): Clo

if (isPubSubBody(bodyJson)) {
const { message } = bodyJson;
const data = message.data === undefined ? undefined : Buffer.from(message.data!, 'base64');
const extensionAttributes = filterExtensionAttributes(message.attributes);
return new CloudEvent<Buffer>({
specversion: message.attributes.specversion,
Expand All @@ -135,7 +136,7 @@ export function convertGooglePubSubMessage(_headers: Headers, body: Buffer): Clo
time: message.publishTime,
datacontenttype: message.attributes.datacontenttype,
dataschema: message.attributes.dataschema,
data: Buffer.from(message.data, 'base64'),
data,
...extensionAttributes,
});
}
Expand Down

0 comments on commit 6e21f20

Please sign in to comment.