domsuite
Version:
Browser testing/automation utilities with async/await
125 lines (123 loc) • 4.71 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// lib/fetch-server.js
var fetch_server_exports = {};
__export(fetch_server_exports, {
FetchServer: () => FetchServer
});
module.exports = __toCommonJS(fetch_server_exports);
var sinon = __toESM(require("sinon"), 1);
var import_lodash_unified = require("lodash-unified");
var FetchServer = class {
constructor(handlers, { debug = false, errorCallback = null, sort = false } = {}) {
this.handlers = handlers;
this.debug = debug;
this.errorCallback = errorCallback;
this.sort = sort;
}
handle(url, params) {
const routes = Object.entries(this.handlers);
if (this.sort) {
routes.sort(([aRoute], [bRoute]) => {
if (bRoute.length === aRoute.length) {
return bRoute.toLowerCase() < aRoute.toLowerCase() ? 1 : -1;
} else {
return bRoute.length - aRoute.length;
}
});
}
for (const [route, handler] of routes) {
if (url.includes(route)) {
if (params && params.body) {
try {
params = JSON.parse(params.body);
} catch (err) {
params = params.body;
}
}
let response = (0, import_lodash_unified.isFunction)(handler) ? handler(url, params) : handler;
if (!response.then) {
const responseOptions = {
status: 200,
headers: {
"Content-Type": `application/json`
}
};
response = Promise.resolve(new Response(JSON.stringify(response), responseOptions));
}
this.debugLog({ url, params, response });
return response;
}
}
throw new Error(`Unexpected fetch: ${url} params: ${JSON.stringify(params)}`);
}
start() {
const fakeFetch = (url, params) => {
try {
return this.handle(url, params);
} catch (err) {
(0, import_lodash_unified.isFunction)(this.errorCallback) && this.errorCallback(err);
throw err;
}
};
this.restore();
if (typeof window !== `undefined` && window.fetch && !Object.hasOwnProperty.call(window.fetch, `restore`)) {
sinon.stub(window, `fetch`).callsFake(fakeFetch);
}
if (typeof global !== `undefined` && global.fetch && !Object.hasOwnProperty.call(global.fetch, `restore`)) {
sinon.stub(global, `fetch`).callsFake(fakeFetch);
}
}
static restore() {
if (typeof window !== `undefined` && window.fetch && Object.hasOwnProperty.call(window.fetch, `restore`)) {
window.fetch.restore();
}
if (typeof global !== `undefined` && global.fetch && Object.hasOwnProperty.call(global.fetch, `restore`)) {
global.fetch.restore();
}
}
restore() {
FetchServer.restore();
}
debugLog({ url, params, response }) {
if (this.debug) {
const responseObjectPromise = response.then((r) => r.clone());
const responseBodyPromise = responseObjectPromise.then((clonedResponse) => clonedResponse.json());
Promise.all([responseObjectPromise, responseBodyPromise]).then(([response2, responseBody]) => {
console.groupCollapsed(`[fetch-server] ${url}`);
console.groupCollapsed(`Params`);
console.log(JSON.stringify(params, null, 2));
console.groupEnd();
console.groupCollapsed(`Response`);
console.log(JSON.stringify(response2, null, 2));
console.groupEnd();
console.group(`Response Body`);
console.log(JSON.stringify(responseBody, null, 2));
console.groupEnd();
console.groupEnd();
});
}
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
FetchServer
});