Skip to content

Commit

Permalink
refactor: Add doNotFollowRedirects option to WebsiteRequest and API
Browse files Browse the repository at this point in the history
This commit adds the `doNotFollowRedirects` option to the `WebsiteRequest` and `API` classes. When set to `true`, the classes will not follow redirects when making HTTP requests. This is achieved by setting the `maxRedirects` property of the `axiosOptions` object to 0. This change allows for more control over the behavior of HTTP requests and ensures that redirects are not automatically followed.
  • Loading branch information
simlarsen committed Oct 9, 2024
1 parent f5d2c61 commit 9e45f4c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Common/Types/WebsiteRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default class WebsiteRequest {
headers?: Headers | undefined;
timeout?: number | undefined;
isHeadRequest?: boolean | undefined;
doNotFollowRedirects?: boolean | undefined;
},
): Promise<WebsiteResponse> {
const axiosOptions: AxiosRequestConfig = {
Expand All @@ -36,6 +37,10 @@ export default class WebsiteRequest {
axiosOptions.method = HTTPMethod.HEAD;
}

if (options.doNotFollowRedirects) {
axiosOptions.maxRedirects = 0;
}

// use axios to fetch an HTML page
let response: AxiosResponse | null = null;

Expand Down
5 changes: 5 additions & 0 deletions Common/Utils/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface RequestOptions {
retries?: number | undefined;
exponentialBackoff?: boolean | undefined;
timeout?: number | undefined;
doNotFollowRedirects?: boolean | undefined;
}

export default class API {
Expand Down Expand Up @@ -395,6 +396,10 @@ export default class API {
axiosOptions.timeout = options.timeout;
}

if (options?.doNotFollowRedirects) {
axiosOptions.maxRedirects = 0;
}

result = await axios(axiosOptions);

break;
Expand Down
1 change: 1 addition & 0 deletions Probe/Utils/Monitors/MonitorTypes/ApiMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export default class ApiMonitor {
undefined,
{
timeout: options.timeout?.toNumber() || 5000,
doNotFollowRedirects: true,
},
);
}
Expand Down
1 change: 1 addition & 0 deletions Probe/Utils/Monitors/MonitorTypes/WebsiteMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export default class WebsiteMonitor {
let result: WebsiteResponse = await WebsiteRequest.fetch(url, {
isHeadRequest: options.isHeadRequest,
timeout: options.timeout?.toNumber() || 5000,
doNotFollowRedirects: true,
});

if (
Expand Down

0 comments on commit 9e45f4c

Please sign in to comment.