@fedify/fedify
Version:
An ActivityPub server framework
68 lines (64 loc) • 1.82 kB
JavaScript
import { Temporal } from "@js-temporal/polyfill";
import { URLPattern } from "urlpattern-polyfill";
globalThis.addEventListener = () => {};
import { getLogger } from "@logtape/logtape";
//#region testing/docloader.ts
const logger = getLogger([
"fedify",
"testing",
"docloader"
]);
/**
* A mock of the document loader. This does not make any actual HTTP requests
* towards the remote server, but looks up the local fixture files instead.
*
* For instance, `mockDocumentLoader("http://example.com/foo/bar")` will look up
* the file `testing/fixtures/http/example.com/foo/bar` (no suffix) and return
* its content as the response.
*/
async function mockDocumentLoader(resource) {
const url = new URL(resource);
if ("navigator" in globalThis && navigator.userAgent === "Cloudflare-Workers") {
const testUrl = new URL(url);
testUrl.hostname += ".test";
const resp = await fetch(testUrl);
if (resp.ok) {
const document$1 = await resp.json();
logger.debug("Successfully fetched fixture {resource}: {status} {statusText}\n{body}", {
resource,
status: resp.status,
statusText: resp.statusText,
body: document$1
});
return {
contextUrl: null,
document: document$1,
documentUrl: resource
};
}
const error = await resp.text();
logger.error("Failed to fetch fixture {resource}: {error}", {
resource,
error
});
throw new Error(error);
}
const path = `./fixtures/${url.host}${url.pathname}.json`;
let document;
try {
document = (await import(path, { with: { type: "json" } })).default;
} catch (error) {
logger.error("Failed to read fixture file {path}: {error}", {
path,
error
});
throw error;
}
return {
contextUrl: null,
document,
documentUrl: resource
};
}
//#endregion
export { mockDocumentLoader };