UNPKG

playwright-fluent

Version:
211 lines (210 loc) 7.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateMockCodeOf = exports.generateCodeForMissingMock = exports.mockGetWithJsonResponseDependingOnQueryString = exports.mockPostWithEmptyResponseAndStatus = exports.mockGetWithForbiddenResponse = exports.mockGetWithUnauthorizedResponse = exports.mockGetWithEmptyResponseAndStatus = exports.mockGetWithJavascriptResponse = exports.mockGetWithJsonResponse = void 0; const tslib_1 = require("tslib"); const path_1 = tslib_1.__importDefault(require("path")); const fs_1 = require("fs"); const utils_1 = require("../../../utils"); /** * Ceate a mock for a GET request to the specified url that returns a JSON response. * * @export * @template T * @param {string} relativeUrl * @param {T} response - The JSON response to return. * @param {UpdatePolicyOptions} [updatePolicy] * @returns {Partial<FluentMock>} * @example * It will return this mock: * { * displayName: `GET ${relativeUrl}`, * urlMatcher: (url) => url.includes(relativeUrl), * methodMatcher: (m) => m === 'GET', * jsonResponse: () => response, * } */ function mockGetWithJsonResponse(relativeUrl, response, updatePolicy) { return { displayName: `GET ${relativeUrl}`, urlMatcher: (url) => url.includes(relativeUrl), methodMatcher: (m) => m === 'GET', jsonResponse: () => response, ...updatePolicy, }; } exports.mockGetWithJsonResponse = mockGetWithJsonResponse; /** * Ceate a mock for a GET request to the specified url that returns a JavaScript response. * * @export * @param {string} relativeUrl * @param {string} response - The JavaScript response to return. * @param {UpdatePolicyOptions} [updatePolicy] * @returns {Partial<FluentMock>} * @example * It will return this mock: * { * displayName: `GET ${relativeUrl}`, * urlMatcher: (url) => url.includes(relativeUrl), * methodMatcher: (m) => m === 'GET', * responseType: 'javascript', * rawResponse: () => response, * } * */ function mockGetWithJavascriptResponse(relativeUrl, response, updatePolicy) { return { displayName: `GET ${relativeUrl}`, urlMatcher: (url) => url.includes(relativeUrl), methodMatcher: (m) => m === 'GET', responseType: 'javascript', rawResponse: () => response, ...updatePolicy, }; } exports.mockGetWithJavascriptResponse = mockGetWithJavascriptResponse; function mockGetWithEmptyResponseAndStatus(relativeUrl, status = 200) { return { displayName: `GET ${relativeUrl}`, urlMatcher: (url) => url.includes(relativeUrl), methodMatcher: (m) => m === 'GET', responseType: 'empty', status, }; } exports.mockGetWithEmptyResponseAndStatus = mockGetWithEmptyResponseAndStatus; /** * Ceate a mock for a GET request to the specified url that returns an HTTP 401 status. * * @export * @param {string} relativeUrl * @returns {Partial<FluentMock>} * @example * It will return this mock: * { * displayName: `GET ${relativeUrl}`, * urlMatcher: (url) => url.includes(relativeUrl), * methodMatcher: (m) => m === 'GET', * responseType: 'empty', * status: 401, * } */ function mockGetWithUnauthorizedResponse(relativeUrl) { return { displayName: `GET ${relativeUrl}`, urlMatcher: (url) => url.includes(relativeUrl), methodMatcher: (m) => m === 'GET', responseType: 'empty', status: 401, }; } exports.mockGetWithUnauthorizedResponse = mockGetWithUnauthorizedResponse; /** * Ceate a mock for a GET request to the specified url that returns an HTTP 403 status. * * @export * @param {string} relativeUrl * @returns {Partial<FluentMock>} * @example * It will return this mock: * { * displayName: `GET ${relativeUrl}`, * urlMatcher: (url) => url.includes(relativeUrl), * methodMatcher: (m) => m === 'GET', * responseType: 'empty', * status: 403, * } */ function mockGetWithForbiddenResponse(relativeUrl) { return { displayName: `GET ${relativeUrl}`, urlMatcher: (url) => url.includes(relativeUrl), methodMatcher: (m) => m === 'GET', responseType: 'empty', status: 403, }; } exports.mockGetWithForbiddenResponse = mockGetWithForbiddenResponse; function mockPostWithEmptyResponseAndStatus(relativeUrl, status = 200) { return { displayName: `POST ${relativeUrl}`, urlMatcher: (url) => url.includes(relativeUrl), methodMatcher: (m) => m === 'POST', responseType: 'empty', status, }; } exports.mockPostWithEmptyResponseAndStatus = mockPostWithEmptyResponseAndStatus; /** * Ceate a mock for a GET request to the specified url that returns a JSON response depending on the query string * * @export * @template T * @param {string} relativeUrl - The url to mock * @param {QueryString} queryString - The query string to match against the url. * @param {T} response - The JSON response to return. * @param {UpdatePolicyOptions} [updatePolicy] * @returns {Partial<FluentMock>} * @example * const mock = mockGetWithJsonResponseDependingOnQueryString('/api/v1/yo', { foo: 'bar' }, response); * // will mock any request to /api/v1/yo whose query string contains ?foo=bar */ function mockGetWithJsonResponseDependingOnQueryString(relativeUrl, queryString, response, updatePolicy) { const searchParams = new URLSearchParams(queryString); return { displayName: `GET ${relativeUrl}?${searchParams.toString()}`, methodMatcher: (m) => m === 'GET', urlMatcher: (url) => url.includes(relativeUrl), queryStringMatcher: (qs) => { let match = true; for (const key in queryString) { if (qs && qs[key] === undefined) { match = false; break; } if (qs && qs[key] && qs[key] !== queryString[key]) { match = false; break; } } return match; }, jsonResponse: () => response, ...updatePolicy, }; } exports.mockGetWithJsonResponseDependingOnQueryString = mockGetWithJsonResponseDependingOnQueryString; function generateCodeForMissingMock(missingMock, targetDirectory) { (0, utils_1.ensureDirectoryExists)(targetDirectory); const targetFolderName = (0, utils_1.urlToShortPath)(missingMock.url).replace(/\//g, '_').replace(/\?/g, '#'); const targetSubDirectory = path_1.default.join(targetDirectory, targetFolderName); (0, utils_1.ensureDirectoryExists)(targetSubDirectory); switch (missingMock.mimeType) { case 'application/json': { const dataFileName = `${targetFolderName}.json`; const dataFilePath = path_1.default.join(targetSubDirectory, dataFileName); (0, fs_1.writeFileSync)(dataFilePath, JSON.stringify(missingMock.response, null, 2)); const mockSourceCode = generateMockCodeOf(missingMock, dataFileName); (0, fs_1.writeFileSync)(path_1.default.join(targetSubDirectory, `${targetFolderName}.ts`), mockSourceCode); } break; default: // eslint-disable-next-line no-console console.log(`Unsupported mime type: ${missingMock.mimeType}`); break; } } exports.generateCodeForMissingMock = generateCodeForMissingMock; function generateMockCodeOf(missingMock, dataFileName) { if (missingMock.mimeType === 'application/json' && missingMock.method === 'GET' && (0, utils_1.hasNoQueryString)(missingMock.url)) { return ` import response from './${dataFileName}'; export const mock = mockGetWithJsonResponse('${missingMock.url}', response) `; } return `Unsupported mime type: ${missingMock.mimeType}`; } exports.generateMockCodeOf = generateMockCodeOf;