Skip to content

Commit

Permalink
fix: reality id in response headers is now fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
osher-sade committed Apr 27, 2020
1 parent dff89c9 commit 62c1abc
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 50 deletions.
1 change: 0 additions & 1 deletion src/config/polaris-server-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
ApplicationProperties,
LoggerConfiguration,
} from '@enigmatis/polaris-logs';
import { PolarisConnection } from '@enigmatis/polaris-typeorm';
import { ApolloServerExpressConfig } from 'apollo-server-express';
import { DocumentNode } from 'graphql';
import { IResolvers } from 'graphql-tools';
Expand Down
1 change: 0 additions & 1 deletion src/config/polaris-server-options.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { RealitiesHolder } from '@enigmatis/polaris-common';
import { PolarisGraphQLLogger } from '@enigmatis/polaris-graphql-logger';
import { ApplicationProperties, LoggerConfiguration } from '@enigmatis/polaris-logs';
import { PolarisConnection } from '@enigmatis/polaris-typeorm';
import { ApolloServerExpressConfig } from 'apollo-server-express';
import { DocumentNode } from 'graphql';
import { IResolvers } from 'graphql-tools';
Expand Down
43 changes: 0 additions & 43 deletions src/headers/response-headers-listener.ts

This file was deleted.

45 changes: 45 additions & 0 deletions src/plugins/headers/response-headers-listener.ts
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');
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { PolarisGraphQLContext } from '@enigmatis/polaris-common';
import { PolarisGraphQLLogger } from '@enigmatis/polaris-graphql-logger';
import {
ApolloServerPlugin,
Expand All @@ -6,16 +7,16 @@ import {
} from 'apollo-server-plugin-base';
import { ResponseHeadersListener } from './response-headers-listener';

export class ResponseHeadersPlugin implements ApolloServerPlugin {
export class ResponseHeadersPlugin implements ApolloServerPlugin<PolarisGraphQLContext> {
public readonly logger: any;

constructor(logger: PolarisGraphQLLogger) {
this.logger = logger;
}

public requestDidStart<TContext>(
requestContext: GraphQLRequestContext<TContext>,
): GraphQLRequestListener<TContext> | void {
public requestDidStart(
requestContext: GraphQLRequestContext<PolarisGraphQLContext>,
): GraphQLRequestListener<PolarisGraphQLContext> | void {
return new ResponseHeadersListener(this.logger);
}
}
2 changes: 1 addition & 1 deletion src/server/polaris-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ import * as path from 'path';
import { v4 as uuid } from 'uuid';
import { formatError, PolarisServerOptions } from '..';
import { PolarisServerConfig } from '../config/polaris-server-config';
import { ResponseHeadersPlugin } from '../headers/response-headers-plugin';
import { getMiddlewaresMap } from '../middlewares/middlewares-map';
import { SnapshotMiddleware } from '../middlewares/snapshot-middleware';
import { ExtensionsPlugin } from '../plugins/extensions/extensions-plugin';
import { ResponseHeadersPlugin } from '../plugins/headers/response-headers-plugin';
import { SnapshotPlugin } from '../plugins/snapshot/snapshot-plugin';
import {
clearSnapshotCleanerInterval,
Expand Down
49 changes: 49 additions & 0 deletions test/integration/tests/response-headers.test.ts
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);
});
});

0 comments on commit 62c1abc

Please sign in to comment.