Skip to content

Commit

Permalink
Merge pull request #64 from pmcelhaney/tools
Browse files Browse the repository at this point in the history
Add a tools object that will be provided to the request
  • Loading branch information
pmcelhaney committed Jul 5, 2022
2 parents c6ba158 + 406b907 commit ca370ef
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/gentle-kangaroos-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"counterfact": patch
---

Add a tools object that will be provided to the request
6 changes: 4 additions & 2 deletions src/dispatcher.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { Tools } from "./tools.js";

export class Dispatcher {
registry;

constructor(registry) {
this.registry = registry;
}

request({ method, path, body, query }) {
request({ method, path, headers, body, query }) {
return this.registry.endpoint(
method,
path
)({
// path: parts.slice(remainingParts).join("/"),
tools: new Tools({ headers }),

reduce: (reducer) => {
this.registry.context = reducer(this.registry.context);
Expand Down
28 changes: 28 additions & 0 deletions src/tools.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export class Tools {
constructor({ headers = {} } = {}) {
this.headers = headers;
}

oneOf(array) {
return array[Math.floor(Math.random() * array.length)];
}

accepts(contentType) {
const acceptHeader = this.headers.Accept;

if (!acceptHeader) {
return true;
}

const acceptTypes = acceptHeader.split(",");

return acceptTypes.some((acceptType) => {
const [type, subtype] = acceptType.split("/");

return (
(type === "*" || type === contentType.split("/")[0]) &&
(subtype === "*" || subtype === contentType.split("/")[1])
);
});
}
}
33 changes: 33 additions & 0 deletions test/dispatcher.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,39 @@ describe("a dispatcher", () => {
expect(response.body).toBe("Searching for stores near 90210!");
});

it("passes a tools object", async () => {
const registry = new Registry();

registry.add("/a", {
GET({ tools }) {
return { body: tools.accepts("text/html") };
},
});

const dispatcher = new Dispatcher(registry);
const htmlResponse = await dispatcher.request({
method: "GET",
path: "/a",

headers: {
Accept: "text/html",
},
});

expect(htmlResponse.body).toBe(true);

const textResponse = await dispatcher.request({
method: "GET",
path: "/a",

headers: {
Accept: "text/plain",
},
});

expect(textResponse.body).toBe(false);
});

it("passes status code in the response", async () => {
const registry = new Registry();

Expand Down
39 changes: 39 additions & 0 deletions test/tools.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Tools } from "../src/tools.js";

describe("tools", () => {
it("oneOf()", () => {
const tools = new Tools();

expect(["A", "B", "C"]).toContain(tools.oneOf(["A", "B", "C"]));
});

it.each`
contentType | acceptHeader
${"what/ever"} | ${undefined}
${"text/html"} | ${"text/html"}
${"text/html"} | ${"text/*"}
${"application/json"} | ${"*/json"}
${"text/*"} | ${"text/*"}
`(
"accept('$contentType') returns true when the accept header is $acceptHeader",
({ contentType, acceptHeader }) => {
const tools = new Tools({ headers: { Accept: acceptHeader } });

expect(tools.accepts(contentType)).toBe(true);
}
);

it.each`
contentType | acceptHeader
${"application/json"} | ${"text/*"}
${"text/html"} | ${"text/plain"}
${"application/json"} | ${"text/json"}
`(
"accept('$contentType') returns false when the accept header is $acceptHeader",
({ contentType, acceptHeader }) => {
const tools = new Tools({ headers: { Accept: acceptHeader } });

expect(tools.accepts(contentType)).toBe(false);
}
);
});

0 comments on commit ca370ef

Please sign in to comment.