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

feat: use axios-mock-adapter for mocking axios requests #2647

Merged
26 changes: 7 additions & 19 deletions src/adapters/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
const requestOptions = enhanceRequestOptions(options);

const startTime = new Date();
const { url, data, method, ...opts } = requestOptions;
const { url, data, method } = requestOptions;
try {
const response = await axios(requestOptions);
clientResponse = { success: true, response };
Expand All @@ -96,7 +96,7 @@
fireHTTPStats(clientResponse, startTime, statTags);
}

setResponsesForMockAxiosAdapter({ url, data, method, options: opts }, clientResponse);
setResponsesForMockAxiosAdapter({ url, data, method, options }, clientResponse);
return clientResponse;
};

Expand All @@ -122,7 +122,7 @@
} finally {
fireHTTPStats(clientResponse, startTime, statTags);
}
setResponsesForMockAxiosAdapter({ url, options: requestOptions, method: 'GET' }, clientResponse);
setResponsesForMockAxiosAdapter({ url, options, method: 'GET' }, clientResponse);
return clientResponse;
};

Expand All @@ -148,10 +148,7 @@
} finally {
fireHTTPStats(clientResponse, startTime, statTags);
}
setResponsesForMockAxiosAdapter(
{ url, options: requestOptions, method: 'DELETE' },
clientResponse,
);
setResponsesForMockAxiosAdapter({ url, options, method: 'DELETE' }, clientResponse);
return clientResponse;
};

Expand All @@ -178,10 +175,7 @@
} finally {
fireHTTPStats(clientResponse, startTime, statTags);
}
setResponsesForMockAxiosAdapter(
{ url, data, options: requestOptions, method: 'POST' },
clientResponse,
);
setResponsesForMockAxiosAdapter({ url, data, options, method: 'POST' }, clientResponse);
return clientResponse;
};

Expand All @@ -208,10 +202,7 @@
} finally {
fireHTTPStats(clientResponse, startTime, statTags);
}
setResponsesForMockAxiosAdapter(
{ url, data, options: requestOptions, method: 'PUT' },
clientResponse,
);
setResponsesForMockAxiosAdapter({ url, data, options, method: 'PUT' }, clientResponse);
return clientResponse;
};

Expand All @@ -238,10 +229,7 @@
} finally {
fireHTTPStats(clientResponse, startTime, statTags);
}
setResponsesForMockAxiosAdapter(
{ url, data, options: requestOptions, method: 'PATCH' },
clientResponse,
);
setResponsesForMockAxiosAdapter({ url, data, options, method: 'PATCH' }, clientResponse);

Check warning on line 232 in src/adapters/network.js

View check run for this annotation

Codecov / codecov/patch

src/adapters/network.js#L232

Added line #L232 was not covered by tests
return clientResponse;
};

Expand Down
15 changes: 10 additions & 5 deletions src/v0/destinations/canny/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const retrieveUserId = async (apiKey, message) => {
message.traits?.email || message.context?.traits?.email || message.properties?.email;
const { userId } = message;

const header = {
const headers = {
'Content-Type': 'application/x-www-form-urlencoded',
Accept: JSON_MIME_TYPE,
};
Expand All @@ -38,10 +38,15 @@ const retrieveUserId = async (apiKey, message) => {
} else {
requestBody.userID = `${userId}`;
}
const response = await httpPOST(url, qs.stringify(requestBody), header, {
destType: 'canny',
feature: 'transformation',
});
const response = await httpPOST(
url,
qs.stringify(requestBody),
{ headers },
{
destType: 'canny',
feature: 'transformation',
},
);
logger.debug(response);
// If the request fails, throwing error.
if (response.success === false) {
Expand Down
6 changes: 3 additions & 3 deletions src/v0/destinations/yahoo_dsp/util.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const qs = require('qs');
const sha256 = require('sha256');
const { generateJWTToken } = require('../../../util/jwtTokenGenerator');
const { httpPOST } = require('../../../adapters/network');
const { httpSend } = require('../../../adapters/network');
const { isDefinedAndNotNullAndNotEmpty } = require('../../util');
const { getDynamicErrorType } = require('../../../adapters/utils/networkUtils');
const { ACCESS_TOKEN_CACHE_TTL, AUDIENCE_ATTRIBUTE, DSP_SUPPORTED_OPERATION } = require('./config');
Expand Down Expand Up @@ -119,7 +119,7 @@ const getAccessToken = async (destination) => {
};

const request = {
header: {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Accept: JSON_MIME_TYPE,
},
Expand All @@ -134,7 +134,7 @@ const getAccessToken = async (destination) => {
}),
method: 'POST',
};
const dspAuthorisationData = await httpPOST(request.url, request.data, request.header, {
const dspAuthorisationData = await httpSend(request, {
destType: 'yahoo_dsp',
feature: 'transformation',
});
Expand Down
17 changes: 12 additions & 5 deletions test/integrations/component.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { Server } from 'http';
import { appendFileSync } from 'fs';
import { responses } from '../testHelper';
import utils from '../../src/v0/util';
import isMatch from 'lodash/isMatch';

// To run single destination test cases
// npm run test:ts -- component --destination=adobe_analytics
Expand Down Expand Up @@ -79,26 +80,32 @@ if (!opts.generate || opts.generate === 'false') {
const { url, method, data: reqData, ...opts } = axiosMock.httpReq;
const { data, headers, status } = axiosMock.httpRes;

const headersAsymMatch = {
asymmetricMatch: function (actual) {
return isMatch(actual, opts.headers);
},
};

switch (method.toLowerCase()) {
case 'get':
// @ts-ignore
mock.onGet(url, reqData, opts.headers).reply(status, data, headers);
mock.onGet(url, reqData, headersAsymMatch).reply(status, data, headers);
koladilip marked this conversation as resolved.
Show resolved Hide resolved
break;
case 'delete':
// @ts-ignore
mock.onDelete(url, reqData, opts.headers).reply(status, data, headers);
mock.onDelete(url, reqData, headersAsymMatch).reply(status, data, headers);
break;
case 'post':
// @ts-ignore
mock.onPost(url, reqData, opts.headers).reply(status, data, headers);
mock.onPost(url, reqData, headersAsymMatch).reply(status, data, headers);
break;
case 'patch':
// @ts-ignore
mock.onPatch(url, reqData, opts.headers).reply(status, data, headers);
mock.onPatch(url, reqData, headersAsymMatch).reply(status, data, headers);
break;
case 'put':
// @ts-ignore
mock.onPut(url, reqData, opts.headers).reply(status, data, headers);
mock.onPut(url, reqData, headersAsymMatch).reply(status, data, headers);
break;
default:
break;
Expand Down
Loading
Loading