-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-key.test.ts
66 lines (53 loc) · 2.21 KB
/
api-key.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import path from "path";
import { Server } from "http";
import request from "supertest";
import { startServer } from "@gateweaver/server";
import { checkResponseHeaders } from "../../utils/check-response-headers";
const MOCK_PATH = "/api-key/mock";
const RATE_LIMITED_PATH = "/api-key/rate-limited";
const TEST_API_KEY = "test-api-key";
describe("e2e - API Key Protected Endpoint", () => {
let gateweaver: Server;
beforeAll(async () => {
const configPath = path.join(__dirname, "gateweaver.yml");
gateweaver = await startServer(configPath, false);
});
afterAll(() => {
gateweaver?.close();
});
it("should return a 200 status, correct body and headers when accessing an API Key protected endpoint with a correct API Key", async () => {
const response = await request(gateweaver)
.get(MOCK_PATH)
.set("x-api-key", TEST_API_KEY);
expect(response.status).toBe(200);
expect(response.body).toEqual({ message: "Message from gateweaver query" });
checkResponseHeaders(response);
});
it("should return a 401 status when an API Key protected endpoint is accessed without an API Key", async () => {
const response = await request(gateweaver).get(MOCK_PATH);
expect(response.status).toBe(401);
expect(response.body).toEqual({ error: "API Key Required" });
});
it("should return a 401 status when an API Key protected endpoint is accessed with an invalid API Key", async () => {
const response = await request(gateweaver)
.get(MOCK_PATH)
.set("x-api-key", "invalid-api-key");
expect(response.status).toBe(401);
expect(response.body).toEqual({ error: "Invalid API Key" });
});
it("should return a 429 status when an API Key protected endpoint is rate limited", async () => {
await request(gateweaver)
.get(RATE_LIMITED_PATH)
.set("x-api-key", TEST_API_KEY);
await request(gateweaver)
.get(RATE_LIMITED_PATH)
.set("x-api-key", TEST_API_KEY);
const rateLimitedResponse = await request(gateweaver)
.get(RATE_LIMITED_PATH)
.set("x-api-key", TEST_API_KEY);
expect(rateLimitedResponse.status).toBe(429);
expect(rateLimitedResponse.text).toBe(
"Too many requests, please try again later.",
);
});
});