-
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 1 commit
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,40 @@ describe("onMessagePublished", () => { | |
| expect(json).to.deep.equal({ hello: "world" }); | ||
| }); | ||
|
|
||
| it("should construct a CloudEvent with the correct context", async () => { | ||
| 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/projects/aProject/topics/topic", | ||
| data, | ||
| }; | ||
|
|
||
| let receivedEvent: CloudEvent<pubsub.MessagePublishedData<any>>; | ||
| const func = pubsub.onMessagePublished("topic", (e) => { | ||
| receivedEvent = e; | ||
| }); | ||
|
|
||
| await func(event); | ||
|
|
||
| expect(receivedEvent.id).to.equal("uuid"); | ||
| expect(receivedEvent.time).to.equal(publishTime); | ||
| expect(receivedEvent.type).to.equal("google.cloud.pubsub.topic.v1.messagePublished"); | ||
| expect(receivedEvent.source).to.equal("//pubsub.googleapis.com/projects/aProject/topics/topic"); | ||
| expect(receivedEvent.data.message.json).to.deep.equal({ hello: "world" }); | ||
| }); | ||
shettyvarun268 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // These tests pass if the transpiler works | ||
| it("allows desirable syntax", () => { | ||
| pubsub.onMessagePublished<string>( | ||
|
|
@@ -193,4 +227,103 @@ describe("onMessagePublished", () => { | |
| (event: CloudEvent<pubsub.MessagePublishedData>) => undefined | ||
| ); | ||
| }); | ||
|
|
||
| it("should not modify a CloudEvent that already has a context", async () => { | ||
shettyvarun268 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 existingContext = { | ||
| eventId: "custom-id", | ||
| timestamp: publishTime, | ||
| eventType: "custom.type", | ||
| resource: "custom/resource", | ||
| params: {}, | ||
| }; | ||
| 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, | ||
| context: existingContext as any, | ||
| }; | ||
|
|
||
| let receivedEvent: CloudEvent<pubsub.MessagePublishedData<any>>; | ||
| const func = pubsub.onMessagePublished("topic", (e) => { | ||
| receivedEvent = e; | ||
| }); | ||
|
|
||
| await func(event); | ||
|
|
||
| expect(receivedEvent.context).to.deep.equal(existingContext); | ||
| }); | ||
|
|
||
| it("should use GCLOUD_PROJECT as fallback for resource name", async () => { | ||
| 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/aProject/topics/topic"); | ||
| }); | ||
|
|
||
| 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("project/unknown-project/topics/topic"); | ||
shettyvarun268 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.