forked from thislooksfun/snoots
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
75fbc27
commit 052c592
Showing
2 changed files
with
203 additions
and
0 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,30 @@ | ||
import type { Data } from "../types"; | ||
import type { Query } from "./core"; | ||
import * as core from "./core"; | ||
|
||
const endpoint = "https://www.reddit.com"; | ||
export async function get<T>( | ||
userAgent: string, | ||
path: string, | ||
query: Query = {} | ||
): Promise<T> { | ||
return core.get(endpoint, `${path}.json`, query, userAgent); | ||
} | ||
|
||
export async function post<T>( | ||
userAgent: string, | ||
path: string, | ||
json: Data, | ||
query: Query = {} | ||
): Promise<T> { | ||
return core.post(endpoint, `${path}.json`, json, query, userAgent); | ||
} | ||
|
||
export async function postForm<T>( | ||
userAgent: string, | ||
path: string, | ||
form: Data, | ||
query: Query = {} | ||
): Promise<T> { | ||
return core.postForm(endpoint, `${path}.json`, form, query, userAgent); | ||
} |
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,173 @@ | ||
import nock from "nock"; | ||
import * as anon from "../../../src/helper/api/anon"; | ||
|
||
afterEach(() => nock.cleanAll()); | ||
afterAll(() => nock.restore()); | ||
|
||
describe("get()", () => { | ||
it("should pass common values", async () => { | ||
const n = nock("https://www.reddit.com") | ||
.get("/foo/bar.json?api_type=json&raw_json=1") | ||
.reply(200, { bim: "bom" }); | ||
|
||
await anon.get("baz", "foo/bar", {}); | ||
|
||
// TODO: Add tests for user agent. | ||
// BODY: Pending https://github.com/nock/nock/issues/2171. | ||
n.done(); | ||
}); | ||
|
||
it("should give back json data", async () => { | ||
const n = nock("https://www.reddit.com") | ||
.get("/foo/bar.json?api_type=json&raw_json=1") | ||
.reply(200, { bim: "bom" }); | ||
|
||
const req = anon.get("baz", "foo/bar", {}); | ||
await expect(req).resolves.toStrictEqual({ bim: "bom" }); | ||
|
||
n.done(); | ||
}); | ||
|
||
describe("when given an api error", () => { | ||
it("should throw", async () => { | ||
const n = nock("https://www.reddit.com") | ||
.get("/foo/bar.json?api_type=json&raw_json=1") | ||
.reply(200, { error: "whoops" }); | ||
|
||
const req = anon.get("baz", "foo/bar", {}); | ||
const err = new Error("Reddit returned an error: whoops"); | ||
await expect(req).rejects.toStrictEqual(err); | ||
|
||
n.done(); | ||
}); | ||
|
||
it("should use the description if available", async () => { | ||
const n = nock("https://www.reddit.com") | ||
.get("/foo/bar.json?api_type=json&raw_json=1") | ||
.reply(200, { | ||
error: "whoops", | ||
error_description: "something went wrong :(", | ||
}); | ||
|
||
const req = anon.get("baz", "foo/bar", {}); | ||
const err = new Error( | ||
"Reddit returned an error: whoops: something went wrong :(" | ||
); | ||
await expect(req).rejects.toStrictEqual(err); | ||
|
||
n.done(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("post()", () => { | ||
it("should pass common values", async () => { | ||
const n = nock("https://www.reddit.com") | ||
.post("/foo/bar.json?api_type=json&raw_json=1") | ||
.reply(200, { bim: "bom" }); | ||
|
||
await anon.post("baz", "foo/bar", { bar: "foo" }, {}); | ||
|
||
// TODO: Add tests for body and user agent. | ||
// BODY: Pending https://github.com/nock/nock/issues/2171. | ||
n.done(); | ||
}); | ||
|
||
it("should give back json data", async () => { | ||
const n = nock("https://www.reddit.com") | ||
.post("/foo/bar.json?api_type=json&raw_json=1") | ||
.reply(200, { bim: "bom" }); | ||
|
||
const req = anon.post("baz", "foo/bar", { bar: "foo" }, {}); | ||
await expect(req).resolves.toStrictEqual({ bim: "bom" }); | ||
|
||
n.done(); | ||
}); | ||
|
||
describe("when given an api error", () => { | ||
it("should throw", async () => { | ||
const n = nock("https://www.reddit.com") | ||
.post("/foo/bar.json?api_type=json&raw_json=1") | ||
.reply(200, { error: "whoops" }); | ||
|
||
const req = anon.post("baz", "foo/bar", { bar: "foo" }, {}); | ||
const err = new Error("Reddit returned an error: whoops"); | ||
await expect(req).rejects.toStrictEqual(err); | ||
|
||
n.done(); | ||
}); | ||
|
||
it("should use the description if available", async () => { | ||
const n = nock("https://www.reddit.com") | ||
.post("/foo/bar.json?api_type=json&raw_json=1") | ||
.reply(200, { | ||
error: "whoops", | ||
error_description: "something went wrong :(", | ||
}); | ||
|
||
const req = anon.post("baz", "foo/bar", { bar: "foo" }, {}); | ||
const err = new Error( | ||
"Reddit returned an error: whoops: something went wrong :(" | ||
); | ||
await expect(req).rejects.toStrictEqual(err); | ||
|
||
n.done(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("postForm()", () => { | ||
it("should pass common values", async () => { | ||
const n = nock("https://www.reddit.com") | ||
.post("/foo/bar.json?api_type=json&raw_json=1") | ||
.reply(200, { bim: "bom" }); | ||
|
||
await anon.postForm("baz", "foo/bar", { bar: "foo" }, {}); | ||
|
||
// TODO: Add tests for body and user agent. | ||
// BODY: Pending https://github.com/nock/nock/issues/2171. | ||
n.done(); | ||
}); | ||
|
||
it("should give back json data", async () => { | ||
const n = nock("https://www.reddit.com") | ||
.post("/foo/bar.json?api_type=json&raw_json=1") | ||
.reply(200, { bim: "bom" }); | ||
|
||
const req = anon.postForm("baz", "foo/bar", { bar: "foo" }, {}); | ||
await expect(req).resolves.toStrictEqual({ bim: "bom" }); | ||
|
||
n.done(); | ||
}); | ||
|
||
describe("when given an api error", () => { | ||
it("should throw", async () => { | ||
const n = nock("https://www.reddit.com") | ||
.post("/foo/bar.json?api_type=json&raw_json=1") | ||
.reply(200, { error: "whoops" }); | ||
|
||
const req = anon.postForm("baz", "foo/bar", { bar: "foo" }, {}); | ||
const err = new Error("Reddit returned an error: whoops"); | ||
await expect(req).rejects.toStrictEqual(err); | ||
|
||
n.done(); | ||
}); | ||
|
||
it("should use the description if available", async () => { | ||
const n = nock("https://www.reddit.com") | ||
.post("/foo/bar.json?api_type=json&raw_json=1") | ||
.reply(200, { | ||
error: "whoops", | ||
error_description: "something went wrong :(", | ||
}); | ||
|
||
const req = anon.postForm("baz", "foo/bar", { bar: "foo" }, {}); | ||
const err = new Error( | ||
"Reddit returned an error: whoops: something went wrong :(" | ||
); | ||
await expect(req).rejects.toStrictEqual(err); | ||
|
||
n.done(); | ||
}); | ||
}); | ||
}); |