UNPKG

jest-axios-snapshot

Version:
127 lines (126 loc) 4.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.setResponseTransformer = exports.serialize = exports.defaultTransformer = exports.test = void 0; const http_1 = require("http"); /** * Serialize an HTTP status line for an axios response. * * @param response - The axios response to serialize. * @returns The HTTP status line. */ function serializeStatusLine({ status, statusText }) { const result = `HTTP/1.1 ${status}`; if (statusText) { return `${result} ${statusText}`; } if (status in http_1.STATUS_CODES) { return `${result} ${http_1.STATUS_CODES[status]}`; } return result; } /** * Serialize axios HTTP headers. * * @param headers - The axios headers object so serialize. * @param toString - A function used to convert arbritary values to string. * @returns The HTTP headers as a string. */ function serializeHeaders({ headers }, toString) { return Object.entries(headers) .sort(([a], [b]) => a.localeCompare(b)) .map(([key, value]) => `\n${key.replace(/((-|^)[a-z])/g, (m) => m.toUpperCase())}: ${typeof value === 'string' ? value : toString(value)}`) .join(''); } /** * Serialize the axios HTTP body. * * @param response - The axios body data so serialize. * @param toString - A function used to convert arbritary values to string. * @returns The HTTP body as a string. */ function serializeBody({ data, headers }, toString) { var _a; if (typeof data === 'string' && ((_a = headers['content-type']) === null || _a === void 0 ? void 0 : _a.split('/')[0]) === 'text') { return data; } return toString(data); } /** * Serialize the Axios HTTP response. * * @param response - The Axios response to serialize * @param toString - XXX * @returns The Axios response serialized as a string. */ function serializeResponse(response, toString) { let result = serializeStatusLine(response); if (response.headers) { result += serializeHeaders(response, toString); } if (response.data !== '') { result += `\n\n${serializeBody(response, toString)}`; } return result; } /** * @internal * @param actual - The actual value passed to expect. * @returns Whether or not the actual input is an axios response. */ function test(actual) { return (typeof actual === 'object' && actual != null && 'data' in actual && typeof actual.config === 'object' && typeof actual.headers === 'object' && typeof actual.status === 'number' && typeof actual.statusText === 'string'); } exports.test = test; /** * Remove the date header from an axios response. * * @param response - The axios repons to transform. * @returns The response without a `datw` header. */ function defaultTransformer(response) { const headers = {}; for (const [key, value] of Object.entries(response.headers)) { if (key.toLowerCase() !== 'date') { headers[key] = value; } } return { ...response, headers, }; } exports.defaultTransformer = defaultTransformer; let transformResponse = defaultTransformer; /** * Serialize an axios response as a string. * * @internal * @param response - The axios response to serialize. * @param config - THe jest snapshot configuration. * @param indentation - The intentation to use for serializing entities. * @param depth - The of the object to serialize. * @param refs - Used for finding circular references. * @param printer - The ejst pretty printer. * @returns The axios response represented as a string. */ function serialize(response, config, indentation, depth, refs, printer) { return serializeResponse(transformResponse(response), (obj) => printer(obj, config, indentation, depth, refs)); } exports.serialize = serialize; /** * Set a function used to transform an axios response before serializing. * * The default transformer removes the `date` header. * * @param transformer - A function to transform the axios response. */ function setResponseTransformer(transformer) { transformResponse = transformer; } exports.setResponseTransformer = setResponseTransformer;