Skip to content

Commit

Permalink
AUT-2578: Remove old getRequestConfig function
Browse files Browse the repository at this point in the history
All internal requests are now converted to the new getInternalRequestConfigWithSecurityHeaders function, which sets security headers. Therefore, this function is now redundant and can be removed.
  • Loading branch information
BeckaL committed May 21, 2024
1 parent 41f29bb commit d7423bf
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 113 deletions.
45 changes: 0 additions & 45 deletions src/utils/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,51 +43,6 @@ export function createApiResponse<T>(
};
}

export function getRequestConfig(options: ConfigOptions): AxiosRequestConfig {
const config: AxiosRequestConfig = {
headers: {
"X-API-Key": getApiKey(),
},
proxy: false,
};

if (options.sessionId) {
config.headers["Session-Id"] = options.sessionId;
}

if (options.clientSessionId) {
config.headers["Client-Session-Id"] = options.clientSessionId;
}

if (options.sourceIp) {
config.headers["X-Forwarded-For"] = options.sourceIp;
}

if (options.validationStatuses) {
config.validateStatus = function (status: number) {
return options.validationStatuses.includes(status);
};
}

if (options.persistentSessionId) {
config.headers["di-persistent-session-id"] = options.persistentSessionId;
}

if (options.baseURL) {
config.baseURL = options.baseURL;
}

if (options.reauthenticate) {
config.headers["Reauthenticate"] = options.reauthenticate;
}

if (options.userLanguage) {
config.headers["User-Language"] = options.userLanguage;
}

return config;
}

function getSecurityHeaders(path: string, req: Request, baseUrl?: string) {
let personalDataHeaders = {};
let url = "";
Expand Down
69 changes: 1 addition & 68 deletions test/unit/utils/http.test.ts
Original file line number Diff line number Diff line change
@@ -1,78 +1,11 @@
import { expect } from "chai";
import { describe } from "mocha";
import {
getRequestConfig,
getInternalRequestConfigWithSecurityHeaders,
} from "../../../src/utils/http";
import { getInternalRequestConfigWithSecurityHeaders } from "../../../src/utils/http";
import { API_ENDPOINTS, HTTP_STATUS_CODES } from "../../../src/app.constants";
import { createMockRequest } from "../../helpers/mock-request-helper";
const headersLibrary = require("@govuk-one-login/frontend-passthrough-headers");
import sinon, { SinonSpy } from "sinon";

describe("getRequestConfig", () => {
const apiKey = "123";

beforeEach(() => {
process.env.API_KEY = apiKey;
});

afterEach(() => {
delete process.env.API_KEY;
});

it("should set the relevant headers on a request", () => {
const sessionId = "someSessionId";
const sourceIp = "123.123.123.123";
const clientSessionId = "someClientSessionId";
const persistentSessionId = "somePersistentSessionId";
const reauthenticate = true;
const userLanguage = "cy";
const actualConfig = getRequestConfig({
sessionId: sessionId,
sourceIp: sourceIp,
clientSessionId: clientSessionId,
persistentSessionId: persistentSessionId,
reauthenticate: reauthenticate,
userLanguage: userLanguage,
});

const expectedHeaders = {
"X-API-Key": apiKey,
"Session-Id": sessionId,
"Client-Session-Id": clientSessionId,
"X-Forwarded-For": sourceIp,
"di-persistent-session-id": persistentSessionId,
Reauthenticate: reauthenticate,
"User-Language": userLanguage,
};

expect(actualConfig.headers).to.deep.eq(expectedHeaders);
});

it("should set the correct baseURL", () => {
const baseURL = "https://www.example.com";

const actualConfig = getRequestConfig({
baseURL: baseURL,
});

expect(actualConfig.baseURL).to.eq(baseURL);
});

it("should set the relevant validation status function", () => {
const validStatus = HTTP_STATUS_CODES.OK;

const actualConfig = getRequestConfig({
validationStatuses: [validStatus],
});

expect(actualConfig.validateStatus(validStatus)).to.eq(true);
expect(actualConfig.validateStatus(HTTP_STATUS_CODES.BAD_REQUEST)).to.eq(
false
);
});
});

describe("getInternalRequestConfigWithSecurityHeaders", () => {
const apiKey = "123";
const req = createMockRequest(API_ENDPOINTS.START);
Expand Down

0 comments on commit d7423bf

Please sign in to comment.