-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add custom headers, allow response object
- Loading branch information
Insidexa
committed
Apr 12, 2020
1 parent
fbb9a0b
commit e30f01e
Showing
12 changed files
with
145 additions
and
49 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { HttpServer } from '@nestjs/common'; | ||
import { CustomHeader } from '@nestjs/core/router/router-response-controller'; | ||
|
||
export class JsonRpcResponseController { | ||
constructor( | ||
private applicationRef: HttpServer, | ||
) { | ||
} | ||
|
||
public setHeaders<TResponse = unknown>( | ||
response: TResponse, | ||
headers: CustomHeader[], | ||
) { | ||
headers.forEach(({ name, value }) => | ||
this.applicationRef.setHeader(response, name, value), | ||
); | ||
} | ||
|
||
public setStatus<TResponse = unknown>( | ||
response: TResponse, | ||
statusCode: number, | ||
) { | ||
this.applicationRef.status(response, statusCode); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import * as request from 'supertest'; | ||
import { JsonRpcModule, RpcHandler, RpcId, RpcMethod, RpcPayload, RpcVersion, IRpcHandler } from '../src'; | ||
import { Header } from '@nestjs/common'; | ||
|
||
@RpcHandler({ | ||
method: 'test', | ||
}) | ||
export class TestHandler implements IRpcHandler<any> { | ||
@Header('Handler-Name', TestHandler.name) | ||
public async invoke( | ||
@RpcPayload() payload: any, | ||
@RpcVersion() version: string, | ||
@RpcMethod() method: string, | ||
@RpcId() id: any, | ||
) { | ||
return payload; | ||
} | ||
} | ||
|
||
describe('Test json rpc custom headers', () => { | ||
let app; | ||
|
||
beforeEach(async () => { | ||
const moduleFixture: TestingModule = await Test.createTestingModule({ | ||
imports: [ | ||
JsonRpcModule.forRoot({ | ||
path: '/rpc', | ||
}), | ||
], | ||
providers: [TestHandler], | ||
}).compile(); | ||
|
||
app = moduleFixture.createNestApplication(); | ||
await app.init(); | ||
}); | ||
|
||
describe('positive', () => { | ||
it('has single custom header', async () => { | ||
const { header } = await request(app.getHttpServer()) | ||
.post('/rpc') | ||
.send({ jsonrpc: '2.0', id: 1, params: { field: 1 }, method: 'test' }) | ||
.expect(200); | ||
|
||
const handlerNameHeader = header['handler-name']; | ||
|
||
expect(handlerNameHeader).not.toBeUndefined(); | ||
}); | ||
|
||
it('custom header contains expected value', async () => { | ||
const { header } = await request(app.getHttpServer()) | ||
.post('/rpc') | ||
.send({ jsonrpc: '2.0', id: 1, params: { field: 1 }, method: 'test' }) | ||
.expect(200); | ||
|
||
const handlerNameHeader = header['handler-name']; | ||
|
||
expect(handlerNameHeader).toEqual(TestHandler.name); | ||
}); | ||
}); | ||
}); |
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 was deleted.
Oops, something went wrong.