@wordpress/e2e-test-utils
Version:
End-To-End (E2E) test utils for WordPress.
118 lines (117 loc) • 4.36 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(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var rest_api_exports = {};
__export(rest_api_exports, {
batch: () => batch,
rest: () => rest
});
module.exports = __toCommonJS(rest_api_exports);
var import_node_fetch = __toESM(require("node-fetch"));
var import_form_data = __toESM(require("form-data"));
var import_api_fetch = __toESM(require("@wordpress/api-fetch"));
var import_config = require("./shared/config");
var import_create_url = require("./create-url");
global.window = global.window || {};
global.window.fetch = import_node_fetch.default;
const setAPIRootURL = (async () => {
const res = await (0, import_node_fetch.default)(import_config.WP_BASE_URL, { method: "HEAD" });
const links = res.headers.get("link");
const restLink = links.match(/<([^>]+)>; rel="https:\/\/api\.w\.org\/"/);
if (!restLink) {
throw new Error(`Failed to discover REST API endpoint.
Link header: ${links}`);
}
const [, rootURL] = restLink;
import_api_fetch.default.use(import_api_fetch.default.createRootURLMiddleware(rootURL));
})();
async function login(retries = 3) {
const formData = new import_form_data.default();
formData.append("log", import_config.WP_USERNAME);
formData.append("pwd", import_config.WP_PASSWORD);
const loginResponse = await (0, import_node_fetch.default)((0, import_create_url.createURL)("wp-login.php"), {
method: "POST",
headers: formData.getHeaders(),
body: formData,
redirect: "manual"
});
const cookies = loginResponse.headers.get("set-cookie");
const cookie = cookies.split(",").map((setCookie) => setCookie.split(";")[0]).join(";");
import_api_fetch.default.nonceEndpoint = (0, import_create_url.createURL)(
"wp-admin/admin-ajax.php",
"action=rest-nonce"
);
const response = await (0, import_node_fetch.default)(import_api_fetch.default.nonceEndpoint, {
headers: { cookie }
});
if (response.status === 200) {
return {
response,
cookie
};
}
if (retries > 0) {
return login(retries - 1);
}
throw new Error(
`Fetch api call failed for ${import_api_fetch.default.nonceEndpoint}: ${response.status}`
);
}
const setNonce = (async () => {
const loginRequest = await login();
const nonce = await loginRequest.response.text();
import_api_fetch.default.use(import_api_fetch.default.createNonceMiddleware(nonce));
import_api_fetch.default.use(function setCookieMiddleware(request, next) {
return next({
...request,
headers: {
...request.headers,
cookie: loginRequest.cookie
}
});
});
})();
async function rest(options = {}) {
await Promise.all([setAPIRootURL, setNonce]);
return await (0, import_api_fetch.default)(options);
}
async function batch(requests) {
return await rest({
method: "POST",
path: "/batch/v1",
data: {
requests,
validation: "require-all-validate"
}
});
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
batch,
rest
});
//# sourceMappingURL=rest-api.js.map