From 9e094907141e999d3ab929475637a272bd5c0ffc Mon Sep 17 00:00:00 2001 From: haricnugraha Date: Tue, 16 Apr 2024 16:15:29 +0700 Subject: [PATCH] refactor: move fields down to child function --- src/components/probe/prober/http/index.ts | 6 --- .../probe/prober/http/request.test.ts | 26 ++++++----- src/components/probe/prober/http/request.ts | 43 ++++++++----------- 3 files changed, 32 insertions(+), 43 deletions(-) diff --git a/src/components/probe/prober/http/index.ts b/src/components/probe/prober/http/index.ts index 5c0b1e633..0d0541208 100644 --- a/src/components/probe/prober/http/index.ts +++ b/src/components/probe/prober/http/index.ts @@ -22,7 +22,6 @@ * SOFTWARE. * **********************************************************************************/ -import { monikaFlagsDefaultValue } from '../../../../flag' import { BaseProber, NotificationType, type ProbeParams } from '..' import { getContext } from '../../../../context' import events from '../../../../events' @@ -57,11 +56,6 @@ export class HTTPProber extends BaseProber { responses.push( // eslint-disable-next-line no-await-in-loop await httpRequest({ - isVerbose: getContext().flags.verbose, - maxRedirects: - getContext().flags['follow-redirects'] || - monikaFlagsDefaultValue['follow-redirects'], - isEnableFetch: getContext().flags['native-fetch'], requestConfig: { ...requestConfig, signal }, responses, }) diff --git a/src/components/probe/prober/http/request.test.ts b/src/components/probe/prober/http/request.test.ts index 789108870..8518a70aa 100644 --- a/src/components/probe/prober/http/request.test.ts +++ b/src/components/probe/prober/http/request.test.ts @@ -26,6 +26,7 @@ import { expect } from '@oclif/test' import { HttpResponse, http } from 'msw' import { setupServer } from 'msw/node' +import { getContext, resetContext, setContext } from '../../../../context' import type { ProbeRequestResponse, RequestConfig, @@ -38,9 +39,14 @@ describe('probingHTTP', () => { describe('httpRequest function', () => { before(() => { server.listen() + setContext({ + ...getContext(), + flags: { ...getContext().flags, 'follow-redirects': 0 }, + }) }) afterEach(() => { server.resetHandlers() + resetContext() }) after(() => { server.close() @@ -93,7 +99,6 @@ describe('probingHTTP', () => { try { // eslint-disable-next-line no-await-in-loop const resp = await httpRequest({ - maxRedirects: 0, requestConfig: request, responses, }) @@ -142,7 +147,6 @@ describe('probingHTTP', () => { } const result = await httpRequest({ - maxRedirects: 0, requestConfig: request, responses: [], }) @@ -182,7 +186,6 @@ describe('probingHTTP', () => { // act const res = await httpRequest({ - maxRedirects: 0, requestConfig: request, responses: [], }) @@ -226,7 +229,6 @@ describe('probingHTTP', () => { // act const res = await httpRequest({ - maxRedirects: 0, requestConfig: request, responses: [], }) @@ -270,7 +272,6 @@ describe('probingHTTP', () => { // act const res = await httpRequest({ - maxRedirects: 0, requestConfig: request, responses: [], }) @@ -314,7 +315,6 @@ describe('probingHTTP', () => { // act const res = await httpRequest({ - maxRedirects: 0, requestConfig: request, responses: [], }) @@ -325,6 +325,13 @@ describe('probingHTTP', () => { it('Should handle HTTP redirect with axios', async () => { // arrange + setContext({ + ...getContext(), + flags: { + ...getContext().flags, + 'follow-redirects': 3, + }, + }) server.use( http.get( 'https://example.com/get', @@ -355,11 +362,10 @@ describe('probingHTTP', () => { method: 'GET', body: '', timeout: 10_000, + followRedirects: 3, } const res = await httpRequest({ - isEnableFetch: true, - maxRedirects: 3, requestConfig, responses: [], }) @@ -398,11 +404,10 @@ describe('probingHTTP', () => { method: 'GET', body: '', timeout: 10_000, + followRedirects: 3, } const res = await httpRequest({ - isEnableFetch: false, - maxRedirects: 3, requestConfig, responses: [], }) @@ -446,7 +451,6 @@ describe('probingHTTP', () => { // act const res = await httpRequest({ - maxRedirects: 0, requestConfig: request, responses: [], }) diff --git a/src/components/probe/prober/http/request.ts b/src/components/probe/prober/http/request.ts index 42d60b580..0911747dd 100644 --- a/src/components/probe/prober/http/request.ts +++ b/src/components/probe/prober/http/request.ts @@ -24,34 +24,30 @@ import * as Handlebars from 'handlebars' import FormData from 'form-data' +import Joi from 'joi' +// eslint-disable-next-line no-restricted-imports +import * as qs from 'querystring' +import { errors as undiciErrors } from 'undici' import YAML from 'yaml' import { type ProbeRequestResponse, type RequestConfig, probeRequestResult, } from '../../../../interfaces/request' - -// eslint-disable-next-line no-restricted-imports -import * as qs from 'querystring' - +import { getContext } from '../../../../context' import { icmpRequest } from '../icmp/request' import registerFakes from '../../../../utils/fakes' import { sendHttpRequest, sendHttpRequestFetch } from '../../../../utils/http' import { log } from '../../../../utils/pino' import { AxiosError } from 'axios' import { getErrorMessage } from '../../../../utils/catch-error-handler' -import { errors as undiciErrors } from 'undici' -import Joi from 'joi' // Register Handlebars helpers registerFakes(Handlebars) type probingParams = { - maxRedirects: number requestConfig: Omit // is a config object responses: Array // an array of previous responses - isVerbose?: boolean - isEnableFetch?: boolean } const UndiciErrorValidator = Joi.object({ @@ -64,11 +60,8 @@ const UndiciErrorValidator = Joi.object({ * @returns ProbeRequestResponse, response to the probe request */ export async function httpRequest({ - maxRedirects, requestConfig, responses, - isEnableFetch, - isVerbose, }: probingParams): Promise { // Compile URL using handlebars to render URLs that uses previous responses data const { @@ -107,24 +100,22 @@ export async function httpRequest({ } // Do the request using compiled URL and compiled headers (if exists) - if (isEnableFetch) { + if (getContext().flags['native-fetch']) { return await probeHttpFetch({ startTime, - isVerbose, + maxRedirects: followRedirects, renderedURL, requestParams: { ...newReq, headers: requestHeaders }, allowUnauthorized, - followRedirects, }) } return await probeHttpAxios({ startTime, - maxRedirects, + maxRedirects: followRedirects, renderedURL, requestParams: { ...newReq, headers: requestHeaders }, allowUnauthorized, - followRedirects, }) } catch (error: unknown) { const responseTime = Date.now() - startTime @@ -243,13 +234,12 @@ async function probeHttpFetch({ renderedURL, requestParams, allowUnauthorized, - isVerbose, - followRedirects, + maxRedirects, }: { startTime: number renderedURL: string allowUnauthorized: boolean | undefined - isVerbose?: boolean + maxRedirects: number requestParams: { method: string | undefined headers: Headers | undefined @@ -257,15 +247,17 @@ async function probeHttpFetch({ body: string | object ping: boolean | undefined } - followRedirects: number }): Promise { - if (isVerbose) log.info(`Probing ${renderedURL} with Node.js fetch`) + if (getContext().flags.verbose) { + log.info(`Probing ${renderedURL} with Node.js fetch`) + } + const response = await sendHttpRequestFetch({ ...requestParams, allowUnauthorizedSsl: allowUnauthorized, keepalive: true, url: renderedURL, - maxRedirects: followRedirects, + maxRedirects, body: typeof requestParams.body === 'string' ? requestParams.body @@ -310,7 +302,6 @@ type ProbeHTTPAxiosParams = { body: string | object ping: boolean | undefined } - followRedirects: number } async function probeHttpAxios({ @@ -318,14 +309,14 @@ async function probeHttpAxios({ renderedURL, requestParams, allowUnauthorized, - followRedirects, + maxRedirects, }: ProbeHTTPAxiosParams): Promise { const resp = await sendHttpRequest({ ...requestParams, allowUnauthorizedSsl: allowUnauthorized, keepalive: true, url: renderedURL, - maxRedirects: followRedirects, + maxRedirects, body: typeof requestParams.body === 'string' ? requestParams.body