-
-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(webhooks): simple raw body setup
utility and documentation for setting up raw body parsing inside of a NestJS application fix #131
- Loading branch information
1 parent
84ed250
commit 6382c05
Showing
4 changed files
with
166 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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
84 changes: 84 additions & 0 deletions
84
packages/webhooks/src/tests/raw-body-and-json-middleware.spec.ts
This file contains 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,84 @@ | ||
import { | ||
Controller, | ||
MiddlewareConsumer, | ||
Module, | ||
NestModule, | ||
Post, | ||
Request, | ||
RequestMethod, | ||
} from '@nestjs/common'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import * as request from 'supertest'; | ||
import { JsonBodyMiddleware, RawBodyMiddleware } from '../webhooks.middleware'; | ||
import { applyRawBodyOnlyTo } from '../webhooks.utilities'; | ||
|
||
const testBodyFn = jest.fn(); | ||
|
||
const expectedBody = { message: 'hello' }; | ||
const expectedRawBody = Buffer.from(JSON.stringify(expectedBody)); | ||
@Controller('raw') | ||
class WebhookController { | ||
@Post() | ||
webhook(@Request() request) { | ||
testBodyFn(request.body); | ||
} | ||
} | ||
|
||
@Controller('api') | ||
class ApiController { | ||
@Post() | ||
body(@Request() request) { | ||
testBodyFn(request.body); | ||
} | ||
} | ||
|
||
@Module({ | ||
controllers: [WebhookController, ApiController], | ||
}) | ||
class TestAppModule implements NestModule { | ||
configure(consumer: MiddlewareConsumer) { | ||
applyRawBodyOnlyTo(consumer, { | ||
method: RequestMethod.ALL, | ||
path: 'raw', | ||
}); | ||
} | ||
} | ||
|
||
describe('Webhooks Raw Body And JSON middleware', () => { | ||
let app; | ||
describe('only for certain endpoitns util', () => { | ||
beforeEach(async () => { | ||
testBodyFn.mockReset(); | ||
const moduleFixture: TestingModule = await Test.createTestingModule({ | ||
imports: [TestAppModule, RawBodyMiddleware, JsonBodyMiddleware], | ||
}).compile(); | ||
|
||
app = moduleFixture.createNestApplication(undefined, { | ||
bodyParser: false, | ||
}); | ||
await app.init(); | ||
}); | ||
|
||
it('should make the raw body available on the correct request property', () => { | ||
return request(app.getHttpServer()) | ||
.post('/raw') | ||
.send(expectedBody) | ||
.expect(201) | ||
.then(() => { | ||
expect(testBodyFn).toHaveBeenCalledTimes(1); | ||
expect(testBodyFn).toHaveBeenCalledWith(expectedRawBody); | ||
}); | ||
}); | ||
|
||
it('should use body parser json on all other routes', () => { | ||
return request(app.getHttpServer()) | ||
.post('/api') | ||
.send(expectedBody) | ||
.expect(201) | ||
.then(() => { | ||
expect(testBodyFn).toHaveBeenCalledTimes(1); | ||
expect(testBodyFn).toHaveBeenCalledWith(expectedBody); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains 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