counterfact
Version:
a library for building a fake REST API for testing
28 lines (27 loc) • 961 B
JavaScript
import { JSONSchemaFaker } from "json-schema-faker";
JSONSchemaFaker.option("useExamplesValue", true);
JSONSchemaFaker.option("fillProperties", false);
export class Tools {
headers;
constructor({ headers = {}, } = {}) {
this.headers = headers;
}
oneOf(array) {
return array[Math.floor(Math.random() * array.length)];
}
accepts(contentType) {
const acceptHeader = this.headers.Accept;
if (acceptHeader === "" || acceptHeader === undefined) {
return true;
}
const acceptTypes = String(acceptHeader).split(",");
return acceptTypes.some((acceptType) => {
const [type, subtype] = acceptType.split("/");
return ((type === "*" || type === contentType.split("/")[0]) &&
(subtype === "*" || subtype === contentType.split("/")[1]));
});
}
randomFromSchema(schema) {
return JSONSchemaFaker.generate(schema);
}
}