-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: reality id in response headers is now fixed
- Loading branch information
1 parent
dff89c9
commit 62c1abc
Showing
7 changed files
with
100 additions
and
50 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 was deleted.
Oops, something went wrong.
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,45 @@ | ||
import { | ||
OICD_CLAIM_UPN, | ||
PolarisGraphQLContext, | ||
REALITY_ID, | ||
REQUEST_ID, | ||
} from '@enigmatis/polaris-common'; | ||
import { PolarisGraphQLLogger } from '@enigmatis/polaris-graphql-logger'; | ||
import { | ||
GraphQLRequestContext, | ||
GraphQLRequestListener, | ||
ValueOrPromise, | ||
WithRequired, | ||
} from 'apollo-server-plugin-base'; | ||
|
||
export class ResponseHeadersListener implements GraphQLRequestListener<PolarisGraphQLContext> { | ||
public readonly logger: any; | ||
|
||
constructor(logger: PolarisGraphQLLogger) { | ||
this.logger = logger; | ||
} | ||
|
||
public willSendResponse( | ||
requestContext: WithRequired< | ||
GraphQLRequestContext<PolarisGraphQLContext>, | ||
'metrics' | 'response' | ||
>, | ||
): ValueOrPromise<void> { | ||
const { context } = requestContext; | ||
if (context.responseHeaders) { | ||
const requestId = context.responseHeaders.requestId; | ||
if (requestId) { | ||
requestContext.response.http?.headers.set(REQUEST_ID, requestId); | ||
} | ||
const realityId = context.responseHeaders.realityId; | ||
if (realityId !== undefined && Number.isInteger(realityId)) { | ||
requestContext.response.http?.headers.set(REALITY_ID, String(realityId)); | ||
} | ||
const upn = context.responseHeaders.upn; | ||
if (upn) { | ||
requestContext.response.http?.headers.set(OICD_CLAIM_UPN, upn); | ||
} | ||
this.logger.debug('response headers were set to response'); | ||
} | ||
} | ||
} |
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,49 @@ | ||
import { OICD_CLAIM_UPN, REALITY_ID, REQUEST_ID } from '@enigmatis/polaris-common'; | ||
import axios from 'axios'; | ||
import { PolarisServer } from '../../../src'; | ||
import * as polarisProperties from '../server-without-connection/resources/polaris-properties.json'; | ||
import { initializeDatabase } from '../server/dal/data-initalizer'; | ||
import { startTestServer, stopTestServer } from '../server/test-server'; | ||
|
||
let polarisServer: PolarisServer; | ||
|
||
beforeEach(async () => { | ||
polarisServer = await startTestServer(); | ||
await initializeDatabase(); | ||
}); | ||
|
||
afterEach(() => { | ||
return stopTestServer(polarisServer); | ||
}); | ||
|
||
describe('response headers tests', () => { | ||
const url = `http://localhost:${polarisProperties.port}/${polarisProperties.version}/graphql`; | ||
const query = '{ allBooks { title } }'; | ||
|
||
test('response headers are set', async () => { | ||
const result = await axios.post(url, { query }); | ||
expect(result.headers[REQUEST_ID]).toBeDefined(); | ||
expect(result.headers[REALITY_ID]).toBeDefined(); | ||
}); | ||
|
||
test('reality id is passed from the request', async () => { | ||
const realityId = 0; | ||
const headers = { 'reality-id': realityId }; | ||
const result = await axios.post(url, { query }, { headers }); | ||
expect(result.headers[REALITY_ID]).toBe(String(realityId)); | ||
}); | ||
|
||
test('upn is passed from the request', async () => { | ||
const upn = 'just some upn'; | ||
const headers = { 'oicd-claim-upn': upn }; | ||
const result = await axios.post(url, { query }, { headers }); | ||
expect(result.headers[OICD_CLAIM_UPN]).toBe(upn); | ||
}); | ||
|
||
test('request id is passed from the request', async () => { | ||
const requestId = 'troubles'; | ||
const headers = { 'request-id': requestId }; | ||
const result = await axios.post(url, { query }, { headers }); | ||
expect(result.headers[REQUEST_ID]).toBe(requestId); | ||
}); | ||
}); |