From aad48981ed7fb769659c9181b1adb78805b2f457 Mon Sep 17 00:00:00 2001 From: tadelesh Date: Mon, 3 Mar 2025 11:40:42 +0800 Subject: [PATCH] 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", +});