Skip to content

Commit f654cbd

Browse files
authored
Add request-id as part of all response headers (#1683)
* Add request-id as part of all response headers Signed-off-by: Alfredo Gutierrez <[email protected]> * changed `request-id` header key name for `X-Request-Id` to better comply with standard conventions, also, moved it to a const so changed in future is easier. Signed-off-by: Alfredo Gutierrez <[email protected]> * fix tests Signed-off-by: Alfredo Gutierrez <[email protected]> * fix tests Signed-off-by: Alfredo Gutierrez <[email protected]> --------- Signed-off-by: Alfredo Gutierrez <[email protected]>
1 parent 2f23fff commit f654cbd

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

packages/server/src/koaJsonRpc/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ const INTERNAL_ERROR = "INTERNAL ERROR";
4848
const INVALID_PARAMS_ERROR = "INVALID PARAMS ERROR";
4949
const INVALID_REQUEST = "INVALID REQUEST";
5050
const IP_RATE_LIMIT_EXCEEDED = "IP RATE LIMIT EXCEEDED";
51-
const JSON_RPC_ERROR = "JSON RPC ERROR"
51+
const JSON_RPC_ERROR = "JSON RPC ERROR";
5252
const METHOD_NOT_FOUND = "METHOD NOT FOUND";
53+
const REQUEST_ID_HEADER_NAME = "X-Request-Id";
5354

5455
const responseSuccessStatusCode = '200';
5556

@@ -97,6 +98,7 @@ export default class KoaJsonRpc {
9798
let body, result;
9899

99100
this.requestId = ctx.state.reqId;
101+
ctx.set(REQUEST_ID_HEADER_NAME, this.requestId);
100102

101103
if (this.token) {
102104
const headerToken = ctx.get('authorization').split(' ').pop();

packages/server/tests/integration/server.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,6 +1799,13 @@ class BaseTest {
17991799
});
18001800
}
18011801

1802+
static validRequestIdCheck(response) {
1803+
const requestIdHeaderName = "X-Request-Id".toLowerCase();
1804+
expect(response.headers, `Default response: headers should have '${requestIdHeaderName}' property`).to.have.property(requestIdHeaderName);
1805+
expect(response.headers[requestIdHeaderName], `Default response: 'headers[${requestIdHeaderName}]' should not be null`).not.to.be.null;
1806+
expect(response.headers[requestIdHeaderName], `Default response: 'headers[${requestIdHeaderName}]' should not be undefined`).not.to.be.undefined;
1807+
}
1808+
18021809
static validResponseCheck(response, options:any = {status: 200, statusText: 'OK'}) {
18031810
expect(response.status).to.eq(options.status);
18041811
expect(response.statusText).to.eq(options.statusText);
@@ -1815,6 +1822,7 @@ class BaseTest {
18151822
static defaultResponseChecks(response) {
18161823
BaseTest.validResponseCheck(response);
18171824
BaseTest.validCorsCheck(response);
1825+
BaseTest.validRequestIdCheck(response);
18181826
expect(response, "Default response: Should have 'data' property").to.have.property('data');
18191827
expect(response.data, "Default response: 'data' should have 'id' property").to.have.property('id');
18201828
expect(response.data, "Default response: 'data' should have 'jsonrpc' property").to.have.property('jsonrpc');
@@ -1825,6 +1833,7 @@ class BaseTest {
18251833
}
18261834

18271835
static errorResponseChecks(response, code, message, name?) {
1836+
BaseTest.validRequestIdCheck(response);
18281837
expect(response, "Error response: should have 'data' property").to.have.property('data');
18291838
expect(response.data, "Error response: 'data' should have 'id' property").to.have.property('id');
18301839
expect(response.data, "Error response: 'data' should have 'jsonrpc' property").to.have.property('jsonrpc');
@@ -1860,6 +1869,7 @@ class BaseTest {
18601869
}
18611870

18621871
static invalidRequestSpecError(response: any, code: number, message: string) {
1872+
BaseTest.validRequestIdCheck(response);
18631873
expect(response.status).to.eq(400);
18641874
expect(response.statusText).to.eq('Bad Request');
18651875
expect(response, "Default response: Should have 'data' property").to.have.property('data');

0 commit comments

Comments
 (0)