Skip to content

Commit

Permalink
Add bot blocking unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
timokoessler committed Dec 18, 2024
1 parent 080b4c9 commit 5e053cd
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion library/sources/Hono.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ wrap(fetch, "fetch", function mock(original) {
ips: ["1.3.2.0/24", "fe80::1234:5678:abcd:ef12/64"],
},
],
blockedUserAgents: "hacker|attacker",
}),
};
}
Expand Down Expand Up @@ -308,7 +309,7 @@ t.test("works using @hono/node-server (real socket ip)", opts, async (t) => {
server.close();
});

t.test("ip blocking works (real socket)", opts, async (t) => {
t.test("ip and bot blocking works (real socket)", opts, async (t) => {
// Start a server with a real socket
// The blocking is implemented in the HTTPServer source
const { serve } =
Expand Down Expand Up @@ -353,6 +354,19 @@ t.test("ip blocking works (real socket)", opts, async (t) => {
});
t.equal(response3.statusCode, 200);

// Test blocked user agent
const response4 = await fetch.fetch({
url: new URL("http://127.0.0.1:8766/"),
headers: {
"User-Agent": "hacker",
},
});
t.equal(response4.statusCode, 403);
t.equal(
response4.body,
"You are not allowed to access this resource because you have been identified as a bot."
);

// Cleanup server
server.close();
});
Expand Down

0 comments on commit 5e053cd

Please sign in to comment.