From aad48981ed7fb769659c9181b1adb78805b2f457 Mon Sep 17 00:00:00 2001 From: tadelesh Date: Mon, 3 Mar 2025 11:40:42 +0800 Subject: [PATCH 1/4] add spector test for jsonl --- packages/http-specs/spec-summary.md | 12 ++++++ .../http-specs/specs/streaming/jsonl/main.tsp | 33 ++++++++++++++++ .../specs/streaming/jsonl/mockapi.ts | 39 +++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 packages/http-specs/specs/streaming/jsonl/main.tsp create mode 100644 packages/http-specs/specs/streaming/jsonl/mockapi.ts diff --git a/packages/http-specs/spec-summary.md b/packages/http-specs/spec-summary.md index 941a64cee8..1c48fd362e 100644 --- a/packages/http-specs/spec-summary.md +++ b/packages/http-specs/spec-summary.md @@ -3537,6 +3537,18 @@ Verify that the name "with" works. Send this parameter to pass with value `ok`. Verify that the name "yield" works. Send this parameter to pass with value `ok`. +### Streaming_Jsonl_Basic_receive + +- Endpoint: `get /streaming/jsonl/basic/receive` + +Basic case of jsonl streaming for response. + +### Streaming_Jsonl_Basic_send + +- Endpoint: `post /streaming/jsonl/basic/send` + +Basic case of jsonl streaming for request. + ### Type_Array_BooleanValue_get - Endpoint: `get /type/array/boolean` diff --git a/packages/http-specs/specs/streaming/jsonl/main.tsp b/packages/http-specs/specs/streaming/jsonl/main.tsp new file mode 100644 index 0000000000..b9439592a8 --- /dev/null +++ b/packages/http-specs/specs/streaming/jsonl/main.tsp @@ -0,0 +1,33 @@ +import "@typespec/http"; +import "@typespec/http/streams"; +import "@typespec/spector"; + +using TypeSpec.Http; +using TypeSpec.Http.Streams; +using Spector; + +@doc("Test of jsonl streaming.") +@scenarioService("/streaming/jsonl") +namespace Streaming.Jsonl; + +@route("basic") +namespace Basic { + @scenario + @scenarioDoc(""" + Basic case of jsonl streaming for request. + """) + @route("send") + @post + op send(stream: JsonlStream): NoContentResponse; + + @scenario + @scenarioDoc(""" + Basic case of jsonl streaming for response. + """) + @route("receive") + op receive(): JsonlStream; + + model Info { + desc: string; + } +} diff --git a/packages/http-specs/specs/streaming/jsonl/mockapi.ts b/packages/http-specs/specs/streaming/jsonl/mockapi.ts new file mode 100644 index 0000000000..02f2c68540 --- /dev/null +++ b/packages/http-specs/specs/streaming/jsonl/mockapi.ts @@ -0,0 +1,39 @@ +import { MockRequest, passOnSuccess, ScenarioMockApi } from "@typespec/spec-api"; + +export const Scenarios: Record = {}; + +Scenarios.Streaming_Jsonl_Basic_send = passOnSuccess({ + uri: "/streaming/jsonl/send", + method: "post", + request: { + headers: { + "Content-Type": "application/jsonl", + }, + body: '{"desc": "one"}\n{"desc": "two"}\n{"desc": "three"}', + }, + response: { + status: 204, + }, + handler: (req: MockRequest) => { + req.expect.containsHeader("content-type", "application/jsonl"); + req.expect.rawBodyEquals('{"desc": "one"}\n{"desc": "two"}\n{"desc": "three"}'); + return { + status: 204, + }; + }, + kind: "MockApiDefinition", +}); + +Scenarios.Streaming_Jsonl_Basic_receive = passOnSuccess({ + uri: "/streaming/jsonl/receive", + method: "get", + request: {}, + response: { + status: 200, + body: { + rawContent: '{"desc": "one"}\n{"desc": "two"}\n{"desc": "three"}', + contentType: "application/jsonl", + }, + }, + kind: "MockApiDefinition", +}); From f934f7e0a81d61587efdc2f7c74d2ec84a3da357 Mon Sep 17 00:00:00 2001 From: tadelesh Date: Mon, 3 Mar 2025 11:41:52 +0800 Subject: [PATCH 2/4] changeset --- .chronus/changes/spector-jsonl-2025-2-3-11-41-47.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .chronus/changes/spector-jsonl-2025-2-3-11-41-47.md diff --git a/.chronus/changes/spector-jsonl-2025-2-3-11-41-47.md b/.chronus/changes/spector-jsonl-2025-2-3-11-41-47.md new file mode 100644 index 0000000000..0e0bd8e7c4 --- /dev/null +++ b/.chronus/changes/spector-jsonl-2025-2-3-11-41-47.md @@ -0,0 +1,7 @@ +--- +changeKind: feature +packages: + - "@typespec/http-specs" +--- + +Add tests for basic jsonl streaming. \ No newline at end of file From ffa0d7c77d0d3bdf11f2f2486ef1a135c95bb89d Mon Sep 17 00:00:00 2001 From: tadelesh Date: Mon, 3 Mar 2025 15:25:39 +0800 Subject: [PATCH 3/4] fix tests --- .../http-specs/specs/streaming/jsonl/mockapi.ts | 13 +++---------- packages/spector/src/server/server.ts | 2 +- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/packages/http-specs/specs/streaming/jsonl/mockapi.ts b/packages/http-specs/specs/streaming/jsonl/mockapi.ts index 02f2c68540..670b8b498b 100644 --- a/packages/http-specs/specs/streaming/jsonl/mockapi.ts +++ b/packages/http-specs/specs/streaming/jsonl/mockapi.ts @@ -1,4 +1,4 @@ -import { MockRequest, passOnSuccess, ScenarioMockApi } from "@typespec/spec-api"; +import { passOnSuccess, ScenarioMockApi } from "@typespec/spec-api"; export const Scenarios: Record = {}; @@ -9,18 +9,11 @@ Scenarios.Streaming_Jsonl_Basic_send = passOnSuccess({ headers: { "Content-Type": "application/jsonl", }, - body: '{"desc": "one"}\n{"desc": "two"}\n{"desc": "three"}', + body: Buffer.from('{"desc": "one"}\n{"desc": "two"}\n{"desc": "three"}'), }, response: { status: 204, }, - handler: (req: MockRequest) => { - req.expect.containsHeader("content-type", "application/jsonl"); - req.expect.rawBodyEquals('{"desc": "one"}\n{"desc": "two"}\n{"desc": "three"}'); - return { - status: 204, - }; - }, kind: "MockApiDefinition", }); @@ -31,7 +24,7 @@ Scenarios.Streaming_Jsonl_Basic_receive = passOnSuccess({ response: { status: 200, body: { - rawContent: '{"desc": "one"}\n{"desc": "two"}\n{"desc": "three"}', + rawContent: Buffer.from('{"desc": "one"}\n{"desc": "two"}\n{"desc": "three"}'), contentType: "application/jsonl", }, }, diff --git a/packages/spector/src/server/server.ts b/packages/spector/src/server/server.ts index 4b5247040f..5320d6a100 100644 --- a/packages/spector/src/server/server.ts +++ b/packages/spector/src/server/server.ts @@ -70,7 +70,7 @@ export class MockApiServer { this.app.use(bodyParser.text({ type: "text/plain" })); this.app.use( bodyParser.raw({ - type: ["application/octet-stream", "image/png"], + type: ["application/octet-stream", "image/png", "application/jsonl"], limit: "10mb", verify: rawBinaryBodySaver, }), From 5c0083d5169e42cf207122eb39e5a6eed8728404 Mon Sep 17 00:00:00 2001 From: tadelesh Date: Mon, 3 Mar 2025 22:44:12 +0800 Subject: [PATCH 4/4] changeset --- .chronus/changes/spector-jsonl-2025-2-3-22-44-4.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .chronus/changes/spector-jsonl-2025-2-3-22-44-4.md diff --git a/.chronus/changes/spector-jsonl-2025-2-3-22-44-4.md b/.chronus/changes/spector-jsonl-2025-2-3-22-44-4.md new file mode 100644 index 0000000000..1211c0bcd5 --- /dev/null +++ b/.chronus/changes/spector-jsonl-2025-2-3-22-44-4.md @@ -0,0 +1,7 @@ +--- +changeKind: feature +packages: + - "@typespec/spector" +--- + +Support `application/jsonl` as binary. \ No newline at end of file