-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'tolgap-express-raw-body'
- Loading branch information
Showing
15 changed files
with
219 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { NestExpressApplication } from '@nestjs/platform-express'; | ||
import { Test } from '@nestjs/testing'; | ||
import { expect } from 'chai'; | ||
import * as request from 'supertest'; | ||
import { ExpressModule } from '../src/express.module'; | ||
|
||
describe('Raw body (Express Application)', () => { | ||
let app: NestExpressApplication; | ||
const body = '{ "amount":0.0 }'; | ||
|
||
beforeEach(async () => { | ||
const moduleFixture = await Test.createTestingModule({ | ||
imports: [ExpressModule], | ||
}).compile(); | ||
|
||
app = moduleFixture.createNestApplication<NestExpressApplication>( | ||
undefined, | ||
{ rawBody: true }, | ||
); | ||
}); | ||
|
||
it('should return exact post body', async () => { | ||
await app.init(); | ||
const response = await request(app.getHttpServer()) | ||
.post('/') | ||
.set('Content-Type', 'application/json') | ||
.send(body) | ||
.expect(201); | ||
|
||
expect(response.body).to.eql({ | ||
parsed: { | ||
amount: 0, | ||
}, | ||
raw: '{ "amount":0.0 }', | ||
}); | ||
}); | ||
|
||
it('should work if post body is empty', async () => { | ||
await app.init(); | ||
await request(app.getHttpServer()) | ||
.post('/') | ||
.set('Content-Type', 'application/json') | ||
.expect(201); | ||
}); | ||
|
||
afterEach(async () => { | ||
await app.close(); | ||
}); | ||
}); |
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,49 @@ | ||
import { NestFastifyApplication } from '@nestjs/platform-fastify'; | ||
import { Test } from '@nestjs/testing'; | ||
import { expect } from 'chai'; | ||
import * as request from 'supertest'; | ||
import { FastifyModule } from '../src/fastify.module'; | ||
|
||
describe('Raw body (Fastify Application)', () => { | ||
let app: NestFastifyApplication; | ||
const body = '{ "amount":0.0 }'; | ||
|
||
beforeEach(async () => { | ||
const moduleFixture = await Test.createTestingModule({ | ||
imports: [FastifyModule], | ||
}).compile(); | ||
|
||
app = moduleFixture.createNestApplication<NestFastifyApplication>(null, { | ||
rawBody: true, | ||
}); | ||
}); | ||
|
||
it('should return exact post body', async () => { | ||
await app.init(); | ||
const response = await request(app.getHttpServer()) | ||
.post('/') | ||
.set('Content-Type', 'application/json') | ||
.set('Accept', 'application/json') | ||
.send(body) | ||
.expect(201); | ||
|
||
expect(response.body).to.eql({ | ||
parsed: { | ||
amount: 0, | ||
}, | ||
raw: '{ "amount":0.0 }', | ||
}); | ||
}); | ||
|
||
it('should work if post body is empty', async () => { | ||
await app.init(); | ||
await request(app.getHttpServer()) | ||
.post('/') | ||
.set('Content-Type', 'application/json') | ||
.expect(201); | ||
}); | ||
|
||
afterEach(async () => { | ||
await app.close(); | ||
}); | ||
}); |
13 changes: 13 additions & 0 deletions
13
integration/nest-application/raw-body/src/express.controller.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,13 @@ | ||
import { Controller, Post, RawBodyRequest, Req } from '@nestjs/common'; | ||
import { Request } from 'express'; | ||
|
||
@Controller() | ||
export class ExpressController { | ||
@Post() | ||
getRawBody(@Req() req: RawBodyRequest<Request>) { | ||
return { | ||
parsed: req.body, | ||
raw: req.rawBody.toString(), | ||
}; | ||
} | ||
} |
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,7 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ExpressController } from './express.controller'; | ||
|
||
@Module({ | ||
controllers: [ExpressController], | ||
}) | ||
export class ExpressModule {} |
13 changes: 13 additions & 0 deletions
13
integration/nest-application/raw-body/src/fastify.controller.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,13 @@ | ||
import { Controller, Post, RawBodyRequest, Req } from '@nestjs/common'; | ||
import type { FastifyRequest } from 'fastify'; | ||
|
||
@Controller() | ||
export class FastifyController { | ||
@Post() | ||
getRawBody(@Req() req: RawBodyRequest<FastifyRequest>) { | ||
return { | ||
parsed: req.body, | ||
raw: req.rawBody.toString(), | ||
}; | ||
} | ||
} |
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,7 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { FastifyController } from './fastify.controller'; | ||
|
||
@Module({ | ||
controllers: [FastifyController], | ||
}) | ||
export class FastifyModule {} |
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,22 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"declaration": false, | ||
"noImplicitAny": false, | ||
"removeComments": true, | ||
"noLib": false, | ||
"emitDecoratorMetadata": true, | ||
"experimentalDecorators": true, | ||
"target": "es6", | ||
"sourceMap": true, | ||
"allowJs": true, | ||
"outDir": "./dist" | ||
}, | ||
"include": [ | ||
"src/**/*", | ||
"e2e/**/*" | ||
], | ||
"exclude": [ | ||
"node_modules" | ||
] | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './http-module.interface'; | ||
export * from './raw-body-request.interface'; |
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 @@ | ||
export type RawBodyRequest<T> = T & { rawBody?: Buffer }; |
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
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
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