-
Notifications
You must be signed in to change notification settings - Fork 221
Implement V1 compat API for PubSub #1782
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 3 commits
8f1dcda
30e6e10
7222aad
a50e328
1de2120
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -170,6 +170,45 @@ | |
| expect(json).to.deep.equal({ hello: "world" }); | ||
| }); | ||
|
|
||
| it("should construct a CloudEvent with the correct context and message", async () => { | ||
| const publishTime = new Date().toISOString(); | ||
| const messagePayload = { | ||
| messageId: "uuid", | ||
| data: Buffer.from(JSON.stringify({ hello: "world" })).toString("base64"), | ||
| publishTime, | ||
| }; | ||
| const data: pubsub.MessagePublishedData = { | ||
| message: messagePayload as any, | ||
| subscription: "projects/aProject/subscriptions/aSubscription", | ||
| }; | ||
| const event: CloudEvent<pubsub.MessagePublishedData> = { | ||
| specversion: "1.0", | ||
| id: "uuid", | ||
| time: publishTime, | ||
| type: "google.cloud.pubsub.topic.v1.messagePublished", | ||
| source: "//pubsub.googleapis.com/projects/aProject/topics/topic", | ||
| data, | ||
| }; | ||
|
|
||
| let destructuredMessage: pubsub.Message<any>; | ||
| let context: any; | ||
| const func = pubsub.onMessagePublished("topic", (e) => { | ||
| ({ message: destructuredMessage, context } = e as any); | ||
| }); | ||
|
|
||
| await func(event); | ||
|
|
||
| expect(destructuredMessage.json).to.deep.equal({ hello: "world" }); | ||
| expect(context).to.exist; | ||
| expect(context.eventId).to.equal("uuid"); | ||
| expect(context.timestamp).to.equal(publishTime); | ||
| expect(context.eventType).to.equal("google.cloud.pubsub.topic.v1.messagePublished"); | ||
| expect(context.resource).to.deep.equal({ | ||
| service: "pubsub.googleapis.com", | ||
| name: "projects/aProject/topics/topic", | ||
| }); | ||
| }); | ||
|
|
||
| // These tests pass if the transpiler works | ||
| it("allows desirable syntax", () => { | ||
| pubsub.onMessagePublished<string>( | ||
|
|
@@ -193,4 +232,40 @@ | |
| (event: CloudEvent<pubsub.MessagePublishedData>) => undefined | ||
| ); | ||
| }); | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| it("should use 'unknown-project' as fallback for resource name", async () => { | ||
| delete process.env.GCLOUD_PROJECT; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this necessary?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I think we should keep this. In case we receive a malformed event where the EventID is not parsed, this would ensure that the function does not crash. Also considering GCLOUD_PROJECT is an environmental variable, there could be a case when its not populated (?). What do you think? |
||
| const publishTime = new Date().toISOString(); | ||
| const message = { | ||
| messageId: "uuid", | ||
| data: Buffer.from(JSON.stringify({ hello: "world" })).toString("base64"), | ||
| publishTime, | ||
| }; | ||
| const data: pubsub.MessagePublishedData = { | ||
| message: message as any, | ||
| subscription: "projects/aProject/subscriptions/aSubscription", | ||
| }; | ||
| const event: CloudEvent<pubsub.MessagePublishedData> = { | ||
| specversion: "1.0", | ||
| id: "uuid", | ||
| time: publishTime, | ||
| type: "google.cloud.pubsub.topic.v1.messagePublished", | ||
| source: "//pubsub.googleapis.com/topics/topic", // Malformed source | ||
| data, | ||
| }; | ||
|
|
||
| let receivedEvent: CloudEvent<pubsub.MessagePublishedData<any>>; | ||
| const func = pubsub.onMessagePublished("topic", (e) => { | ||
| receivedEvent = e; | ||
| }); | ||
|
|
||
| await func(event); | ||
|
|
||
| expect(receivedEvent.context.resource.name).to.equal("projects/unknown-project/topics/topic"); | ||
| }); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.