Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Axios request and response interceptors #16445

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions tests/e2e/driver/CheReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { DriverHelper } from '../utils/DriverHelper';
import { ScreenCatcher } from '../utils/ScreenCatcher';
import { ITestWorkspaceUtil } from '../utils/workspace/ITestWorkspaceUtil';
import { PreferencesHandler, AskForConfirmationType } from '../utils/PreferencesHandler';
import { CheApiRequestHandler } from '../utils/requestHandlers/CheApiRequestHandler';

const e2eContainer = inversifyConfig.e2eContainer;
const driver: IDriver = e2eContainer.get(TYPES.Driver);
Expand Down Expand Up @@ -67,6 +68,10 @@ class CheReporter extends mocha.reporters.Spec {
console.log(launchInformation);

rm.sync(TestConstants.TS_SELENIUM_REPORT_FOLDER);
if (TestConstants.TS_SELENIUM_LOG_LEVEL === 'TRACE') {
CheApiRequestHandler.enableRequestInteceptor();
CheApiRequestHandler.enableResponseInterceptor();
}
preferencesHalder.setConfirmExit(AskForConfirmationType.never);
});

Expand Down
42 changes: 41 additions & 1 deletion tests/e2e/utils/requestHandlers/CheApiRequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,54 @@
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/

import axios, { AxiosResponse } from 'axios';
import axios, { AxiosResponse, AxiosRequestConfig } from 'axios';
import { TestConstants } from '../../TestConstants';
import { TYPES } from '../../inversify.types';
import { inject, injectable } from 'inversify';
import { IAuthorizationHeaderHandler } from './headers/IAuthorizationHeaderHandler';

@injectable()
export class CheApiRequestHandler {

/**
* This method adds a request interceptor into axios request interceptors list and returns an ID of the interceptor
*/
public static enableRequestInteceptor(): number {
console.log(`CheApiRequestHandler.enableRequestInterceptor`);
Katka92 marked this conversation as resolved.
Show resolved Hide resolved
return axios.interceptors.request.use( request => {
try {
let request_censored: AxiosRequestConfig = JSON.parse(JSON.stringify(request));
request_censored.headers['Authorization'] = 'CENSORED';
console.log(`RequestHandler request:\n`, request_censored);
} catch (err) {
console.log(`RequestHandler request: Failed to deep clone AxiosRequestConfig:`, err);
}
return request;
});
}

/**
* This method adds a response interceptor into axios response interceptors list and returns an ID of the interceptor
*/
public static enableResponseInterceptor(): number {
console.log(`CheApiRequestHandler.enableResponseRedirects`);
return axios.interceptors.response.use( response => {
try {
let response_censored: AxiosResponse = JSON.parse(JSON.stringify(response, (key, value) => {
switch (key) {
case 'request': return 'CENSORED';
default: return value;
}
}));
response_censored.config.headers['Authorization'] = 'CENSORED';
console.log(`RequestHandler response:\n`, response_censored);
} catch (err) {
console.log(`RequestHandler response: Failed to deep clone AxiosResponse:`, err);
}
return response;
});
}

constructor(@inject(TYPES.IAuthorizationHeaderHandler) private readonly headerHandler: IAuthorizationHeaderHandler) { }

async get(relativeUrl: string): Promise<AxiosResponse> {
Expand Down