@zereight/mcp-gitlab 2.1.70 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.ko.md +3 -1
- package/README.md +6 -2
- package/README.pt-BR.md +1 -1
- package/README.zh-CN.md +3 -1
- package/build/config.js +12 -4
- package/build/deprecated-env.js +47 -0
- package/build/index.js +369 -184
- package/build/oauth-device-flow.js +80 -0
- package/build/oauth.js +135 -18
- package/build/schemas.js +1 -1
- package/build/server/remote-http-stack.js +39 -0
- package/build/tools/discover-activation.js +38 -0
- package/build/tools/exposed-tools.js +35 -0
- package/build/tools/listed-tools.js +76 -0
- package/build/tools/registry.js +226 -57
- package/build/tools/tool-profile.js +66 -0
- package/build/utils/compact-tool-allowlist.js +14 -0
- package/build/utils/logger.js +34 -12
- package/package.json +3 -3
- package/build/scripts/check-skill-sync.js +0 -143
- package/build/scripts/generate-server-card.js +0 -39
- package/build/scripts/generate-tool-coverage.js +0 -58
- package/build/scripts/generate-tool-docs.js +0 -430
- package/build/scripts/tool-coverage/coverage.js +0 -341
- package/build/scripts/tool-coverage/coverage.test.js +0 -142
- package/build/test/callback-proxy-tests.js +0 -321
- package/build/test/cli/args.test.js +0 -175
- package/build/test/cli/boolean-flags.test.js +0 -106
- package/build/test/cli/compact-mcp-result.test.js +0 -93
- package/build/test/cli/credentials.test.js +0 -153
- package/build/test/cli/exposure.test.js +0 -104
- package/build/test/cli/load-config-snapshot.js +0 -5
- package/build/test/cli/output.test.js +0 -74
- package/build/test/cli/replay-command.test.js +0 -37
- package/build/test/cli/router.test.js +0 -183
- package/build/test/client-pool-test.js +0 -109
- package/build/test/clients/client.js +0 -34
- package/build/test/clients/custom-header-client.js +0 -122
- package/build/test/clients/sse-client.js +0 -95
- package/build/test/clients/stdio-client.js +0 -113
- package/build/test/clients/streamable-http-client.js +0 -95
- package/build/test/config-allowed-groups.test.js +0 -97
- package/build/test/create-commit-actions.test.js +0 -102
- package/build/test/draft-note-schema.test.js +0 -44
- package/build/test/dynamic-api-url-allowlist.test.js +0 -121
- package/build/test/dynamic-api-url-test.js +0 -304
- package/build/test/dynamic-routing-tests.js +0 -495
- package/build/test/get-merge-request-discussion-schema.test.js +0 -30
- package/build/test/gitlab-api-tool-errors.test.js +0 -112
- package/build/test/gitlab-artifact-entry-schema.test.js +0 -44
- package/build/test/gitlab-diff-schema.test.js +0 -36
- package/build/test/gitlab-pipeline-schedule-schema.test.js +0 -32
- package/build/test/gitlab-tag-schema.test.js +0 -51
- package/build/test/group-milestone-schema.test.js +0 -45
- package/build/test/mcp-oauth-tests.js +0 -905
- package/build/test/mcp-server-name.test.js +0 -87
- package/build/test/merge-merge-request-schema.test.js +0 -75
- package/build/test/multi-server-test.js +0 -190
- package/build/test/no-proxy-integration-test.js +0 -183
- package/build/test/no-proxy-test.js +0 -149
- package/build/test/nullable-gitlab-response-fields.test.js +0 -37
- package/build/test/nullish-tool-arguments-schema.test.js +0 -118
- package/build/test/oauth-device-flow-tests.js +0 -450
- package/build/test/oauth-download-url.test.js +0 -157
- package/build/test/oauth-startup.test.js +0 -204
- package/build/test/oauth-tests.js +0 -428
- package/build/test/path-segment-encoding.test.js +0 -98
- package/build/test/remote-auth-simple-test.js +0 -277
- package/build/test/remote-auth-tests.js +0 -314
- package/build/test/response-masking.test.js +0 -445
- package/build/test/schema-tests.js +0 -1679
- package/build/test/server/metrics.test.js +0 -47
- package/build/test/server/request-helpers.test.js +0 -48
- package/build/test/server/version.test.js +0 -13
- package/build/test/sse-auth-guard.test.js +0 -96
- package/build/test/sse-session-limits.test.js +0 -273
- package/build/test/stateless/callback-proxy.test.js +0 -510
- package/build/test/stateless/client-id.test.js +0 -177
- package/build/test/stateless/codec.test.js +0 -328
- package/build/test/stateless/config-ttl.test.js +0 -149
- package/build/test/stateless/consumed-proxy-code-cache.test.js +0 -150
- package/build/test/stateless/session-id-integration.test.js +0 -675
- package/build/test/stateless/session-id.test.js +0 -157
- package/build/test/streamable-http-concurrent-session.test.js +0 -171
- package/build/test/streamable-http-dns-rebinding.test.js +0 -185
- package/build/test/streamable-http-sse-stream.test.js +0 -350
- package/build/test/streamable-http-static-token-auth.test.js +0 -157
- package/build/test/streamable-http-unauthenticated-discovery.test.js +0 -207
- package/build/test/test-all-transport-server.js +0 -255
- package/build/test/test-auth-retry.js +0 -195
- package/build/test/test-blank-filters.js +0 -156
- package/build/test/test-ci-catalog.js +0 -177
- package/build/test/test-ci-lint.js +0 -191
- package/build/test/test-ci-variables.js +0 -309
- package/build/test/test-create-repository.js +0 -120
- package/build/test/test-dependency-proxy.js +0 -195
- package/build/test/test-deployment-tools.js +0 -463
- package/build/test/test-download-attachment.js +0 -190
- package/build/test/test-dynamic-project-scope.js +0 -524
- package/build/test/test-empty-toolsets.js +0 -535
- package/build/test/test-get-file-blame.js +0 -145
- package/build/test/test-get-file-contents.js +0 -128
- package/build/test/test-geteffectiveprojectid.js +0 -804
- package/build/test/test-health-check.js +0 -115
- package/build/test/test-issue-description-patch.js +0 -256
- package/build/test/test-job-artifacts.js +0 -275
- package/build/test/test-json-schema.js +0 -166
- package/build/test/test-list-group-members.js +0 -150
- package/build/test/test-list-group-merge-requests.js +0 -165
- package/build/test/test-list-issues.js +0 -170
- package/build/test/test-list-merge-requests.js +0 -201
- package/build/test/test-list-milestones.js +0 -145
- package/build/test/test-list-project-members.js +0 -132
- package/build/test/test-merge-request-approval-state-tools.js +0 -213
- package/build/test/test-merge-request-approvals.js +0 -187
- package/build/test/test-merge-request-pipelines.js +0 -106
- package/build/test/test-mr-diffs-filter.js +0 -132
- package/build/test/test-mr-file-diffs.js +0 -251
- package/build/test/test-oauth-proxy-rate-limit.js +0 -133
- package/build/test/test-orbit.js +0 -155
- package/build/test/test-permission-mode.js +0 -424
- package/build/test/test-pipeline-schedules.js +0 -484
- package/build/test/test-protected-branches.js +0 -174
- package/build/test/test-remote-downloads.js +0 -510
- package/build/test/test-search-code.js +0 -275
- package/build/test/test-snippets.js +0 -735
- package/build/test/test-tags.js +0 -220
- package/build/test/test-todos.js +0 -195
- package/build/test/test-token-optimizations.js +0 -691
- package/build/test/test-toolset-filtering.js +0 -499
- package/build/test/test-update-issue-slim.js +0 -141
- package/build/test/test-update-project.js +0 -112
- package/build/test/test-upload-markdown.js +0 -206
- package/build/test/test-vulnerabilities.js +0 -399
- package/build/test/test-webhooks.js +0 -218
- package/build/test/tool-description-quality.test.js +0 -50
- package/build/test/utils/bulk-publish-options.test.js +0 -32
- package/build/test/utils/download-token.test.js +0 -35
- package/build/test/utils/forwarded-public-base-url.test.js +0 -46
- package/build/test/utils/gitlab-instance-version.test.js +0 -32
- package/build/test/utils/gitlab-version-gate.test.js +0 -74
- package/build/test/utils/graphql-query.test.js +0 -168
- package/build/test/utils/helpers-path.test.js +0 -104
- package/build/test/utils/jmespath-tool-result.test.js +0 -91
- package/build/test/utils/masking.test.js +0 -384
- package/build/test/utils/merge-request-position.test.js +0 -57
- package/build/test/utils/mock-gitlab-server.js +0 -614
- package/build/test/utils/proxy-client-ip.test.js +0 -28
- package/build/test/utils/redact-sensitive.test.js +0 -32
- package/build/test/utils/safe-redirect-fetch.test.js +0 -463
- package/build/test/utils/server-launcher.js +0 -231
- package/build/test/utils/tool-args.test.js +0 -210
- package/build/test/utils/url.test.js +0 -141
- package/build/test/utils/version-check.test.js +0 -239
- package/build/test/utils/wiki-title.test.js +0 -21
- package/build/test-note.js +0 -54
- package/build/test-resolve-issue-note.js +0 -127
|
@@ -1,207 +0,0 @@
|
|
|
1
|
-
import { after, describe, test } from "node:test";
|
|
2
|
-
import assert from "node:assert";
|
|
3
|
-
import { cleanupServers, findAvailablePort, HOST, launchServer, TransportMode, } from "./utils/server-launcher.js";
|
|
4
|
-
async function rawMcpRequest(url, body, headers = {}) {
|
|
5
|
-
const response = await fetch(url, {
|
|
6
|
-
method: "POST",
|
|
7
|
-
headers: {
|
|
8
|
-
"Content-Type": "application/json",
|
|
9
|
-
Accept: "application/json, text/event-stream",
|
|
10
|
-
...headers,
|
|
11
|
-
},
|
|
12
|
-
body: JSON.stringify(body),
|
|
13
|
-
});
|
|
14
|
-
const sessionId = response.headers.get("mcp-session-id");
|
|
15
|
-
if (response.status === 202 || response.status === 204) {
|
|
16
|
-
return { status: response.status, data: null, sessionId, text: "" };
|
|
17
|
-
}
|
|
18
|
-
const text = await response.text();
|
|
19
|
-
const contentType = response.headers.get("content-type") ?? "";
|
|
20
|
-
if (contentType.includes("text/event-stream")) {
|
|
21
|
-
const dataLines = text
|
|
22
|
-
.split("\n")
|
|
23
|
-
.filter(line => line.startsWith("data: "))
|
|
24
|
-
.map(line => line.slice(6));
|
|
25
|
-
return {
|
|
26
|
-
status: response.status,
|
|
27
|
-
data: dataLines.length > 0 ? JSON.parse(dataLines.at(-1)) : null,
|
|
28
|
-
sessionId,
|
|
29
|
-
text,
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
return {
|
|
33
|
-
status: response.status,
|
|
34
|
-
data: text ? JSON.parse(text) : null,
|
|
35
|
-
sessionId,
|
|
36
|
-
text,
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
const initializeBody = {
|
|
40
|
-
jsonrpc: "2.0",
|
|
41
|
-
id: 1,
|
|
42
|
-
method: "initialize",
|
|
43
|
-
params: {
|
|
44
|
-
protocolVersion: "2025-03-26",
|
|
45
|
-
capabilities: {},
|
|
46
|
-
clientInfo: { name: "unauth-discovery-test", version: "1.0.0" },
|
|
47
|
-
},
|
|
48
|
-
};
|
|
49
|
-
async function initialize(mcpUrl) {
|
|
50
|
-
const response = await rawMcpRequest(mcpUrl, initializeBody);
|
|
51
|
-
assert.strictEqual(response.status, 200, response.text);
|
|
52
|
-
assert.ok(response.sessionId, "initialize should return Mcp-Session-Id");
|
|
53
|
-
return response.sessionId;
|
|
54
|
-
}
|
|
55
|
-
let portOffset = 0;
|
|
56
|
-
async function launchRemoteAuthServer(extraEnv = {}) {
|
|
57
|
-
// Keep above remote-auth-simple-test timeout suite (3500+) to avoid parallel bind races.
|
|
58
|
-
const port = await findAvailablePort(3600 + portOffset++ * 10);
|
|
59
|
-
const server = await launchServer({
|
|
60
|
-
mode: TransportMode.STREAMABLE_HTTP,
|
|
61
|
-
port,
|
|
62
|
-
timeout: 10_000,
|
|
63
|
-
env: {
|
|
64
|
-
STREAMABLE_HTTP: "true",
|
|
65
|
-
REMOTE_AUTHORIZATION: "true",
|
|
66
|
-
GITLAB_API_URL: "https://gitlab.example.com/api/v4",
|
|
67
|
-
...extraEnv,
|
|
68
|
-
},
|
|
69
|
-
});
|
|
70
|
-
return { server, mcpUrl: `http://${HOST}:${port}/mcp` };
|
|
71
|
-
}
|
|
72
|
-
describe("Streamable HTTP unauthenticated tool discovery", { timeout: 20_000 }, () => {
|
|
73
|
-
let servers = [];
|
|
74
|
-
after(() => {
|
|
75
|
-
cleanupServers(servers);
|
|
76
|
-
servers = [];
|
|
77
|
-
});
|
|
78
|
-
test("keeps unauthenticated initialize blocked by default", async () => {
|
|
79
|
-
const { server, mcpUrl } = await launchRemoteAuthServer();
|
|
80
|
-
servers.push(server);
|
|
81
|
-
const initResponse = await rawMcpRequest(mcpUrl, initializeBody);
|
|
82
|
-
assert.strictEqual(initResponse.status, 401, initResponse.text);
|
|
83
|
-
assert.strictEqual(initResponse.sessionId, null);
|
|
84
|
-
});
|
|
85
|
-
test("rejects invalid auth headers even when unauthenticated discovery is enabled", async () => {
|
|
86
|
-
const { server, mcpUrl } = await launchRemoteAuthServer({
|
|
87
|
-
GITLAB_ALLOW_UNAUTHENTICATED_TOOL_DISCOVERY: "true",
|
|
88
|
-
});
|
|
89
|
-
servers.push(server);
|
|
90
|
-
const initResponse = await rawMcpRequest(mcpUrl, initializeBody, {
|
|
91
|
-
Authorization: "Bearer definitely-not-valid",
|
|
92
|
-
});
|
|
93
|
-
assert.strictEqual(initResponse.status, 401, initResponse.text);
|
|
94
|
-
assert.strictEqual(initResponse.sessionId, null);
|
|
95
|
-
});
|
|
96
|
-
test("allows unauthenticated tools/list when explicitly enabled", async () => {
|
|
97
|
-
const { server, mcpUrl } = await launchRemoteAuthServer({
|
|
98
|
-
GITLAB_ALLOW_UNAUTHENTICATED_TOOL_DISCOVERY: "true",
|
|
99
|
-
});
|
|
100
|
-
servers.push(server);
|
|
101
|
-
const sessionId = await initialize(mcpUrl);
|
|
102
|
-
const initialized = await rawMcpRequest(mcpUrl, { jsonrpc: "2.0", method: "notifications/initialized" }, { "mcp-session-id": sessionId });
|
|
103
|
-
assert.ok([200, 202, 204].includes(initialized.status), initialized.text);
|
|
104
|
-
const listResponse = await rawMcpRequest(mcpUrl, { jsonrpc: "2.0", id: 2, method: "tools/list", params: {} }, { "mcp-session-id": sessionId });
|
|
105
|
-
assert.strictEqual(listResponse.status, 200, listResponse.text);
|
|
106
|
-
assert.ok(Array.isArray(listResponse.data.result?.tools), "tools/list should return tools");
|
|
107
|
-
assert.ok(listResponse.data.result.tools.length > 0, "tools/list should not be empty");
|
|
108
|
-
});
|
|
109
|
-
test("allows unauthenticated server/discover in individual and batch requests", async () => {
|
|
110
|
-
const { server, mcpUrl } = await launchRemoteAuthServer({
|
|
111
|
-
GITLAB_ALLOW_UNAUTHENTICATED_TOOL_DISCOVERY: "true",
|
|
112
|
-
});
|
|
113
|
-
servers.push(server);
|
|
114
|
-
const preInitDiscover = await rawMcpRequest(mcpUrl, {
|
|
115
|
-
jsonrpc: "2.0",
|
|
116
|
-
id: 10,
|
|
117
|
-
method: "server/discover",
|
|
118
|
-
params: {},
|
|
119
|
-
});
|
|
120
|
-
assert.strictEqual(preInitDiscover.status, 400, preInitDiscover.text);
|
|
121
|
-
assert.match(preInitDiscover.data?.error?.message ?? "", /Server not initialized/i, "server/discover should pass the auth gate before MCP initialization errors");
|
|
122
|
-
const sessionId = await initialize(mcpUrl);
|
|
123
|
-
const initialized = await rawMcpRequest(mcpUrl, { jsonrpc: "2.0", method: "notifications/initialized" }, { "mcp-session-id": sessionId });
|
|
124
|
-
assert.ok([200, 202, 204].includes(initialized.status), initialized.text);
|
|
125
|
-
const discoverResponse = await rawMcpRequest(mcpUrl, { jsonrpc: "2.0", id: 11, method: "server/discover", params: {} }, { "mcp-session-id": sessionId });
|
|
126
|
-
assert.strictEqual(discoverResponse.status, 200, discoverResponse.text);
|
|
127
|
-
assert.strictEqual(discoverResponse.data?.id, 11);
|
|
128
|
-
assert.strictEqual(discoverResponse.data?.error?.code, -32601);
|
|
129
|
-
assert.match(discoverResponse.data?.error?.message ?? "", /Method not found/i);
|
|
130
|
-
const batchResponse = await rawMcpRequest(mcpUrl, [
|
|
131
|
-
{ jsonrpc: "2.0", id: 12, method: "server/discover", params: {} },
|
|
132
|
-
{ jsonrpc: "2.0", id: 13, method: "tools/list", params: {} },
|
|
133
|
-
], { "mcp-session-id": sessionId });
|
|
134
|
-
assert.strictEqual(batchResponse.status, 200, batchResponse.text);
|
|
135
|
-
assert.ok(Array.isArray(batchResponse.data.result?.tools), "batch tools/list should return tools");
|
|
136
|
-
assert.ok(batchResponse.data.result.tools.length > 0, "batch tools/list should not be empty");
|
|
137
|
-
});
|
|
138
|
-
test("rejects unauthenticated discovery batch when tools/call is included", async () => {
|
|
139
|
-
const { server, mcpUrl } = await launchRemoteAuthServer({
|
|
140
|
-
GITLAB_ALLOW_UNAUTHENTICATED_TOOL_DISCOVERY: "true",
|
|
141
|
-
});
|
|
142
|
-
servers.push(server);
|
|
143
|
-
const response = await rawMcpRequest(mcpUrl, [
|
|
144
|
-
{ jsonrpc: "2.0", id: 20, method: "server/discover", params: {} },
|
|
145
|
-
{
|
|
146
|
-
jsonrpc: "2.0",
|
|
147
|
-
id: 21,
|
|
148
|
-
method: "tools/call",
|
|
149
|
-
params: {
|
|
150
|
-
name: "get_project",
|
|
151
|
-
arguments: { project_id: "123" },
|
|
152
|
-
},
|
|
153
|
-
},
|
|
154
|
-
]);
|
|
155
|
-
assert.strictEqual(response.status, 401, response.text);
|
|
156
|
-
});
|
|
157
|
-
test("expires unauthenticated discovery sessions and frees capacity", async () => {
|
|
158
|
-
const { server, mcpUrl } = await launchRemoteAuthServer({
|
|
159
|
-
GITLAB_ALLOW_UNAUTHENTICATED_TOOL_DISCOVERY: "true",
|
|
160
|
-
MAX_SESSIONS: "1",
|
|
161
|
-
MAX_REQUESTS_PER_MINUTE: "1",
|
|
162
|
-
MCP_TRUST_PROXY: "true",
|
|
163
|
-
SESSION_TIMEOUT_SECONDS: "1",
|
|
164
|
-
});
|
|
165
|
-
servers.push(server);
|
|
166
|
-
const expiredSessionId = await initialize(mcpUrl);
|
|
167
|
-
await new Promise(resolve => setTimeout(resolve, 2_000));
|
|
168
|
-
const expiredResponse = await rawMcpRequest(mcpUrl, { jsonrpc: "2.0", id: 2, method: "tools/list", params: {} }, { "mcp-session-id": expiredSessionId });
|
|
169
|
-
assert.strictEqual(expiredResponse.status, 404, expiredResponse.text);
|
|
170
|
-
assert.deepStrictEqual(expiredResponse.data, { error: "Session not found" });
|
|
171
|
-
const healthResponse = await fetch(mcpUrl.replace("/mcp", "/health"));
|
|
172
|
-
assert.strictEqual(healthResponse.status, 200);
|
|
173
|
-
const nextSession = await rawMcpRequest(mcpUrl, initializeBody, {
|
|
174
|
-
"x-forwarded-for": "192.0.2.10",
|
|
175
|
-
});
|
|
176
|
-
assert.strictEqual(nextSession.status, 200, nextSession.text);
|
|
177
|
-
assert.ok(nextSession.sessionId, "expired discovery session should free capacity");
|
|
178
|
-
});
|
|
179
|
-
test("rejects inherited object-property names as unknown session IDs", async () => {
|
|
180
|
-
const { server, mcpUrl } = await launchRemoteAuthServer({
|
|
181
|
-
GITLAB_ALLOW_UNAUTHENTICATED_TOOL_DISCOVERY: "true",
|
|
182
|
-
});
|
|
183
|
-
servers.push(server);
|
|
184
|
-
for (const sessionId of ["constructor", "toString", "__proto__"]) {
|
|
185
|
-
const response = await rawMcpRequest(mcpUrl, { jsonrpc: "2.0", id: 2, method: "tools/list", params: {} }, { "mcp-session-id": sessionId });
|
|
186
|
-
assert.strictEqual(response.status, 404, `${sessionId}: ${response.text}`);
|
|
187
|
-
assert.deepStrictEqual(response.data, { error: "Session not found" });
|
|
188
|
-
}
|
|
189
|
-
});
|
|
190
|
-
test("still blocks unauthenticated tools/call when discovery is enabled", async () => {
|
|
191
|
-
const { server, mcpUrl } = await launchRemoteAuthServer({
|
|
192
|
-
GITLAB_ALLOW_UNAUTHENTICATED_TOOL_DISCOVERY: "true",
|
|
193
|
-
});
|
|
194
|
-
servers.push(server);
|
|
195
|
-
const sessionId = await initialize(mcpUrl);
|
|
196
|
-
const callResponse = await rawMcpRequest(mcpUrl, {
|
|
197
|
-
jsonrpc: "2.0",
|
|
198
|
-
id: 3,
|
|
199
|
-
method: "tools/call",
|
|
200
|
-
params: {
|
|
201
|
-
name: "get_project",
|
|
202
|
-
arguments: { project_id: "123" },
|
|
203
|
-
},
|
|
204
|
-
}, { "mcp-session-id": sessionId });
|
|
205
|
-
assert.strictEqual(callResponse.status, 401, callResponse.text);
|
|
206
|
-
});
|
|
207
|
-
});
|
|
@@ -1,255 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* GitLab MCP Server Transport Tests
|
|
3
|
-
* Tests all three transport modes: stdio, SSE, and streamable-http
|
|
4
|
-
*/
|
|
5
|
-
import * as path from 'path';
|
|
6
|
-
import { describe, test, after, before } from 'node:test';
|
|
7
|
-
import assert from 'node:assert';
|
|
8
|
-
import { launchServer, findAvailablePort, cleanupServers, TransportMode, checkHealthEndpoint, HOST } from './utils/server-launcher.js';
|
|
9
|
-
import { StdioTestClient } from './clients/stdio-client.js';
|
|
10
|
-
import { SSETestClient } from './clients/sse-client.js';
|
|
11
|
-
import { StreamableHTTPTestClient } from './clients/streamable-http-client.js';
|
|
12
|
-
console.log('🚀 GitLab MCP Server Tests');
|
|
13
|
-
console.log('');
|
|
14
|
-
// Configuration check
|
|
15
|
-
const GITLAB_API_URL = process.env.GITLAB_API_URL || "https://gitlab.com";
|
|
16
|
-
const GITLAB_TOKEN = process.env.GITLAB_TOKEN_TEST || process.env.GITLAB_TOKEN;
|
|
17
|
-
const TEST_PROJECT_ID = process.env.TEST_PROJECT_ID;
|
|
18
|
-
console.log('🔧 Test Configuration:');
|
|
19
|
-
console.log(` GitLab URL: ${GITLAB_API_URL}`);
|
|
20
|
-
console.log(` Token: ${GITLAB_TOKEN ? '✅ Provided' : '❌ Missing'}`);
|
|
21
|
-
console.log(` Project ID: ${TEST_PROJECT_ID || '❌ Missing'}`);
|
|
22
|
-
// Validate required configuration
|
|
23
|
-
if (!GITLAB_TOKEN) {
|
|
24
|
-
console.error('❌ Error: GITLAB_TOKEN_TEST or GITLAB_TOKEN environment variable is required for testing');
|
|
25
|
-
console.error(' Set one of these variables to your GitLab API token');
|
|
26
|
-
process.exit(1);
|
|
27
|
-
}
|
|
28
|
-
if (!TEST_PROJECT_ID) {
|
|
29
|
-
console.error('❌ Error: TEST_PROJECT_ID environment variable is required for testing');
|
|
30
|
-
console.error(' Set this variable to a valid GitLab project ID (e.g., "123" or "group/project")');
|
|
31
|
-
process.exit(1);
|
|
32
|
-
}
|
|
33
|
-
console.log('✅ Configuration validated');
|
|
34
|
-
console.log('');
|
|
35
|
-
let servers = [];
|
|
36
|
-
// Cleanup function for all tests
|
|
37
|
-
const cleanup = () => {
|
|
38
|
-
cleanupServers(servers);
|
|
39
|
-
servers = [];
|
|
40
|
-
};
|
|
41
|
-
// Handle process termination
|
|
42
|
-
process.on('SIGINT', cleanup);
|
|
43
|
-
process.on('SIGTERM', cleanup);
|
|
44
|
-
process.on('exit', cleanup);
|
|
45
|
-
describe('GitLab MCP Server - Stdio Transport', () => {
|
|
46
|
-
let client;
|
|
47
|
-
// Prepare environment variables for stdio server
|
|
48
|
-
const stdioEnv = {
|
|
49
|
-
GITLAB_PERSONAL_ACCESS_TOKEN: GITLAB_TOKEN,
|
|
50
|
-
GITLAB_API_URL: `${GITLAB_API_URL}/api/v4`,
|
|
51
|
-
GITLAB_PROJECT_ID: TEST_PROJECT_ID,
|
|
52
|
-
GITLAB_READ_ONLY_MODE: 'true',
|
|
53
|
-
// Explicitly disable other transport modes to ensure stdio mode
|
|
54
|
-
SSE: 'false',
|
|
55
|
-
STREAMABLE_HTTP: 'false'
|
|
56
|
-
};
|
|
57
|
-
before(async () => {
|
|
58
|
-
client = new StdioTestClient();
|
|
59
|
-
const serverPath = path.resolve(process.cwd(), 'build/index.js');
|
|
60
|
-
await client.connect(serverPath, stdioEnv);
|
|
61
|
-
assert.ok(client.isConnected, 'Client should be connected');
|
|
62
|
-
console.log('Client connected to stdio server');
|
|
63
|
-
});
|
|
64
|
-
after(async () => {
|
|
65
|
-
if (client && client.isConnected) {
|
|
66
|
-
await client.disconnect();
|
|
67
|
-
}
|
|
68
|
-
});
|
|
69
|
-
test('should list tools via stdio', async () => {
|
|
70
|
-
const tools = await client.listTools();
|
|
71
|
-
assert.ok(tools !== null && tools !== undefined, 'Tools response should be defined');
|
|
72
|
-
assert.ok('tools' in tools, 'Response should have tools property');
|
|
73
|
-
assert.ok(Array.isArray(tools.tools) && tools.tools.length > 0, 'Tools array should not be empty');
|
|
74
|
-
// Check for specific GitLab tools with proper typing
|
|
75
|
-
const toolNames = tools.tools.map(tool => tool.name);
|
|
76
|
-
assert.ok(toolNames.includes('list_merge_requests'), 'Should have list_merge_requests tool');
|
|
77
|
-
assert.ok(toolNames.includes('get_project'), 'Should have get_project tool');
|
|
78
|
-
// Verify tools have proper structure
|
|
79
|
-
const gitlabTools = tools.tools.filter(tool => tool.name === 'list_merge_requests' || tool.name === 'get_project');
|
|
80
|
-
assert.ok(gitlabTools.length >= 2, 'Should have at least 2 GitLab tools');
|
|
81
|
-
for (const tool of gitlabTools) {
|
|
82
|
-
assert.ok(tool.description !== null && tool.description !== undefined, `Tool ${tool.name} should have description`);
|
|
83
|
-
assert.ok('inputSchema' in tool, `Tool ${tool.name} should have input schema`);
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
test('should call list_merge_requests tool via stdio', async () => {
|
|
87
|
-
const result = await client.callTool('list_merge_requests', {
|
|
88
|
-
project_id: TEST_PROJECT_ID
|
|
89
|
-
});
|
|
90
|
-
assert.ok(result !== null && result !== undefined, 'Tool call result should be defined');
|
|
91
|
-
assert.ok('content' in result, 'Result should have content property');
|
|
92
|
-
});
|
|
93
|
-
test('should call get_project tool via stdio', async () => {
|
|
94
|
-
const result = await client.callTool('get_project', {
|
|
95
|
-
project_id: TEST_PROJECT_ID
|
|
96
|
-
});
|
|
97
|
-
// Verify proper CallToolResult structure
|
|
98
|
-
assert.ok(result !== null && result !== undefined, 'Tool call result should be defined');
|
|
99
|
-
assert.ok('content' in result, 'Result should have content property');
|
|
100
|
-
assert.ok(Array.isArray(result.content), 'Content should be an array');
|
|
101
|
-
assert.ok(result.content.length > 0, 'Content array should not be empty');
|
|
102
|
-
// Check content structure
|
|
103
|
-
const firstContent = result.content[0];
|
|
104
|
-
assert.ok(firstContent !== null && firstContent !== undefined, 'First content item should be defined');
|
|
105
|
-
assert.ok('type' in firstContent, 'Content item should have type');
|
|
106
|
-
assert.strictEqual(firstContent.type, 'text', 'Content type should be text');
|
|
107
|
-
assert.ok('text' in firstContent, 'Text content should have text property');
|
|
108
|
-
// Verify it's valid JSON containing project info
|
|
109
|
-
const projectData = JSON.parse(firstContent.text);
|
|
110
|
-
assert.ok(projectData !== null && projectData !== undefined, 'Project data should be parseable JSON');
|
|
111
|
-
assert.ok('id' in projectData, 'Project should have id');
|
|
112
|
-
assert.ok('name' in projectData, 'Project should have name');
|
|
113
|
-
});
|
|
114
|
-
});
|
|
115
|
-
describe('GitLab MCP Server - SSE Transport', () => {
|
|
116
|
-
let server;
|
|
117
|
-
let client;
|
|
118
|
-
let port;
|
|
119
|
-
before(async () => {
|
|
120
|
-
port = await findAvailablePort();
|
|
121
|
-
server = await launchServer({
|
|
122
|
-
mode: TransportMode.SSE,
|
|
123
|
-
port,
|
|
124
|
-
timeout: 3000,
|
|
125
|
-
env: {
|
|
126
|
-
SSE: 'true',
|
|
127
|
-
STREAMABLE_HTTP: 'false'
|
|
128
|
-
}
|
|
129
|
-
});
|
|
130
|
-
servers.push(server);
|
|
131
|
-
// Verify server started successfully
|
|
132
|
-
assert.ok(server.process.pid !== undefined, 'Server process should have PID');
|
|
133
|
-
assert.strictEqual(server.mode, TransportMode.SSE, 'Server mode should be SSE');
|
|
134
|
-
assert.strictEqual(server.port, port, 'Server should use correct port');
|
|
135
|
-
// Verify health check
|
|
136
|
-
const health = await checkHealthEndpoint(server.port);
|
|
137
|
-
assert.strictEqual(health.status, 'healthy', 'Health status should be healthy');
|
|
138
|
-
assert.strictEqual(health.transport, 'sse', 'Transport should be SSE');
|
|
139
|
-
assert.ok(health.version !== null && health.version !== undefined, 'Version should be defined');
|
|
140
|
-
// Create and connect client
|
|
141
|
-
client = new SSETestClient();
|
|
142
|
-
await client.connect(`http://${HOST}:${port}/sse`);
|
|
143
|
-
assert.ok(client.isConnected, 'Client should be connected');
|
|
144
|
-
assert.ok(await client.testConnection(), 'Connection test should pass');
|
|
145
|
-
console.log('Client connected to SSE server');
|
|
146
|
-
});
|
|
147
|
-
after(async () => {
|
|
148
|
-
if (client && client.isConnected) {
|
|
149
|
-
await client.disconnect();
|
|
150
|
-
}
|
|
151
|
-
cleanup();
|
|
152
|
-
console.log('Client disconnected from SSE server');
|
|
153
|
-
});
|
|
154
|
-
test('should list tools via SSE', async () => {
|
|
155
|
-
const tools = await client.listTools();
|
|
156
|
-
assert.ok(tools !== null && tools !== undefined, 'Tools response should be defined');
|
|
157
|
-
assert.ok('tools' in tools, 'Response should have tools property');
|
|
158
|
-
assert.ok(Array.isArray(tools.tools) && tools.tools.length > 0, 'Tools array should not be empty');
|
|
159
|
-
// Check for specific GitLab tools
|
|
160
|
-
const toolNames = tools.tools.map((tool) => tool.name);
|
|
161
|
-
assert.ok(toolNames.includes('list_merge_requests'), 'Should have list_merge_requests tool');
|
|
162
|
-
assert.ok(toolNames.includes('get_project'), 'Should have get_project tool');
|
|
163
|
-
});
|
|
164
|
-
test('should call list_merge_requests tool via SSE', async () => {
|
|
165
|
-
const result = await client.callTool('list_merge_requests', {
|
|
166
|
-
project_id: TEST_PROJECT_ID
|
|
167
|
-
});
|
|
168
|
-
assert.ok(result !== null && result !== undefined, 'Tool call result should be defined');
|
|
169
|
-
assert.ok('content' in result, 'Result should have content property');
|
|
170
|
-
});
|
|
171
|
-
test('should call get_project tool via SSE', async () => {
|
|
172
|
-
const result = await client.callTool('get_project', {
|
|
173
|
-
project_id: TEST_PROJECT_ID
|
|
174
|
-
});
|
|
175
|
-
assert.ok(result !== null && result !== undefined, 'Tool call result should be defined');
|
|
176
|
-
assert.ok('content' in result, 'Result should have content property');
|
|
177
|
-
});
|
|
178
|
-
});
|
|
179
|
-
describe('GitLab MCP Server - Streamable HTTP Transport', () => {
|
|
180
|
-
let server;
|
|
181
|
-
let client;
|
|
182
|
-
let port;
|
|
183
|
-
before(async () => {
|
|
184
|
-
port = await findAvailablePort();
|
|
185
|
-
server = await launchServer({
|
|
186
|
-
mode: TransportMode.STREAMABLE_HTTP,
|
|
187
|
-
port,
|
|
188
|
-
timeout: 3000,
|
|
189
|
-
env: {
|
|
190
|
-
SSE: 'false',
|
|
191
|
-
STREAMABLE_HTTP: 'true'
|
|
192
|
-
}
|
|
193
|
-
});
|
|
194
|
-
servers.push(server);
|
|
195
|
-
// Verify server started successfully
|
|
196
|
-
assert.ok(server.process.pid !== undefined, 'Server process should have PID');
|
|
197
|
-
assert.strictEqual(server.mode, TransportMode.STREAMABLE_HTTP, 'Server mode should be streamable-http');
|
|
198
|
-
assert.strictEqual(server.port, port, 'Server should use correct port');
|
|
199
|
-
// Verify health check
|
|
200
|
-
const health = await checkHealthEndpoint(server.port);
|
|
201
|
-
assert.strictEqual(health.status, 'healthy', 'Health status should be healthy');
|
|
202
|
-
assert.strictEqual(health.transport, 'streamable-http', 'Transport should be streamable-http');
|
|
203
|
-
assert.ok(health.version !== null && health.version !== undefined, 'Version should be defined');
|
|
204
|
-
assert.ok(health.activeSessions !== null && health.activeSessions !== undefined, 'Active sessions should be defined');
|
|
205
|
-
// Create and connect client
|
|
206
|
-
client = new StreamableHTTPTestClient();
|
|
207
|
-
await client.connect(`http://${HOST}:${port}/mcp`);
|
|
208
|
-
assert.ok(client.isConnected, 'Client should be connected');
|
|
209
|
-
assert.ok(await client.testConnection(), 'Connection test should pass');
|
|
210
|
-
console.log('Client connected to Streamable HTTP server');
|
|
211
|
-
});
|
|
212
|
-
after(async () => {
|
|
213
|
-
if (client && client.isConnected) {
|
|
214
|
-
await client.disconnect();
|
|
215
|
-
}
|
|
216
|
-
cleanup();
|
|
217
|
-
console.log('Client disconnected from Streamable HTTP server');
|
|
218
|
-
});
|
|
219
|
-
test('should return 406 for GET /mcp without Accept: text/event-stream', async () => {
|
|
220
|
-
const response = await fetch(`http://${HOST}:${port}/mcp`);
|
|
221
|
-
assert.strictEqual(response.status, 406, 'GET /mcp should respond with 406 when Accept header is missing text/event-stream');
|
|
222
|
-
const body = await response.json();
|
|
223
|
-
assert.strictEqual(body?.error, 'Not Acceptable');
|
|
224
|
-
});
|
|
225
|
-
test('should return 405 for unsupported HTTP methods like PUT /mcp', async () => {
|
|
226
|
-
const response = await fetch(`http://${HOST}:${port}/mcp`, { method: 'PUT' });
|
|
227
|
-
assert.strictEqual(response.status, 405, 'PUT /mcp should respond with 405');
|
|
228
|
-
const allowHeader = response.headers.get('allow');
|
|
229
|
-
assert.ok(allowHeader?.includes('GET') && allowHeader?.includes('POST') && allowHeader?.includes('DELETE'), 'Allow header should contain GET, POST, DELETE');
|
|
230
|
-
});
|
|
231
|
-
test('should list tools via Streamable HTTP', async () => {
|
|
232
|
-
const tools = await client.listTools();
|
|
233
|
-
assert.ok(tools !== null && tools !== undefined, 'Tools response should be defined');
|
|
234
|
-
assert.ok('tools' in tools, 'Response should have tools property');
|
|
235
|
-
assert.ok(Array.isArray(tools.tools) && tools.tools.length > 0, 'Tools array should not be empty');
|
|
236
|
-
// Check for specific GitLab tools
|
|
237
|
-
const toolNames = tools.tools.map((tool) => tool.name);
|
|
238
|
-
assert.ok(toolNames.includes('list_merge_requests'), 'Should have list_merge_requests tool');
|
|
239
|
-
assert.ok(toolNames.includes('get_project'), 'Should have get_project tool');
|
|
240
|
-
});
|
|
241
|
-
test('should call list_merge_requests tool via Streamable HTTP', async () => {
|
|
242
|
-
const result = await client.callTool('list_merge_requests', {
|
|
243
|
-
project_id: TEST_PROJECT_ID
|
|
244
|
-
});
|
|
245
|
-
assert.ok(result !== null && result !== undefined, 'Tool call result should be defined');
|
|
246
|
-
assert.ok('content' in result, 'Result should have content property');
|
|
247
|
-
});
|
|
248
|
-
test('should call get_project tool via Streamable HTTP', async () => {
|
|
249
|
-
const result = await client.callTool('get_project', {
|
|
250
|
-
project_id: TEST_PROJECT_ID
|
|
251
|
-
});
|
|
252
|
-
assert.ok(result !== null && result !== undefined, 'Tool call result should be defined');
|
|
253
|
-
assert.ok('content' in result, 'Result should have content property');
|
|
254
|
-
});
|
|
255
|
-
});
|
|
@@ -1,195 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Auth Retry Tests
|
|
3
|
-
* Unit tests for headersToPlainObject, isNonReplayableBody, and wrapWithAuthRetry.
|
|
4
|
-
*
|
|
5
|
-
* These are pure-function / DI-based tests — no env vars or external services needed.
|
|
6
|
-
*/
|
|
7
|
-
import { describe, test } from "node:test";
|
|
8
|
-
import assert from "node:assert";
|
|
9
|
-
import { Readable } from "node:stream";
|
|
10
|
-
import { Response } from "undici";
|
|
11
|
-
import { headersToPlainObject, isNonReplayableBody, wrapWithAuthRetry, } from "../auth-retry.js";
|
|
12
|
-
function mockFetch(status) {
|
|
13
|
-
return async () => new Response("", { status });
|
|
14
|
-
}
|
|
15
|
-
function mockFetchThenRetry() {
|
|
16
|
-
let callCount = 0;
|
|
17
|
-
return async () => {
|
|
18
|
-
callCount++;
|
|
19
|
-
return new Response("", { status: callCount === 1 ? 401 : 200 });
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
function makeConfig(overrides) {
|
|
23
|
-
return {
|
|
24
|
-
isOAuthEnabled: () => true,
|
|
25
|
-
refreshToken: async () => "new-token",
|
|
26
|
-
onTokenRefreshed: () => { },
|
|
27
|
-
buildAuthHeaders: () => ({ Authorization: "Bearer new-token" }),
|
|
28
|
-
...overrides,
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
describe("headersToPlainObject", () => {
|
|
32
|
-
test("null returns empty object", () => {
|
|
33
|
-
assert.deepStrictEqual(headersToPlainObject(null), {});
|
|
34
|
-
});
|
|
35
|
-
test("undefined returns empty object", () => {
|
|
36
|
-
assert.deepStrictEqual(headersToPlainObject(undefined), {});
|
|
37
|
-
});
|
|
38
|
-
test("plain object passed through", () => {
|
|
39
|
-
const obj = { "Content-Type": "application/json", Accept: "text/html" };
|
|
40
|
-
assert.deepStrictEqual(headersToPlainObject(obj), obj);
|
|
41
|
-
});
|
|
42
|
-
test("Headers instance normalized", () => {
|
|
43
|
-
const h = new Headers();
|
|
44
|
-
h.set("x-custom", "value1");
|
|
45
|
-
h.set("authorization", "Bearer tok");
|
|
46
|
-
const result = headersToPlainObject(h);
|
|
47
|
-
assert.strictEqual(result["x-custom"], "value1");
|
|
48
|
-
assert.strictEqual(result["authorization"], "Bearer tok");
|
|
49
|
-
});
|
|
50
|
-
test("array of tuples normalized", () => {
|
|
51
|
-
const arr = [
|
|
52
|
-
["x-foo", "bar"],
|
|
53
|
-
["x-baz", "qux"],
|
|
54
|
-
];
|
|
55
|
-
assert.deepStrictEqual(headersToPlainObject(arr), {
|
|
56
|
-
"x-foo": "bar",
|
|
57
|
-
"x-baz": "qux",
|
|
58
|
-
});
|
|
59
|
-
});
|
|
60
|
-
});
|
|
61
|
-
describe("isNonReplayableBody", () => {
|
|
62
|
-
test("null returns false", () => {
|
|
63
|
-
assert.strictEqual(isNonReplayableBody(null), false);
|
|
64
|
-
});
|
|
65
|
-
test("undefined returns false", () => {
|
|
66
|
-
assert.strictEqual(isNonReplayableBody(undefined), false);
|
|
67
|
-
});
|
|
68
|
-
test("empty string returns false", () => {
|
|
69
|
-
assert.strictEqual(isNonReplayableBody(""), false);
|
|
70
|
-
});
|
|
71
|
-
test("plain string returns false", () => {
|
|
72
|
-
assert.strictEqual(isNonReplayableBody("hello"), false);
|
|
73
|
-
});
|
|
74
|
-
test("object with .pipe() returns true (stream-like)", () => {
|
|
75
|
-
assert.strictEqual(isNonReplayableBody({ pipe: () => { } }), true);
|
|
76
|
-
});
|
|
77
|
-
test("object with .read() returns true (stream-like)", () => {
|
|
78
|
-
assert.strictEqual(isNonReplayableBody({ read: () => { } }), true);
|
|
79
|
-
});
|
|
80
|
-
test("Web ReadableStream returns true", () => {
|
|
81
|
-
const stream = new ReadableStream({
|
|
82
|
-
start(controller) {
|
|
83
|
-
controller.close();
|
|
84
|
-
},
|
|
85
|
-
});
|
|
86
|
-
assert.strictEqual(isNonReplayableBody(stream), true);
|
|
87
|
-
});
|
|
88
|
-
});
|
|
89
|
-
describe("wrapWithAuthRetry", () => {
|
|
90
|
-
test("non-401 response passes through unchanged", async () => {
|
|
91
|
-
const wrapped = wrapWithAuthRetry(mockFetch(200), makeConfig());
|
|
92
|
-
const res = await wrapped("http://example.com");
|
|
93
|
-
assert.strictEqual(res.status, 200);
|
|
94
|
-
});
|
|
95
|
-
test("401 when OAuth disabled passes through unchanged", async () => {
|
|
96
|
-
const config = makeConfig({ isOAuthEnabled: () => false });
|
|
97
|
-
const wrapped = wrapWithAuthRetry(mockFetch(401), config);
|
|
98
|
-
const res = await wrapped("http://example.com");
|
|
99
|
-
assert.strictEqual(res.status, 401);
|
|
100
|
-
});
|
|
101
|
-
test("401 with OAuth enabled triggers refresh and retry", async () => {
|
|
102
|
-
let refreshCalled = false;
|
|
103
|
-
let tokenSet = null;
|
|
104
|
-
const config = makeConfig({
|
|
105
|
-
refreshToken: async () => {
|
|
106
|
-
refreshCalled = true;
|
|
107
|
-
return "refreshed-token";
|
|
108
|
-
},
|
|
109
|
-
onTokenRefreshed: (token) => {
|
|
110
|
-
tokenSet = token;
|
|
111
|
-
},
|
|
112
|
-
buildAuthHeaders: () => ({ Authorization: "Bearer refreshed-token" }),
|
|
113
|
-
});
|
|
114
|
-
const base = mockFetchThenRetry();
|
|
115
|
-
const wrapped = wrapWithAuthRetry(base, config);
|
|
116
|
-
const res = await wrapped("http://example.com");
|
|
117
|
-
assert.strictEqual(res.status, 200);
|
|
118
|
-
assert.strictEqual(refreshCalled, true);
|
|
119
|
-
assert.strictEqual(tokenSet, "refreshed-token");
|
|
120
|
-
});
|
|
121
|
-
test("401 with non-replayable body skips retry", async () => {
|
|
122
|
-
let refreshCalled = false;
|
|
123
|
-
const config = makeConfig({
|
|
124
|
-
refreshToken: async () => {
|
|
125
|
-
refreshCalled = true;
|
|
126
|
-
return "tok";
|
|
127
|
-
},
|
|
128
|
-
});
|
|
129
|
-
const wrapped = wrapWithAuthRetry(mockFetch(401), config);
|
|
130
|
-
const res = await wrapped("http://example.com", {
|
|
131
|
-
body: Readable.from([]),
|
|
132
|
-
duplex: "half",
|
|
133
|
-
});
|
|
134
|
-
assert.strictEqual(res.status, 401);
|
|
135
|
-
assert.strictEqual(refreshCalled, false);
|
|
136
|
-
});
|
|
137
|
-
test("401 with Web ReadableStream body skips retry", async () => {
|
|
138
|
-
let refreshCalled = false;
|
|
139
|
-
const config = makeConfig({
|
|
140
|
-
refreshToken: async () => {
|
|
141
|
-
refreshCalled = true;
|
|
142
|
-
return "tok";
|
|
143
|
-
},
|
|
144
|
-
});
|
|
145
|
-
const stream = new ReadableStream({
|
|
146
|
-
start(controller) {
|
|
147
|
-
controller.close();
|
|
148
|
-
},
|
|
149
|
-
});
|
|
150
|
-
const wrapped = wrapWithAuthRetry(mockFetch(401), config);
|
|
151
|
-
const res = await wrapped("http://example.com", {
|
|
152
|
-
body: stream,
|
|
153
|
-
duplex: "half",
|
|
154
|
-
});
|
|
155
|
-
assert.strictEqual(res.status, 401);
|
|
156
|
-
assert.strictEqual(refreshCalled, false);
|
|
157
|
-
});
|
|
158
|
-
test("concurrent 401s only trigger one refresh (stampede test)", async () => {
|
|
159
|
-
let refreshCount = 0;
|
|
160
|
-
let resolveRefresh = () => { };
|
|
161
|
-
const config = makeConfig({
|
|
162
|
-
refreshToken: () => {
|
|
163
|
-
refreshCount++;
|
|
164
|
-
return new Promise(resolve => {
|
|
165
|
-
resolveRefresh = resolve;
|
|
166
|
-
});
|
|
167
|
-
},
|
|
168
|
-
buildAuthHeaders: () => ({ Authorization: "Bearer stamped" }),
|
|
169
|
-
});
|
|
170
|
-
let callCount = 0;
|
|
171
|
-
const base = async () => {
|
|
172
|
-
callCount++;
|
|
173
|
-
return new Response("", { status: callCount <= 2 ? 401 : 200 });
|
|
174
|
-
};
|
|
175
|
-
const wrapped = wrapWithAuthRetry(base, config);
|
|
176
|
-
const p1 = wrapped("http://example.com/a");
|
|
177
|
-
const p2 = wrapped("http://example.com/b");
|
|
178
|
-
await new Promise(r => setTimeout(r, 10));
|
|
179
|
-
resolveRefresh("stamped-token");
|
|
180
|
-
const [r1, r2] = await Promise.all([p1, p2]);
|
|
181
|
-
assert.strictEqual(r1.status, 200);
|
|
182
|
-
assert.strictEqual(r2.status, 200);
|
|
183
|
-
assert.strictEqual(refreshCount, 1, "refresh should be called exactly once");
|
|
184
|
-
});
|
|
185
|
-
test("token refresh failure returns original 401 response", async () => {
|
|
186
|
-
const config = makeConfig({
|
|
187
|
-
refreshToken: async () => {
|
|
188
|
-
throw new Error("refresh exploded");
|
|
189
|
-
},
|
|
190
|
-
});
|
|
191
|
-
const wrapped = wrapWithAuthRetry(mockFetch(401), config);
|
|
192
|
-
const res = await wrapped("http://example.com");
|
|
193
|
-
assert.strictEqual(res.status, 401);
|
|
194
|
-
});
|
|
195
|
-
});
|