Skip to content

Commit

Permalink
Update mock server
Browse files Browse the repository at this point in the history
  • Loading branch information
hansott committed Feb 7, 2025
1 parent d66f58d commit c6814e7
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 22 deletions.
4 changes: 3 additions & 1 deletion end2end/server/src/handlers/lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ module.exports = function lists(req, res) {
},
]
: [],
blockedUserAgents: blockedUserAgents,
blockedUserAgentsV2: Array.isArray(blockedUserAgents)
? blockedUserAgents
: [],
});
};
6 changes: 3 additions & 3 deletions end2end/server/src/handlers/updateLists.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ module.exports = function updateIPLists(req, res) {
updateBlockedIPAddresses(req.app, req.body.blockedIPAddresses);

if (
req.body.blockedUserAgents &&
typeof req.body.blockedUserAgents === "string"
req.body.blockedUserAgentsV2 &&
typeof req.body.blockedUserAgentsV2 === "string"
) {
updateBlockedUserAgents(req.app, req.body.blockedUserAgents);
updateBlockedUserAgents(req.app, req.body.blockedUserAgentsV2);
}

res.json({ success: true });
Expand Down
3 changes: 2 additions & 1 deletion library/agent/Agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { Wrapper } from "./Wrapper";
import { Context } from "./Context";
import { createTestAgent } from "../helpers/createTestAgent";
import { setTimeout } from "node:timers/promises";
import type { Response } from "./api/fetchBlockedLists";

wrap(fetch, "fetch", function mock() {
return async function mock() {
Expand All @@ -42,7 +43,7 @@ wrap(fetch, "fetch", function mock() {
pattern: "Bytespider",
},
],
}),
} satisfies Response),
};
};
});
Expand Down
2 changes: 1 addition & 1 deletion library/agent/ServiceConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { IPMatcher } from "../helpers/ip-matcher/IPMatcher";
import { LimitedContext, matchEndpoints } from "../helpers/matchEndpoints";
import { Endpoint } from "./Config";
import {
Blocklist as BlocklistType,
IPBlocklist as BlocklistType,
AgentBlockList,
} from "./api/fetchBlockedLists";

Expand Down
14 changes: 8 additions & 6 deletions library/agent/api/fetchBlockedLists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { fetch } from "../../helpers/fetch";
import { getAPIURL } from "../getAPIURL";
import { Token } from "./Token";

export type Blocklist = {
export type IPBlocklist = {
key: string;
source: string;
description: string;
Expand All @@ -14,8 +14,13 @@ export type AgentBlockList = {
pattern: string; // e.g. "Googlebot|Bingbot"
};

export type Response = {
blockedIPAddresses: IPBlocklist[];
blockedUserAgentsV2: AgentBlockList[];
};

export async function fetchBlockedLists(token: Token): Promise<{
blockedIPAddresses: Blocklist[];
blockedIPAddresses: IPBlocklist[];
blockedUserAgents: AgentBlockList[];
}> {
const baseUrl = getAPIURL();
Expand All @@ -34,10 +39,7 @@ export async function fetchBlockedLists(token: Token): Promise<{
throw new Error(`Failed to fetch blocked lists: ${statusCode}`);
}

const result: {
blockedIPAddresses: Blocklist[];
blockedUserAgentsV2: string;
} = JSON.parse(body);
const result: Response = JSON.parse(body);

return {
blockedIPAddresses:
Expand Down
7 changes: 2 additions & 5 deletions library/sources/HTTPServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { wrap } from "../helpers/wrap";
import { HTTPServer } from "./HTTPServer";
import { join } from "path";
import { createTestAgent } from "../helpers/createTestAgent";
import type { Blocklist, AgentBlockList } from "../agent/api/fetchBlockedLists";
import type { Response } from "../agent/api/fetchBlockedLists";
import * as fetchBlockedLists from "../agent/api/fetchBlockedLists";
import { mkdtemp, writeFile, unlink } from "fs/promises";
import { exec } from "child_process";
Expand Down Expand Up @@ -51,10 +51,7 @@ const agent = createTestAgent({
agent.start([new HTTPServer()]);

wrap(fetchBlockedLists, "fetchBlockedLists", function fetchBlockedLists() {
return async function fetchBlockedLists(): Promise<{
blockedIPAddresses: Blocklist[];
blockedUserAgentsV2: AgentBlockList[];
}> {
return async function fetchBlockedLists(): Promise<Response> {
return {
blockedIPAddresses: [
{
Expand Down
7 changes: 2 additions & 5 deletions library/sources/Hono.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable prefer-rest-params */
import * as t from "tap";
import type { Blocklist, AgentBlockList } from "../agent/api/fetchBlockedLists";
import type { Response } from "../agent/api/fetchBlockedLists";
import { ReportingAPIForTesting } from "../agent/api/ReportingAPIForTesting";
import { Token } from "../agent/api/Token";
import { setUser } from "../agent/context/user";
Expand Down Expand Up @@ -38,10 +38,7 @@ wrap(fetch, "fetch", function mock(original) {
pattern: "hacker|attacker",
},
],
} satisfies {
blockedIPAddresses: Blocklist[];
blockedUserAgentsV2: AgentBlockList[];
}),
} satisfies Response),
};
}

Expand Down

0 comments on commit c6814e7

Please sign in to comment.