-
Notifications
You must be signed in to change notification settings - Fork 1
Handle multipart/form-data webhooks #241
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
Open
asein-sinch
wants to merge
2
commits into
v1.4
Choose a base branch
from
bugfix/handle-multipart-form-data-webhooks
base: v1.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
193 changes: 193 additions & 0 deletions
193
packages/fax/tests/rest/v3/webhooks/callbacks-webhook.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| import { Fax, FaxCallbackWebhooks } from '../../../../src'; | ||
|
|
||
| describe('Fax Callback Webhook', () => { | ||
| let callbackWebhooks: FaxCallbackWebhooks; | ||
|
|
||
| const EVENT_TIME_AS_STRING = '2026-04-09T21:33:46Z'; | ||
| const EVENT_TIME_AS_DATE = new Date(EVENT_TIME_AS_STRING); | ||
| const CREATE_TIME_AS_STRING = '2026-04-08T14:23:48Z'; | ||
| const CREATE_TIME_AS_DATE = new Date(CREATE_TIME_AS_STRING); | ||
| const COMPLETED_TIME_AS_STRING = '2026-04-08T14:25:00Z'; | ||
| const COMPLETED_TIME_AS_DATE = new Date(COMPLETED_TIME_AS_STRING); | ||
|
|
||
| const FAX_OBJECT = { | ||
| id: '01KNPQW6SGZKAZEX8ZX7MCN559', | ||
| direction: 'INBOUND', | ||
| from: '+16179216545', | ||
| to: '+17818510006', | ||
| numberOfPages: 1, | ||
| status: 'COMPLETED', | ||
| createTime: CREATE_TIME_AS_STRING, | ||
| completedTime: COMPLETED_TIME_AS_STRING, | ||
| projectId: '37b62a7b-0177-429a-bb0b-e10f848de0b8', | ||
| serviceId: '01HQK89SYWBCJK1DMYD3QAW2DW', | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| callbackWebhooks = new FaxCallbackWebhooks(); | ||
| }); | ||
|
|
||
| it('should parse an INCOMING_FAX event from an object payload', () => { | ||
| const payload = { | ||
| event: 'INCOMING_FAX', | ||
| eventTime: EVENT_TIME_AS_STRING, | ||
| fax: { ...FAX_OBJECT }, | ||
| }; | ||
| const result = callbackWebhooks.parseEvent(payload) as Fax.IncomingFaxEvent; | ||
| expect(result.event).toBe('INCOMING_FAX'); | ||
| expect(result.eventTime).toStrictEqual(EVENT_TIME_AS_DATE); | ||
| expect(result.fax).toBeDefined(); | ||
| expect(result.fax!.id).toBe('01KNPQW6SGZKAZEX8ZX7MCN559'); | ||
| expect(result.fax!.createTime).toStrictEqual(CREATE_TIME_AS_DATE); | ||
| }); | ||
|
|
||
| it('should parse a FAX_COMPLETED event from an object payload', () => { | ||
| const payload = { | ||
| event: 'FAX_COMPLETED', | ||
| eventTime: EVENT_TIME_AS_STRING, | ||
| fax: { ...FAX_OBJECT }, | ||
| }; | ||
| const result = callbackWebhooks.parseEvent(payload) as Fax.FaxCompletedEvent; | ||
| expect(result.event).toBe('FAX_COMPLETED'); | ||
| expect(result.eventTime).toStrictEqual(EVENT_TIME_AS_DATE); | ||
| expect(result.fax).toBeDefined(); | ||
| expect(result.fax!.id).toBe('01KNPQW6SGZKAZEX8ZX7MCN559'); | ||
| expect(result.fax!.createTime).toStrictEqual(CREATE_TIME_AS_DATE); | ||
| }); | ||
|
|
||
| it('should parse an INCOMING_FAX event from a JSON string', () => { | ||
| const payload = JSON.stringify({ | ||
| event: 'INCOMING_FAX', | ||
| eventTime: EVENT_TIME_AS_STRING, | ||
| fax: { ...FAX_OBJECT }, | ||
| }); | ||
| const result = callbackWebhooks.parseEvent(payload) as Fax.IncomingFaxEvent; | ||
| expect(result.event).toBe('INCOMING_FAX'); | ||
| expect(result.eventTime).toStrictEqual(EVENT_TIME_AS_DATE); | ||
| expect(result.fax!.id).toBe('01KNPQW6SGZKAZEX8ZX7MCN559'); | ||
| expect(result.fax!.createTime).toStrictEqual(CREATE_TIME_AS_DATE); | ||
| }); | ||
|
|
||
| it('should parse a FAX_COMPLETED event from a JSON string', () => { | ||
| const payload = JSON.stringify({ | ||
| event: 'FAX_COMPLETED', | ||
| eventTime: EVENT_TIME_AS_STRING, | ||
| fax: { ...FAX_OBJECT }, | ||
| }); | ||
| const result = callbackWebhooks.parseEvent(payload) as Fax.FaxCompletedEvent; | ||
| expect(result.event).toBe('FAX_COMPLETED'); | ||
| expect(result.eventTime).toStrictEqual(EVENT_TIME_AS_DATE); | ||
| expect(result.fax!.id).toBe('01KNPQW6SGZKAZEX8ZX7MCN559'); | ||
| expect(result.fax!.createTime).toStrictEqual(CREATE_TIME_AS_DATE); | ||
| }); | ||
|
|
||
| it('should parse an INCOMING_FAX event from a multipart/form-data body with CRLF', () => { | ||
|
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. Hi, @asein-sinch! |
||
| const boundary = '----Boundary123'; | ||
| const faxJson = JSON.stringify(FAX_OBJECT); | ||
| const multipartBody = [ | ||
| `--${boundary}`, | ||
| 'Content-Disposition: form-data; name="event"', | ||
| '', | ||
| 'INCOMING_FAX', | ||
| `--${boundary}`, | ||
| 'Content-Disposition: form-data; name="eventTime"', | ||
| '', | ||
| EVENT_TIME_AS_STRING, | ||
| `--${boundary}`, | ||
| 'Content-Disposition: form-data; name="fax"', | ||
| 'Content-Type: application/json', | ||
| '', | ||
| faxJson, | ||
| `--${boundary}--`, | ||
| ].join('\r\n'); | ||
|
|
||
| const result = callbackWebhooks.parseEvent(multipartBody) as Fax.IncomingFaxEvent; | ||
| expect(result.event).toBe('INCOMING_FAX'); | ||
| expect(result.eventTime).toStrictEqual(EVENT_TIME_AS_DATE); | ||
| expect(result.fax).toBeDefined(); | ||
| expect(result.fax!.id).toBe('01KNPQW6SGZKAZEX8ZX7MCN559'); | ||
| expect(result.fax!.createTime).toStrictEqual(CREATE_TIME_AS_DATE); | ||
| expect(result.fax!.completedTime).toStrictEqual(COMPLETED_TIME_AS_DATE); | ||
| }); | ||
|
|
||
| it('should parse a FAX_COMPLETED event from a multipart/form-data body with CRLF', () => { | ||
| const boundary = '----Boundary123'; | ||
| const faxJson = JSON.stringify(FAX_OBJECT); | ||
| const multipartBody = [ | ||
| `--${boundary}`, | ||
| 'Content-Disposition: form-data; name="event"', | ||
| '', | ||
| 'FAX_COMPLETED', | ||
| `--${boundary}`, | ||
| 'Content-Disposition: form-data; name="eventTime"', | ||
| '', | ||
| EVENT_TIME_AS_STRING, | ||
| `--${boundary}`, | ||
| 'Content-Disposition: form-data; name="fax"', | ||
| 'Content-Type: application/json', | ||
| '', | ||
| faxJson, | ||
| `--${boundary}--`, | ||
| ].join('\r\n'); | ||
|
|
||
| const result = callbackWebhooks.parseEvent(multipartBody) as Fax.FaxCompletedEvent; | ||
| expect(result.event).toBe('FAX_COMPLETED'); | ||
| expect(result.eventTime).toStrictEqual(EVENT_TIME_AS_DATE); | ||
| expect(result.fax).toBeDefined(); | ||
| expect(result.fax!.id).toBe('01KNPQW6SGZKAZEX8ZX7MCN559'); | ||
| expect(result.fax!.createTime).toStrictEqual(CREATE_TIME_AS_DATE); | ||
| expect(result.fax!.completedTime).toStrictEqual(COMPLETED_TIME_AS_DATE); | ||
| }); | ||
|
|
||
| it('should parse an event without eventTime', () => { | ||
| const payload = { | ||
| event: 'INCOMING_FAX', | ||
| fax: { ...FAX_OBJECT }, | ||
| }; | ||
| const result = callbackWebhooks.parseEvent(payload) as Fax.IncomingFaxEvent; | ||
| expect(result.event).toBe('INCOMING_FAX'); | ||
| expect(result.eventTime).toBeUndefined(); | ||
| expect(result.fax!.id).toBe('01KNPQW6SGZKAZEX8ZX7MCN559'); | ||
| }); | ||
|
|
||
| it('should parse an event using the static method', () => { | ||
| const payload = { | ||
| event: 'INCOMING_FAX', | ||
| eventTime: EVENT_TIME_AS_STRING, | ||
| fax: { ...FAX_OBJECT }, | ||
| }; | ||
| const result = FaxCallbackWebhooks.parseEvent(payload) as Fax.IncomingFaxEvent; | ||
| expect(result.event).toBe('INCOMING_FAX'); | ||
| expect(result.eventTime).toStrictEqual(EVENT_TIME_AS_DATE); | ||
| expect(result.fax!.id).toBe('01KNPQW6SGZKAZEX8ZX7MCN559'); | ||
| }); | ||
|
|
||
| it('should always return true for authentication validation', () => { | ||
| const result = callbackWebhooks.validateAuthenticationHeader({}, '', '/fax', 'POST'); | ||
| expect(result).toBeTruthy(); | ||
| }); | ||
|
|
||
| it('should throw an error when the event type is unknown', () => { | ||
| const payload = { | ||
| event: 'UNKNOWN_EVENT', | ||
| eventTime: EVENT_TIME_AS_STRING, | ||
| }; | ||
| expect(() => callbackWebhooks.parseEvent(payload)).toThrow('Unknown Fax event: UNKNOWN_EVENT'); | ||
| }); | ||
|
|
||
| it('should throw an error when the event property is missing', () => { | ||
| const payload = { | ||
| eventTime: EVENT_TIME_AS_STRING, | ||
| }; | ||
| expect(() => callbackWebhooks.parseEvent(payload)).toThrow('Unknown Fax event'); | ||
| }); | ||
|
|
||
| it('should throw an error when parsing an invalid JSON string', () => { | ||
| expect(() => callbackWebhooks.parseEvent('not valid json')).toThrow(); | ||
| }); | ||
|
|
||
| it('should throw an error when parsing a multipart body with no detectable boundary', () => { | ||
| // A string starting with "--" but with no actual content | ||
| expect(() => callbackWebhooks.parseEvent('--\r\n')).toThrow('Unable to detect multipart boundary from the body'); | ||
| }); | ||
| }); | ||
71 changes: 71 additions & 0 deletions
71
packages/fax/tests/rest/v3/webhooks/webhooks-events.steps.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import { Given, Then, When } from '@cucumber/cucumber'; | ||
| import { FaxCallbackWebhooks, Fax } from '../../../../src'; | ||
| import assert from 'assert'; | ||
|
|
||
| let faxCallbackWebhook: FaxCallbackWebhooks; | ||
| let rawEvent: string; | ||
| let event: Fax.FaxWebhookEventParsed; | ||
|
|
||
| Given('the Fax Webhooks handler is available', function () { | ||
| faxCallbackWebhook = new FaxCallbackWebhooks(); | ||
| }); | ||
|
|
||
| When('I send a request to trigger the INCOMING_FAX event with the "{}" content type', async (contentType) => { | ||
| const response = await fetch(`http://localhost:3012/webhooks/fax/incoming-fax/${contentType}`); | ||
| rawEvent = await response.text(); | ||
| event = faxCallbackWebhook.parseEvent(rawEvent); | ||
| }); | ||
|
|
||
| Then('the event describes an INCOMING_FAX event with the application-json content type', () => { | ||
| assert.equal(event.event, 'INCOMING_FAX'); | ||
| assert.deepEqual(event.eventTime, new Date('2024-06-06T14:42:35.000Z')); | ||
| const fax = event.fax!; | ||
| assert.equal(fax.id, '01W4FFL35P4NC4K35CR3P35TOP1'); | ||
| assert.equal(fax.direction, 'INBOUND'); | ||
| assert.equal(fax.from, '+16179216545'); | ||
| assert.equal(fax.to, '+17818510000'); | ||
| assert.equal(fax.numberOfPages, 1); | ||
| assert.equal(fax.status, 'COMPLETED'); | ||
| assert.equal(fax.headerTimeZone, 'UTC'); | ||
| assert.equal(fax.retryDelaySeconds, 60); | ||
| assert.equal(fax.resolution, 'FINE'); | ||
| assert.equal(fax.callbackUrl, 'https://my.callback.url/fax'); | ||
| assert.equal(fax.callbackUrlContentType, 'application/json'); | ||
| assert.equal(fax.projectId, '123coffee-dada-beef-cafe-baadc0de5678'); | ||
| assert.equal(fax.serviceId, '01W4FFL35P4NC4K35FAXSERVICE'); | ||
| assert.equal(fax.price?.amount, '0.045'); | ||
| assert.equal(fax.price?.currencyCode, 'USD'); | ||
| assert.equal(fax.maxRetries, 0); | ||
| assert.deepEqual(fax.createTime, new Date('2026-06-06T14:42:22Z')); | ||
| assert.deepEqual(fax.completedTime, new Date('2026-06-06T14:42:22Z')); | ||
| assert.equal(fax.headerText, ''); | ||
| assert.equal(fax.headerPageNumbers, true); | ||
| assert.equal(fax.hasFile, true); | ||
| }); | ||
|
|
||
| Then('the event describes an INCOMING_FAX event with the multipart-formdata content type', () => { | ||
| assert.equal(event.event, 'INCOMING_FAX'); | ||
| assert.deepEqual(event.eventTime, new Date('2024-06-06T14:42:46.000Z')); | ||
| const fax = event.fax!; | ||
| assert.equal(fax.id, '01W4FFL35P4NC4K35CR3P35TOP2'); | ||
| assert.equal(fax.from, '+16179216545'); | ||
| assert.equal(fax.to, '+17818510000'); | ||
| assert.equal(fax.numberOfPages, 0); | ||
| assert.equal(fax.status, 'FAILURE'); | ||
| assert.equal(fax.headerTimeZone, 'UTC'); | ||
| assert.equal(fax.retryDelaySeconds, 60); | ||
| assert.equal(fax.resolution, 'FINE'); | ||
| assert.equal(fax.callbackUrl, 'https://my.callback.url/fax'); | ||
| assert.equal(fax.callbackUrlContentType, 'multipart/form-data'); | ||
| assert.equal(fax.errorType, 'FAX_ERROR'); | ||
| assert.equal(fax.errorMessage, 'No fax tone detected'); | ||
| assert.equal(fax.errorCode, 132); | ||
| assert.equal(fax.projectId, '123coffee-dada-beef-cafe-baadc0de5678'); | ||
| assert.equal(fax.serviceId, '01W4FFL35P4NC4K35FAXSERVICE'); | ||
| assert.equal(fax.maxRetries, 0); | ||
| assert.deepEqual(fax.createTime, new Date('2026-06-06T14:42:48Z')); | ||
| assert.deepEqual(fax.completedTime, new Date('2026-06-06T14:42:48Z')); | ||
| assert.equal(fax.headerText, ''); | ||
| assert.equal(fax.headerPageNumbers, true); | ||
| assert.equal(fax.hasFile, false); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, @asein-sinch!
Should we trim
eventBodybefore passing it toparseMultipartFormData()? The ^ regex inside the method will fail to match if the body has leading whitespace, even though thetrimStart()check in the condition already handles it. Passing the trimmed string keeps the two in sync:eventBody = this.parseMultipartFormData(eventBody.trimStart());What do you think?