@mastra/server-adapters-test-suite 0.2.2-alpha.0 → 0.3.0-alpha.2
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/CHANGELOG.md +36 -0
- package/dist/custom-route-fga-test-suite.d.ts.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +343 -2
- package/dist/index.js.map +1 -1
- package/dist/route-adapter-test-suite.d.ts.map +1 -1
- package/dist/stream-disconnect-test-suite.d.ts +24 -0
- package/dist/stream-disconnect-test-suite.d.ts.map +1 -0
- package/package.json +5 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,41 @@
|
|
|
1
1
|
# @mastra/server-adapters-test-suite
|
|
2
2
|
|
|
3
|
+
## 0.3.0-alpha.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Added conformance tests for custom route auth: auth and FGA checks must follow the route that handles the request, including its path params, for overlapping routes, `HEAD` requests, letter case and percent-encoded paths. ([#29325](https://github.com/mastra-ai/mastra/pull/29325))
|
|
8
|
+
|
|
9
|
+
- Updated dependencies [[`ce86e53`](https://github.com/mastra-ai/mastra/commit/ce86e5327a0e835e26792e238dacc2c15a777bbf), [`ce76df9`](https://github.com/mastra-ai/mastra/commit/ce76df95d5d942940de7d5453fcbd38b7df5ea83), [`bfda77d`](https://github.com/mastra-ai/mastra/commit/bfda77de9773c383f23101cde8f4ee12af8bff19), [`66c620c`](https://github.com/mastra-ai/mastra/commit/66c620c086cd4a5d12347a832057290a19cb9db2), [`964e1ba`](https://github.com/mastra-ai/mastra/commit/964e1baa24a5a383c86b9f3aa639d488e8742b13), [`311d434`](https://github.com/mastra-ai/mastra/commit/311d4342cc61e9f5a621625e28e483a892fda6bc), [`9730315`](https://github.com/mastra-ai/mastra/commit/9730315a2fddf51580599030916d43b17e1ca553), [`bf9212b`](https://github.com/mastra-ai/mastra/commit/bf9212b308c1d418cac64d54aa380dac324db1bd), [`92f9771`](https://github.com/mastra-ai/mastra/commit/92f9771a0fefaf6e23541ac702850da63bfe4ef9)]:
|
|
10
|
+
- @mastra/core@1.83.0-alpha.5
|
|
11
|
+
- @mastra/server@1.83.0-alpha.5
|
|
12
|
+
- @mastra/mcp@2.9.0-alpha.2
|
|
13
|
+
|
|
14
|
+
## 0.3.0-alpha.1
|
|
15
|
+
|
|
16
|
+
### Minor Changes
|
|
17
|
+
|
|
18
|
+
- Added `createStreamDisconnectTestSuite`, which checks over real HTTP that an adapter stops reading a route's stream when the client disconnects and pauses reading while the client is not reading. ([#29669](https://github.com/mastra-ai/mastra/pull/29669))
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { createStreamDisconnectTestSuite } from '@mastra/server-adapters-test-suite';
|
|
22
|
+
|
|
23
|
+
createStreamDisconnectTestSuite({
|
|
24
|
+
// Serve `route` with your adapter and return its URL. Run `beforeContext` before the adapter's context middleware.
|
|
25
|
+
startServer: async (route, { beforeContext } = {}) => {
|
|
26
|
+
const { url, close } = await startMyAdapterServer(route, beforeContext);
|
|
27
|
+
return { url, close };
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Patch Changes
|
|
33
|
+
|
|
34
|
+
- Updated dependencies [[`58a66b0`](https://github.com/mastra-ai/mastra/commit/58a66b01d9b08e7b59c27fc3ab68eb90be13c0ed), [`ac2a640`](https://github.com/mastra-ai/mastra/commit/ac2a6401a09e2177555369cff2f7ad15f2cc7286), [`94b1641`](https://github.com/mastra-ai/mastra/commit/94b1641919dd90455131b16cd0572ce28008781b), [`8279c30`](https://github.com/mastra-ai/mastra/commit/8279c3073737dd1c228b5df15416ca4ce7e0c00c), [`bc9d8d4`](https://github.com/mastra-ai/mastra/commit/bc9d8d482cec626ef2107f1cbd5a05febceaa491), [`8a10dfe`](https://github.com/mastra-ai/mastra/commit/8a10dfe648610a6687d1e4ecc10a0a81e3d80c4c), [`d77893a`](https://github.com/mastra-ai/mastra/commit/d77893aed38eefe2dbac21e9fffd872137ee6b92), [`791cf60`](https://github.com/mastra-ai/mastra/commit/791cf606e91badea097b883949dc717689caa49d), [`b31cbe2`](https://github.com/mastra-ai/mastra/commit/b31cbe2888f890383629d20c853e21f870dee5a6), [`bfedbfe`](https://github.com/mastra-ai/mastra/commit/bfedbfe5a8e5721cba782efb4652be84e9fecc04), [`c9140d2`](https://github.com/mastra-ai/mastra/commit/c9140d2d4d16dfd674f163cd0a83e28593969c04), [`a836d09`](https://github.com/mastra-ai/mastra/commit/a836d09a43c3c471b5bfced711731355bf26db26), [`4854e36`](https://github.com/mastra-ai/mastra/commit/4854e3641931c984a5b3941371a4f0fa58eafd0e), [`7358ad7`](https://github.com/mastra-ai/mastra/commit/7358ad73a381765572858d520f8170ab59e88514), [`efa3999`](https://github.com/mastra-ai/mastra/commit/efa3999a21fea5bfaa455325f9a548b3daf358e9), [`b93d804`](https://github.com/mastra-ai/mastra/commit/b93d804a7a406d834389e686687b06d22ea2e4f1), [`ed1cecb`](https://github.com/mastra-ai/mastra/commit/ed1cecba5c26a73600640f74e9a1c9d322271b23), [`4cd4c46`](https://github.com/mastra-ai/mastra/commit/4cd4c46e546d44c556fce4d731cac03cad4f88c4), [`9e5b108`](https://github.com/mastra-ai/mastra/commit/9e5b1082cbe2454c377b3328cd8a0085e7452d1a), [`b31cbe2`](https://github.com/mastra-ai/mastra/commit/b31cbe2888f890383629d20c853e21f870dee5a6), [`28ddc16`](https://github.com/mastra-ai/mastra/commit/28ddc161a441fdc3ecbdcc1168dee773f1f25243), [`0f496bd`](https://github.com/mastra-ai/mastra/commit/0f496bd3c1cc7f07d2e4ecb3005d480406b3d342), [`de005a4`](https://github.com/mastra-ai/mastra/commit/de005a43d8703d36c5dd25d7ddd1f618ea98bf55)]:
|
|
35
|
+
- @mastra/core@1.83.0-alpha.3
|
|
36
|
+
- @mastra/server@1.83.0-alpha.3
|
|
37
|
+
- @mastra/mcp@2.9.0-alpha.1
|
|
38
|
+
|
|
3
39
|
## 0.2.2-alpha.0
|
|
4
40
|
|
|
5
41
|
### Patch Changes
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"custom-route-fga-test-suite.d.ts","sourceRoot":"","sources":["../src/custom-route-fga-test-suite.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAKtC;;GAEG;AACH,MAAM,WAAW,6BAA6B;IAC5C,8BAA8B;IAC9B,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;;OAGG;IACH,WAAW,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC;QACvC,OAAO,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;QACjE,KAAK,CAAC,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;KAC7B,CAAC,CAAC;CACJ;AAED;;;;GAIG;AACH,wBAAgB,6BAA6B,CAAC,MAAM,EAAE,6BAA6B,
|
|
1
|
+
{"version":3,"file":"custom-route-fga-test-suite.d.ts","sourceRoot":"","sources":["../src/custom-route-fga-test-suite.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAKtC;;GAEG;AACH,MAAM,WAAW,6BAA6B;IAC5C,8BAA8B;IAC9B,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;;OAGG;IACH,WAAW,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC;QACvC,OAAO,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;QACjE,KAAK,CAAC,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;KAC7B,CAAC,CAAC;CACJ;AAED;;;;GAIG;AACH,wBAAgB,6BAA6B,CAAC,MAAM,EAAE,6BAA6B,QAoOlF"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export { createRouteAdapterTestSuite } from './route-adapter-test-suite.js';
|
|
2
2
|
export { createStructuredWakeTestSuite, type StructuredWakeTestConfig } from './structured-wake-test-suite.js';
|
|
3
3
|
export { createDurableDisconnectTestSuite, type DurableDisconnectTestConfig } from './durable-disconnect-test-suite.js';
|
|
4
|
+
export { createStreamDisconnectTestSuite, type StreamDisconnectTestConfig } from './stream-disconnect-test-suite.js';
|
|
4
5
|
export type { AdapterTestSuiteConfig, AdapterTestContext, AdapterSetupOptions, HttpRequest, HttpResponse, } from './test-helpers.js';
|
|
5
6
|
export { createMCPRouteTestSuite } from './mcp-route-test-suite.js';
|
|
6
7
|
export { createMCPTransportTestSuite, type MCPTransportTestConfig } from './mcp-transport-test-suite.js';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,MAAM,4BAA4B,CAAC;AACzE,OAAO,EAAE,6BAA6B,EAAE,KAAK,wBAAwB,EAAE,MAAM,8BAA8B,CAAC;AAC5G,OAAO,EAAE,gCAAgC,EAAE,KAAK,2BAA2B,EAAE,MAAM,iCAAiC,CAAC;AACrH,YAAY,EACV,sBAAsB,EACtB,kBAAkB,EAClB,mBAAmB,EACnB,WAAW,EACX,YAAY,GACb,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,uBAAuB,EAAE,MAAM,wBAAwB,CAAC;AACjE,OAAO,EAAE,2BAA2B,EAAE,KAAK,sBAAsB,EAAE,MAAM,4BAA4B,CAAC;AACtG,OAAO,EAAE,wBAAwB,EAAE,KAAK,wBAAwB,EAAE,MAAM,wBAAwB,CAAC;AACjG,OAAO,EAAE,0BAA0B,EAAE,KAAK,0BAA0B,EAAE,MAAM,2BAA2B,CAAC;AACxG,OAAO,EAAE,wBAAwB,EAAE,KAAK,wBAAwB,EAAE,MAAM,yBAAyB,CAAC;AAClG,OAAO,EAAE,6BAA6B,EAAE,KAAK,6BAA6B,EAAE,MAAM,+BAA+B,CAAC;AAElH,OAAO,EACL,wBAAwB,EACxB,6BAA6B,EAC7B,mCAAmC,EACnC,4BAA4B,EAC5B,gBAAgB,GACjB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,8BAA8B,EAAE,KAAK,8BAA8B,EAAE,MAAM,+BAA+B,CAAC;AAEpH,OAAO,EACL,uCAAuC,EACvC,KAAK,kCAAkC,GACxC,MAAM,yCAAyC,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,MAAM,4BAA4B,CAAC;AACzE,OAAO,EAAE,6BAA6B,EAAE,KAAK,wBAAwB,EAAE,MAAM,8BAA8B,CAAC;AAC5G,OAAO,EAAE,gCAAgC,EAAE,KAAK,2BAA2B,EAAE,MAAM,iCAAiC,CAAC;AACrH,OAAO,EAAE,+BAA+B,EAAE,KAAK,0BAA0B,EAAE,MAAM,gCAAgC,CAAC;AAClH,YAAY,EACV,sBAAsB,EACtB,kBAAkB,EAClB,mBAAmB,EACnB,WAAW,EACX,YAAY,GACb,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,uBAAuB,EAAE,MAAM,wBAAwB,CAAC;AACjE,OAAO,EAAE,2BAA2B,EAAE,KAAK,sBAAsB,EAAE,MAAM,4BAA4B,CAAC;AACtG,OAAO,EAAE,wBAAwB,EAAE,KAAK,wBAAwB,EAAE,MAAM,wBAAwB,CAAC;AACjG,OAAO,EAAE,0BAA0B,EAAE,KAAK,0BAA0B,EAAE,MAAM,2BAA2B,CAAC;AACxG,OAAO,EAAE,wBAAwB,EAAE,KAAK,wBAAwB,EAAE,MAAM,yBAAyB,CAAC;AAClG,OAAO,EAAE,6BAA6B,EAAE,KAAK,6BAA6B,EAAE,MAAM,+BAA+B,CAAC;AAElH,OAAO,EACL,wBAAwB,EACxB,6BAA6B,EAC7B,mCAAmC,EACnC,4BAA4B,EAC5B,gBAAgB,GACjB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,8BAA8B,EAAE,KAAK,8BAA8B,EAAE,MAAM,+BAA+B,CAAC;AAEpH,OAAO,EACL,uCAAuC,EACvC,KAAK,kCAAkC,GACxC,MAAM,yCAAyC,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -7,7 +7,7 @@ import * as zod from "zod";
|
|
|
7
7
|
import { z } from "zod";
|
|
8
8
|
import { z as z$1 } from "zod/v3";
|
|
9
9
|
import { getZodDef, getZodTypeName, makeCoreTool } from "@mastra/core/utils";
|
|
10
|
-
import { randomUUID } from "crypto";
|
|
10
|
+
import { randomBytes, randomUUID } from "crypto";
|
|
11
11
|
import * as fs from "fs";
|
|
12
12
|
import * as os from "os";
|
|
13
13
|
import * as path from "path";
|
|
@@ -1783,14 +1783,20 @@ function createRouteAdapterTestSuite(config) {
|
|
|
1783
1783
|
let context;
|
|
1784
1784
|
let app;
|
|
1785
1785
|
let setup;
|
|
1786
|
+
let netlifyProviders;
|
|
1786
1787
|
beforeEach(async () => {
|
|
1787
1788
|
if (createTestContext) {
|
|
1788
1789
|
const result = createTestContext();
|
|
1789
1790
|
context = result instanceof Promise ? await result : result;
|
|
1790
1791
|
} else context = await createDefaultTestContext();
|
|
1792
|
+
const netlify = context.mastra.listGateways()?.netlify;
|
|
1793
|
+
netlifyProviders = netlify ? vi.spyOn(netlify, "fetchProviders").mockResolvedValue({}) : void 0;
|
|
1791
1794
|
setup = await setupAdapter(context);
|
|
1792
1795
|
app = setup.app;
|
|
1793
1796
|
});
|
|
1797
|
+
afterEach(() => {
|
|
1798
|
+
netlifyProviders?.mockRestore();
|
|
1799
|
+
});
|
|
1794
1800
|
if (config.supportsPostQueryRequestContext) it("merges query requestContext into POST route handlers", async () => {
|
|
1795
1801
|
await setup.adapter.registerRoute(app, {
|
|
1796
1802
|
method: "POST",
|
|
@@ -4331,6 +4337,199 @@ function createDurableDisconnectTestSuite(config) {
|
|
|
4331
4337
|
}
|
|
4332
4338
|
});
|
|
4333
4339
|
}
|
|
4340
|
+
// src/stream-disconnect-test-suite.ts
|
|
4341
|
+
const incompressible = (bytes) => randomBytes(Math.ceil(bytes * 3 / 4)).toString("base64").slice(0, bytes);
|
|
4342
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
4343
|
+
/**
|
|
4344
|
+
* Covers what happens to the stream a route returns when the client goes away (#29634, #29640). The adapter has
|
|
4345
|
+
* to cancel the upstream stream once the client is gone; for agents with resumable streams that cancel is the only
|
|
4346
|
+
* signal that the HTTP observer left, and without it the upstream keeps being read into a dead socket.
|
|
4347
|
+
*/
|
|
4348
|
+
function createStreamDisconnectTestSuite(config) {
|
|
4349
|
+
describe("streaming client disconnect", () => {
|
|
4350
|
+
let close;
|
|
4351
|
+
afterEach(async () => {
|
|
4352
|
+
await close?.();
|
|
4353
|
+
close = void 0;
|
|
4354
|
+
});
|
|
4355
|
+
async function setup({ responseType, chunks = 60, chunkBytes = 16, paceMs = 10, holdUntilDisconnect = false, compress = false }) {
|
|
4356
|
+
const state = {
|
|
4357
|
+
pulls: 0,
|
|
4358
|
+
canceled: false
|
|
4359
|
+
};
|
|
4360
|
+
let handlerEntered;
|
|
4361
|
+
const handlerStarted = new Promise((resolve) => handlerEntered = resolve);
|
|
4362
|
+
let handlerReturned;
|
|
4363
|
+
const handlerDone = new Promise((resolve) => handlerReturned = resolve);
|
|
4364
|
+
const padding = compress ? incompressible(chunkBytes) : "x".repeat(chunkBytes);
|
|
4365
|
+
const route = {
|
|
4366
|
+
method: "POST",
|
|
4367
|
+
path: "/test/disconnect",
|
|
4368
|
+
responseType,
|
|
4369
|
+
handler: async ({ abortSignal }) => {
|
|
4370
|
+
handlerEntered();
|
|
4371
|
+
if (holdUntilDisconnect) await Promise.race([new Promise((resolve) => {
|
|
4372
|
+
if (abortSignal?.aborted) resolve();
|
|
4373
|
+
abortSignal?.addEventListener("abort", () => resolve(), { once: true });
|
|
4374
|
+
}), sleep(2e3)]);
|
|
4375
|
+
handlerReturned();
|
|
4376
|
+
const body = new ReadableStream({
|
|
4377
|
+
async pull(controller) {
|
|
4378
|
+
if (state.pulls >= chunks) return controller.close();
|
|
4379
|
+
state.pulls++;
|
|
4380
|
+
if (paceMs) await sleep(paceMs);
|
|
4381
|
+
controller.enqueue(responseType === "stream" ? {
|
|
4382
|
+
type: "chunk",
|
|
4383
|
+
n: state.pulls,
|
|
4384
|
+
padding
|
|
4385
|
+
} : new TextEncoder().encode(`chunk-${state.pulls} ${padding}\n`));
|
|
4386
|
+
},
|
|
4387
|
+
cancel() {
|
|
4388
|
+
state.canceled = true;
|
|
4389
|
+
}
|
|
4390
|
+
});
|
|
4391
|
+
return responseType === "stream" ? body : new Response(body, {
|
|
4392
|
+
status: 200,
|
|
4393
|
+
headers: { "Content-Type": "text/plain" }
|
|
4394
|
+
});
|
|
4395
|
+
}
|
|
4396
|
+
};
|
|
4397
|
+
const server = await config.startServer(route, { compress });
|
|
4398
|
+
close = server.close;
|
|
4399
|
+
return {
|
|
4400
|
+
url: server.url,
|
|
4401
|
+
state,
|
|
4402
|
+
handlerStarted,
|
|
4403
|
+
handlerDone
|
|
4404
|
+
};
|
|
4405
|
+
}
|
|
4406
|
+
const post = (url, signal) => fetch(url, {
|
|
4407
|
+
method: "POST",
|
|
4408
|
+
headers: { "content-type": "application/json" },
|
|
4409
|
+
body: "{}",
|
|
4410
|
+
signal
|
|
4411
|
+
});
|
|
4412
|
+
it("aborts the route abort signal when the client left while earlier middleware was running", async () => {
|
|
4413
|
+
let hookEntered;
|
|
4414
|
+
const inHook = new Promise((resolve) => hookEntered = resolve);
|
|
4415
|
+
let releaseHook;
|
|
4416
|
+
const hookReleased = new Promise((resolve) => releaseHook = resolve);
|
|
4417
|
+
let routeRan;
|
|
4418
|
+
const routeResult = new Promise((resolve) => routeRan = resolve);
|
|
4419
|
+
const server = await config.startServer({
|
|
4420
|
+
method: "GET",
|
|
4421
|
+
path: "/test/early-close",
|
|
4422
|
+
responseType: "json",
|
|
4423
|
+
handler: async ({ abortSignal }) => {
|
|
4424
|
+
routeRan(abortSignal?.aborted);
|
|
4425
|
+
return {};
|
|
4426
|
+
}
|
|
4427
|
+
}, { beforeContext: async () => {
|
|
4428
|
+
hookEntered();
|
|
4429
|
+
await hookReleased;
|
|
4430
|
+
} });
|
|
4431
|
+
close = server.close;
|
|
4432
|
+
const client = new AbortController();
|
|
4433
|
+
const request = fetch(server.url, { signal: client.signal }).catch(() => void 0);
|
|
4434
|
+
await inHook;
|
|
4435
|
+
client.abort();
|
|
4436
|
+
await request;
|
|
4437
|
+
await sleep(100);
|
|
4438
|
+
releaseHook();
|
|
4439
|
+
const aborted = await Promise.race([routeResult, sleep(2e3).then(() => "route never ran")]);
|
|
4440
|
+
expect(aborted, "route abortSignal.aborted").toBe(true);
|
|
4441
|
+
});
|
|
4442
|
+
describe.each(["stream", "datastream-response"])("%s route", (responseType) => {
|
|
4443
|
+
it("stops reading the upstream when the client aborts mid-body", async () => {
|
|
4444
|
+
const { url, state } = await setup({ responseType });
|
|
4445
|
+
const client = new AbortController();
|
|
4446
|
+
await (await post(url, client.signal)).body.getReader().read();
|
|
4447
|
+
const pullsAtAbort = state.pulls;
|
|
4448
|
+
client.abort();
|
|
4449
|
+
await sleep(300);
|
|
4450
|
+
expect(state.pulls - pullsAtAbort, "upstream pulls after client abort").toBeLessThanOrEqual(2);
|
|
4451
|
+
expect(state.canceled, "upstream cancel()").toBe(true);
|
|
4452
|
+
});
|
|
4453
|
+
it("stops reading the upstream when the client aborts before the handler returns", async () => {
|
|
4454
|
+
const { url, state, handlerStarted, handlerDone } = await setup({
|
|
4455
|
+
responseType,
|
|
4456
|
+
holdUntilDisconnect: true
|
|
4457
|
+
});
|
|
4458
|
+
const client = new AbortController();
|
|
4459
|
+
const request = post(url, client.signal).catch(() => void 0);
|
|
4460
|
+
await handlerStarted;
|
|
4461
|
+
client.abort();
|
|
4462
|
+
await request;
|
|
4463
|
+
await handlerDone;
|
|
4464
|
+
await sleep(300);
|
|
4465
|
+
expect(state.pulls, "upstream pulls after client abort").toBeLessThanOrEqual(2);
|
|
4466
|
+
expect(state.canceled, "upstream cancel()").toBe(true);
|
|
4467
|
+
});
|
|
4468
|
+
it("delivers the whole body without cancelling the upstream when the client reads to the end", async () => {
|
|
4469
|
+
const { url, state } = await setup({
|
|
4470
|
+
responseType,
|
|
4471
|
+
chunks: 20
|
|
4472
|
+
});
|
|
4473
|
+
const text = await (await post(url)).text();
|
|
4474
|
+
expect(text.match(/"n":\d+|chunk-\d+/g)).toHaveLength(20);
|
|
4475
|
+
expect(state.canceled, "upstream cancel()").toBe(false);
|
|
4476
|
+
});
|
|
4477
|
+
it("pauses reading the upstream while a connected client is not reading, and cancels it once the client leaves", async () => {
|
|
4478
|
+
const { url, state } = await setup({
|
|
4479
|
+
responseType,
|
|
4480
|
+
chunks: 400,
|
|
4481
|
+
chunkBytes: 65536,
|
|
4482
|
+
paceMs: 0
|
|
4483
|
+
});
|
|
4484
|
+
const client = new AbortController();
|
|
4485
|
+
const response = await post(url, client.signal);
|
|
4486
|
+
await sleep(500);
|
|
4487
|
+
const pullsWhilePaused = state.pulls;
|
|
4488
|
+
client.abort();
|
|
4489
|
+
await response.body?.cancel().catch(() => {});
|
|
4490
|
+
expect(pullsWhilePaused, "upstream pulls while the client was not reading").toBeLessThan(400);
|
|
4491
|
+
await sleep(300);
|
|
4492
|
+
expect(state.canceled, "upstream cancel()").toBe(true);
|
|
4493
|
+
});
|
|
4494
|
+
it.runIf(config.supportsCompression)("streams a compressed response to a slow client without piling up drain listeners", async () => {
|
|
4495
|
+
const warnings = [];
|
|
4496
|
+
const onWarning = (warning) => {
|
|
4497
|
+
if (warning.name === "MaxListenersExceededWarning") warnings.push(warning.message);
|
|
4498
|
+
};
|
|
4499
|
+
process.on("warning", onWarning);
|
|
4500
|
+
try {
|
|
4501
|
+
const { url } = await setup({
|
|
4502
|
+
responseType,
|
|
4503
|
+
chunks: 100,
|
|
4504
|
+
chunkBytes: 65536,
|
|
4505
|
+
paceMs: 0,
|
|
4506
|
+
compress: true
|
|
4507
|
+
});
|
|
4508
|
+
const response = await fetch(url, {
|
|
4509
|
+
method: "POST",
|
|
4510
|
+
headers: {
|
|
4511
|
+
"content-type": "application/json",
|
|
4512
|
+
"accept-encoding": "gzip"
|
|
4513
|
+
},
|
|
4514
|
+
body: "{}"
|
|
4515
|
+
});
|
|
4516
|
+
expect(response.headers.get("content-encoding")).toBe("gzip");
|
|
4517
|
+
const reader = response.body.pipeThrough(new TextDecoderStream()).getReader();
|
|
4518
|
+
let text = "";
|
|
4519
|
+
for (let next = await reader.read(); !next.done; next = await reader.read()) {
|
|
4520
|
+
text += next.value;
|
|
4521
|
+
await sleep(1);
|
|
4522
|
+
}
|
|
4523
|
+
await sleep(50);
|
|
4524
|
+
expect(text.match(/"n":\d+|chunk-\d+/g)).toHaveLength(100);
|
|
4525
|
+
expect(warnings, "MaxListenersExceededWarning").toEqual([]);
|
|
4526
|
+
} finally {
|
|
4527
|
+
process.off("warning", onWarning);
|
|
4528
|
+
}
|
|
4529
|
+
});
|
|
4530
|
+
});
|
|
4531
|
+
});
|
|
4532
|
+
}
|
|
4334
4533
|
// src/mcp-route-test-suite.ts
|
|
4335
4534
|
/**
|
|
4336
4535
|
* Creates a standardized integration test suite for MCP registry routes
|
|
@@ -6164,6 +6363,148 @@ function createCustomRouteFGATestSuite(config) {
|
|
|
6164
6363
|
expect(response.status).toBe(403);
|
|
6165
6364
|
expect(checkedIds(check)).toEqual(["locked"]);
|
|
6166
6365
|
});
|
|
6366
|
+
it("authenticates overlapping routes the same way they are dispatched", async () => {
|
|
6367
|
+
const mastra = new Mastra({
|
|
6368
|
+
logger: false,
|
|
6369
|
+
server: { apiRoutes: [registerApiRoute("/admin/:id", {
|
|
6370
|
+
method: "GET",
|
|
6371
|
+
handler: async (c) => c.json({ route: "admin" })
|
|
6372
|
+
}), registerApiRoute("/admin/public", {
|
|
6373
|
+
method: "GET",
|
|
6374
|
+
requiresAuth: false,
|
|
6375
|
+
handler: async (c) => c.json({ route: "public" })
|
|
6376
|
+
})] }
|
|
6377
|
+
});
|
|
6378
|
+
const originalGetServer = mastra.getServer.bind(mastra);
|
|
6379
|
+
mastra.getServer = () => ({
|
|
6380
|
+
...originalGetServer(),
|
|
6381
|
+
auth: {
|
|
6382
|
+
authenticateToken: async (token) => token === "valid-token" ? { id: "user-1" } : null,
|
|
6383
|
+
authorize: async () => true
|
|
6384
|
+
}
|
|
6385
|
+
});
|
|
6386
|
+
const server = await startServer(mastra);
|
|
6387
|
+
close = server.close;
|
|
6388
|
+
expect((await server.request("/admin/public")).status).toBe(401);
|
|
6389
|
+
const authed = await server.request("/admin/public", { headers: { authorization: "Bearer valid-token" } });
|
|
6390
|
+
expect(authed.status).toBe(200);
|
|
6391
|
+
expect(await authed.json()).toEqual({ route: "admin" });
|
|
6392
|
+
});
|
|
6393
|
+
const startWithAuth = async (apiRoutes) => {
|
|
6394
|
+
const mastra = new Mastra({
|
|
6395
|
+
logger: false,
|
|
6396
|
+
server: { apiRoutes }
|
|
6397
|
+
});
|
|
6398
|
+
const originalGetServer = mastra.getServer.bind(mastra);
|
|
6399
|
+
mastra.getServer = () => ({
|
|
6400
|
+
...originalGetServer(),
|
|
6401
|
+
auth: {
|
|
6402
|
+
authenticateToken: async (token) => token === "valid-token" ? { id: "user-1" } : null,
|
|
6403
|
+
authorize: async () => true
|
|
6404
|
+
}
|
|
6405
|
+
});
|
|
6406
|
+
const server = await startServer(mastra);
|
|
6407
|
+
close = server.close;
|
|
6408
|
+
return server;
|
|
6409
|
+
};
|
|
6410
|
+
const authed = { headers: { authorization: "Bearer valid-token" } };
|
|
6411
|
+
it("does not run a protected route shadowed by an earlier public one without auth", async () => {
|
|
6412
|
+
const protectedHandler = vi.fn(async (c) => c.json({ route: "protected" }));
|
|
6413
|
+
const response = await (await startWithAuth([registerApiRoute("/admin/:id", {
|
|
6414
|
+
method: "GET",
|
|
6415
|
+
requiresAuth: false,
|
|
6416
|
+
handler: async (c) => c.json({ route: "param" })
|
|
6417
|
+
}), registerApiRoute("/admin/public", {
|
|
6418
|
+
method: "GET",
|
|
6419
|
+
handler: protectedHandler
|
|
6420
|
+
})])).request("/admin/public");
|
|
6421
|
+
expect(response.status).toBe(200);
|
|
6422
|
+
expect(await response.json()).toEqual({ route: "param" });
|
|
6423
|
+
expect(protectedHandler).not.toHaveBeenCalled();
|
|
6424
|
+
});
|
|
6425
|
+
it("requires auth for HEAD requests to a protected GET route", async () => {
|
|
6426
|
+
const protectedHandler = vi.fn(async (c) => c.json({ secret: true }));
|
|
6427
|
+
const server = await startWithAuth([registerApiRoute("/secret", {
|
|
6428
|
+
method: "GET",
|
|
6429
|
+
handler: protectedHandler
|
|
6430
|
+
})]);
|
|
6431
|
+
expect((await server.request("/secret", { method: "HEAD" })).status).toBe(401);
|
|
6432
|
+
expect(protectedHandler).not.toHaveBeenCalled();
|
|
6433
|
+
expect((await server.request("/secret", {
|
|
6434
|
+
method: "HEAD",
|
|
6435
|
+
...authed
|
|
6436
|
+
})).status).toBe(200);
|
|
6437
|
+
});
|
|
6438
|
+
it("matches routes case-sensitively when deciding auth", async () => {
|
|
6439
|
+
const protectedHandler = vi.fn(async (c) => c.json({ route: "protected" }));
|
|
6440
|
+
const server = await startWithAuth([registerApiRoute("/Admin", {
|
|
6441
|
+
method: "GET",
|
|
6442
|
+
requiresAuth: false,
|
|
6443
|
+
handler: async (c) => c.json({ route: "public" })
|
|
6444
|
+
}), registerApiRoute("/admin", {
|
|
6445
|
+
method: "GET",
|
|
6446
|
+
handler: protectedHandler
|
|
6447
|
+
})]);
|
|
6448
|
+
expect((await server.request("/admin")).status).toBe(401);
|
|
6449
|
+
expect(protectedHandler).not.toHaveBeenCalled();
|
|
6450
|
+
const publicResponse = await server.request("/Admin");
|
|
6451
|
+
expect(publicResponse.status).toBe(200);
|
|
6452
|
+
expect(await publicResponse.json()).toEqual({ route: "public" });
|
|
6453
|
+
});
|
|
6454
|
+
it("checks auth for percent-encoded paths against the route they decode to", async () => {
|
|
6455
|
+
const protectedHandler = vi.fn(async (c) => c.json({ route: "admin" }));
|
|
6456
|
+
const server = await startWithAuth([registerApiRoute("/admin", {
|
|
6457
|
+
method: "GET",
|
|
6458
|
+
handler: protectedHandler
|
|
6459
|
+
}), registerApiRoute("/:page", {
|
|
6460
|
+
method: "GET",
|
|
6461
|
+
requiresAuth: false,
|
|
6462
|
+
handler: async (c) => c.json({ route: "page" })
|
|
6463
|
+
})]);
|
|
6464
|
+
const response = await server.request("/%61dmin");
|
|
6465
|
+
expect(protectedHandler).not.toHaveBeenCalled();
|
|
6466
|
+
expect(response.status).not.toBe(200);
|
|
6467
|
+
expect((await server.request("/about")).status).toBe(200);
|
|
6468
|
+
});
|
|
6469
|
+
it("checks FGA with the params of the route that handles an overlapping path", async () => {
|
|
6470
|
+
const check = vi.fn(async (_user, { resource }) => resource.id === "open");
|
|
6471
|
+
const fga = {
|
|
6472
|
+
check,
|
|
6473
|
+
require: vi.fn(),
|
|
6474
|
+
filterAccessible: vi.fn()
|
|
6475
|
+
};
|
|
6476
|
+
const mastra = new Mastra({
|
|
6477
|
+
logger: false,
|
|
6478
|
+
server: {
|
|
6479
|
+
fga,
|
|
6480
|
+
apiRoutes: [registerApiRoute("/docs/:docId", {
|
|
6481
|
+
method: "GET",
|
|
6482
|
+
fga: {
|
|
6483
|
+
resourceType: "doc",
|
|
6484
|
+
resourceIdParam: "docId",
|
|
6485
|
+
permission: "docs:read"
|
|
6486
|
+
},
|
|
6487
|
+
handler: async (c) => c.json({ docId: c.req.param("docId") })
|
|
6488
|
+
}), registerApiRoute("/docs/open", {
|
|
6489
|
+
method: "GET",
|
|
6490
|
+
handler: async (c) => c.json({ route: "static" })
|
|
6491
|
+
})]
|
|
6492
|
+
}
|
|
6493
|
+
});
|
|
6494
|
+
const originalGetServer = mastra.getServer.bind(mastra);
|
|
6495
|
+
mastra.getServer = () => ({
|
|
6496
|
+
...originalGetServer(),
|
|
6497
|
+
auth: {
|
|
6498
|
+
authenticateToken: async (token) => token === "valid-token" ? { id: "user-1" } : null,
|
|
6499
|
+
authorize: async () => true
|
|
6500
|
+
}
|
|
6501
|
+
});
|
|
6502
|
+
const server = await startServer(mastra);
|
|
6503
|
+
close = server.close;
|
|
6504
|
+
const response = await server.request("/docs/open", { headers: { authorization: "Bearer valid-token" } });
|
|
6505
|
+
expect(response.status).toBe(200);
|
|
6506
|
+
expect(checkedIds(check)).toEqual(["open"]);
|
|
6507
|
+
});
|
|
6167
6508
|
});
|
|
6168
6509
|
}
|
|
6169
6510
|
// src/knowledge-routes-test-suite.ts
|
|
@@ -6735,6 +7076,6 @@ function createObservabilitySignalWriteTestSuite({ startServer }) {
|
|
|
6735
7076
|
});
|
|
6736
7077
|
});
|
|
6737
7078
|
}
|
|
6738
|
-
export { consumeSSEStream, createBodyLimitTestSuite, createCustomRouteFGATestSuite, createDefaultTestContext, createDurableDisconnectTestSuite, createHttpLoggingTestSuite, createKnowledgeRoutesTestSuite, createMCPRouteTestSuite, createMCPTransportTestSuite, createMultipartTestSuite, createObservabilitySignalWriteTestSuite, createRouteAdapterTestSuite, createStreamWithSensitiveData, createStreamWithUnserializableChunk, createStructuredWakeTestSuite, expectSerializedStreamChunks };
|
|
7079
|
+
export { consumeSSEStream, createBodyLimitTestSuite, createCustomRouteFGATestSuite, createDefaultTestContext, createDurableDisconnectTestSuite, createHttpLoggingTestSuite, createKnowledgeRoutesTestSuite, createMCPRouteTestSuite, createMCPTransportTestSuite, createMultipartTestSuite, createObservabilitySignalWriteTestSuite, createRouteAdapterTestSuite, createStreamDisconnectTestSuite, createStreamWithSensitiveData, createStreamWithUnserializableChunk, createStructuredWakeTestSuite, expectSerializedStreamChunks };
|
|
6739
7080
|
|
|
6740
7081
|
//# sourceMappingURL=index.js.map
|