Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
j3lte committed Nov 24, 2023
1 parent 196d709 commit e817c8b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
1 change: 0 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"lint": "deno lint ./src/",
"test": "deno test --coverage=.coverage",
"coverage": "deno coverage .coverage --lcov --exclude=/src/node/ --exclude=/test/ --exclude=/scripts/ > ./.coverage/coverage.lcov",

"test:watch": "deno test --watch",
"update:version": "deno run --allow-read --allow-write ./scripts/update-version.ts",
"update:deno_deps": "deno run -A https://deno.land/x/udd/main.ts dev_deps.ts ./scripts/build-npm.ts ./src/lib/Pastebin.ts ./src/lib/Scraper.ts",
Expand Down
14 changes: 9 additions & 5 deletions src/lib/Scraper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const convertData = (paste: ScrapePasteRaw): ScrapePaste => {

export class Scraper {
// We're able to overwrite fetch because it is an abstract class
fetch = globalThis.fetch;
#fetch = globalThis.fetch;

#events = new Event<EventTypes>();

Expand Down Expand Up @@ -139,7 +139,7 @@ export class Scraper {
};

if (fetch) {
this.fetch = fetch;
this.#fetch = fetch;
}

if (
Expand Down Expand Up @@ -226,7 +226,7 @@ export class Scraper {
const realKey = key
.replace("https://scrape.pastebin.com/api_scrape_item.php?i=", "")
.replace("https://pastebin.com/", "");
return this.fetch(`${ENDPOINTS.RAW}${realKey}`)
return this.#fetch(`${ENDPOINTS.RAW}${realKey}`)
.then((response) => {
if (!response.ok) {
throw new Error(`Error fetching raw: ${response.statusText}`);
Expand All @@ -245,7 +245,7 @@ export class Scraper {
const realKey = key
.replace("https://scrape.pastebin.com/api_scrape_item.php?i=", "")
.replace("https://pastebin.com/", "");
return this.fetch(`${ENDPOINTS.META}${realKey}`)
return this.#fetch(`${ENDPOINTS.META}${realKey}`)
.then((response) => {
if (!response.ok) {
throw new Error(`Error fetching meta: ${response.statusText}`);
Expand Down Expand Up @@ -283,7 +283,7 @@ export class Scraper {
}
}
const limited = typeof limit !== "undefined" ? limit : this.#limit;
const response = await this.fetch(`${ENDPOINTS.SCRAPE}?limit=${limited}`);
const response = await this.#fetch(`${ENDPOINTS.SCRAPE}?limit=${limited}`);
if (response.status === 403) {
const text = await response.text();
throw new Error(`Pastebin blocked you, have you whitelisted your IP? \n\n Response: ${text}`);
Expand Down Expand Up @@ -339,4 +339,8 @@ export class Scraper {

this.#events.post.call(this.#events, callArgs as never);
}

get fetch(): typeof globalThis.fetch {
return this.#fetch;
}
}
49 changes: 49 additions & 0 deletions test/Scraper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
Deno.test({
name: "Scraper",
fn: async (context) => {
await runTest(context, "constructor", () => {
// overwrite fetch
const fetch = () => {};

const scraper = new Scraper({}, fetch as unknown as typeof globalThis.fetch);
assertEquals(scraper.fetch, fetch as unknown as typeof globalThis.fetch);
});

await runTest(context, "errors", () => {
assertThrows(() => new Scraper({ intervalTime: 0, limit: 0 }));
assertThrows(() => new Scraper({ intervalTime: 1000, limit: 0 }));
Expand All @@ -48,6 +56,14 @@ Deno.test({
mf.intercept("https://scrape.pastebin.com/api_scraping.php?limit=1").response(json(1)).times(
2,
);
mf.intercept("https://scrape.pastebin.com/api_scraping.php?limit=1").response(
JSON.stringify([
{
...simpleScrape,
expire: "1111111111",
},
]),
);

const scraper = new Scraper({});

Expand All @@ -56,6 +72,7 @@ Deno.test({
assertEquals(d.key, "abc");
assertEquals(d.title, "abc");
assertEquals(d.user, "abc");
assertEquals(d.expire, null);
assertEquals(d.size, 123);
assertEquals(d.hits, 123);
assertEquals(d.syntax, "abc");
Expand All @@ -70,6 +87,11 @@ Deno.test({
assertEquals(single[0].hits, 123);
assertEquals(single[0].syntax, "abc");

const second = await scraper.singleScrape(1);

assertEquals(second.length, 1);
assertEquals(typeof second[0].expire, "object");

mf.deactivate();
});

Expand Down Expand Up @@ -114,6 +136,33 @@ Deno.test({
mf.deactivate();
});

await runTest(context, "scrape error test", async () => {
const mf = new MockFetch();
mf.intercept("https://scrape.pastebin.com/api_scraping.php?limit=1").response(
"test",
{ status: 403 },
);

const started = await new Promise((resolve) => {
const scraper = new Scraper({
limit: 1,
breakOnError: true,
});

scraper.on("error", async () => {
await wait(1000).then(() => {
resolve(true);
});
});

scraper.start();
});

assertEquals(started, true);

mf.deactivate();
});

await runTest(context, "scrape raw", async () => {
const mf = new MockFetch();

Expand Down

0 comments on commit e817c8b

Please sign in to comment.