-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from pmcelhaney/tools
Add a tools object that will be provided to the request
- Loading branch information
Showing
5 changed files
with
109 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
); | ||
}); |