-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdk-identifier.test.ts
More file actions
176 lines (136 loc) · 6.15 KB
/
sdk-identifier.test.ts
File metadata and controls
176 lines (136 loc) · 6.15 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { describe, test, expect, vi } from "vitest";
import { SDKIdentifierHook } from "../../hooks/sdk-identifier.js";
import type { BeforeRequestContext } from "../../hooks/types.js";
describe("SDKIdentifierHook", () => {
const mockContext: BeforeRequestContext = {
baseURL: new URL("http://localhost:8088"),
operationID: "testOperation",
oAuth2Scopes: null,
retryConfig: { strategy: "none" },
resolvedSecurity: null,
options: {},
};
describe("beforeRequest", () => {
test("adds X-Galileo-SDK header to request", async () => {
const hook = new SDKIdentifierHook();
const request = new Request("http://localhost:8088/api/test");
const modifiedRequest = await hook.beforeRequest(mockContext, request);
expect(modifiedRequest.headers.has("X-Galileo-SDK")).toBe(true);
});
test("header contains galileo-generated prefix", async () => {
const hook = new SDKIdentifierHook();
const request = new Request("http://localhost:8088/api/test");
const modifiedRequest = await hook.beforeRequest(mockContext, request);
const headerValue = modifiedRequest.headers.get("X-Galileo-SDK");
expect(headerValue).toMatch(/^galileo-generated\//);
});
test("header contains valid version format", async () => {
const hook = new SDKIdentifierHook();
const request = new Request("http://localhost:8088/api/test");
const modifiedRequest = await hook.beforeRequest(mockContext, request);
const headerValue = modifiedRequest.headers.get("X-Galileo-SDK");
expect(headerValue).toMatch(/^galileo-generated\/\d+\.\d+\.\d+/);
});
test("preserves existing headers", async () => {
const hook = new SDKIdentifierHook();
const request = new Request("http://localhost:8088/api/test", {
headers: {
"Content-Type": "application/json",
Authorization: "Bearer token",
},
});
const modifiedRequest = await hook.beforeRequest(mockContext, request);
expect(modifiedRequest.headers.get("Content-Type")).toBe("application/json");
expect(modifiedRequest.headers.get("Authorization")).toBe("Bearer token");
expect(modifiedRequest.headers.has("X-Galileo-SDK")).toBe(true);
});
test("returns a Request object", async () => {
const hook = new SDKIdentifierHook();
const request = new Request("http://localhost:8088/api/test");
const result = await hook.beforeRequest(mockContext, request);
expect(result instanceof Request).toBe(true);
});
test("does not modify request method or body", async () => {
const hook = new SDKIdentifierHook();
const body = JSON.stringify({ test: "data" });
const request = new Request("http://localhost:8088/api/test", {
method: "POST",
body,
});
const modifiedRequest = await hook.beforeRequest(mockContext, request);
expect(modifiedRequest.method).toBe("POST");
});
test("works with different HTTP methods", async () => {
const hook = new SDKIdentifierHook();
for (const method of ["GET", "POST", "PUT", "DELETE", "PATCH"]) {
const request = new Request("http://localhost:8088/api/test", {
method,
});
const modifiedRequest = await hook.beforeRequest(mockContext, request);
expect(modifiedRequest.headers.has("X-Galileo-SDK")).toBe(true);
expect(modifiedRequest.method).toBe(method);
}
});
test("header value is consistent across multiple calls", async () => {
const hook = new SDKIdentifierHook();
const request1 = new Request("http://localhost:8088/api/test");
const request2 = new Request("http://localhost:8088/api/test");
const modifiedRequest1 = await hook.beforeRequest(mockContext, request1);
const modifiedRequest2 = await hook.beforeRequest(mockContext, request2);
const header1 = modifiedRequest1.headers.get("X-Galileo-SDK");
const header2 = modifiedRequest2.headers.get("X-Galileo-SDK");
expect(header1).toBe(header2);
});
test("overwrites existing X-Galileo-SDK header", async () => {
const hook = new SDKIdentifierHook();
const request = new Request("http://localhost:8088/api/test", {
headers: {
"X-Galileo-SDK": "old-value",
},
});
const modifiedRequest = await hook.beforeRequest(mockContext, request);
const headerValue = modifiedRequest.headers.get("X-Galileo-SDK");
expect(headerValue).toMatch(/^galileo-generated\//);
expect(headerValue).not.toBe("old-value");
});
test("handles loadVersion() failure gracefully with fallback to unknown", async () => {
vi.resetModules();
vi.doMock("../../hooks/sdk-identifier.js", async () => {
return {
SDKIdentifierHook: class {
async beforeRequest(
_hookCtx: BeforeRequestContext,
request: Request,
): Promise<Request> {
const newRequest = request.clone();
const loadVersionWithFailure = (): string => {
try {
throw new Error("Failed to load package.json (ESM runtime issue)");
} catch {
return "unknown";
}
};
const version = loadVersionWithFailure();
const sdkIdentifier = `galileo-generated/${version}`;
newRequest.headers.set("X-Galileo-SDK", sdkIdentifier);
return newRequest;
}
},
};
});
const { SDKIdentifierHook: MockedHook } = await import("../../hooks/sdk-identifier.js");
const hook = new MockedHook();
const request = new Request("http://localhost:8088/api/test");
const modifiedRequest = await hook.beforeRequest(mockContext, request);
const headerValue = modifiedRequest.headers.get("X-Galileo-SDK");
expect(headerValue).toBe("galileo-generated/unknown");
vi.doUnmock("../../hooks/sdk-identifier.js");
});
});
describe("hook integration", () => {
test("hook implements BeforeRequestHook interface", () => {
const hook = new SDKIdentifierHook();
expect(typeof hook.beforeRequest).toBe("function");
});
});
});