Skip to content

Commit

Permalink
fix some lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
pmcelhaney committed Jul 5, 2022
1 parent 16c8c88 commit 774a98c
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
1 change: 0 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ module.exports = {
{
ignorePattern: "eslint|it\\(|describe\\(",
ignoreTemplateLiterals: true,
length: 120,
},
],

Expand Down
2 changes: 1 addition & 1 deletion src/dispatcher.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Tools } from "./tools";
import { Tools } from "./tools.js";

export class Dispatcher {
registry;
Expand Down
2 changes: 2 additions & 0 deletions src/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export class Tools {

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

if (!acceptHeader) {
return true;
}
Expand All @@ -17,6 +18,7 @@ export class Tools {

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

return (
(type === "*" || type === contentType.split("/")[0]) &&
(subtype === "*" || subtype === contentType.split("/")[1])
Expand Down
13 changes: 6 additions & 7 deletions test/dispatcher.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,34 +76,33 @@ describe("a dispatcher", () => {
const registry = new Registry();

registry.add("/a", {
GET({ query, tools }) {
if (tools.accepts("text/html")) {
return { contentType: "text/html", body: "<p>Hello!</p>" };
}
return { contentType: "text/plain", body: "Hello!" };
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.contentType).toBe("text/html");
expect(htmlResponse.body).toBe(true);

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

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

expect(textResponse.body).toBe("Hello!");
expect(textResponse.body).toBe(false);
});

it("passes status code in the response", async () => {
Expand Down
22 changes: 11 additions & 11 deletions test/tools.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@ describe("tools", () => {
});

it.each`
contentType | acceptHeader
${"what/ever"} | ${undefined}
${"text/html"} | ${"text/html"}
${"text/html"} | ${"text/*"}
${"application/json"} | ${"*/json"}
${"text/*"} | ${"text/*"}
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"}
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 }) => {
Expand Down

0 comments on commit 774a98c

Please sign in to comment.