diff --git a/.chronus/changes/copilot-add-csv-encoding-tests-2025-10-28-11-48-49.md b/.chronus/changes/copilot-add-csv-encoding-tests-2025-10-28-11-48-49.md new file mode 100644 index 00000000000..d2a1d3706c7 --- /dev/null +++ b/.chronus/changes/copilot-add-csv-encoding-tests-2025-10-28-11-48-49.md @@ -0,0 +1,7 @@ +--- +changeKind: feature +packages: + - "@typespec/http-specs" +--- + +Add tests for array encode. \ No newline at end of file diff --git a/packages/http-specs/spec-summary.md b/packages/http-specs/spec-summary.md index 082812f7ef7..70862966e7d 100644 --- a/packages/http-specs/spec-summary.md +++ b/packages/http-specs/spec-summary.md @@ -105,6 +105,90 @@ Expected behavior: Should handle nested and combined formatting. Test italic text formatting using _single asterisks_. Expected behavior: Text between \* should render as italic. +### Encode_Array_Property_commaDelimited + +- Endpoint: `post /encode/array/property/comma-delimited` + +Test operation with request and response model contains a string array property with commaDelimited encode. +Expected request body: + +```json +{ + "value": "blue,red,green" +} +``` + +Expected response body: + +```json +{ + "value": "blue,red,green" +} +``` + +### Encode_Array_Property_newlineDelimited + +- Endpoint: `post /encode/array/property/newline-delimited` + +Test operation with request and response model contains a string array property with newlineDelimited encode. +Expected request body: + +```json +{ + "value": "blue\nred\ngreen" +} +``` + +Expected response body: + +```json +{ + "value": "blue\nred\ngreen" +} +``` + +### Encode_Array_Property_pipeDelimited + +- Endpoint: `post /encode/array/property/pipe-delimited` + +Test operation with request and response model contains a string array property with pipeDelimited encode. +Expected request body: + +```json +{ + "value": "blue|red|green" +} +``` + +Expected response body: + +```json +{ + "value": "blue|red|green" +} +``` + +### Encode_Array_Property_spaceDelimited + +- Endpoint: `post /encode/array/property/space-delimited` + +Test operation with request and response model contains a string array property with spaceDelimited encode. +Expected request body: + +```json +{ + "value": "blue red green" +} +``` + +Expected response body: + +```json +{ + "value": "blue red green" +} +``` + ### Encode_Bytes_Header_base64 - Endpoint: `get /encode/bytes/header/base64` diff --git a/packages/http-specs/specs/encode/array/main.tsp b/packages/http-specs/specs/encode/array/main.tsp new file mode 100644 index 00000000000..4e707eeb41d --- /dev/null +++ b/packages/http-specs/specs/encode/array/main.tsp @@ -0,0 +1,112 @@ +import "@typespec/http"; +import "@typespec/spector"; + +using Http; +using Spector; + +@doc("Test for encode decorator on array.") +@scenarioService("/encode/array") +namespace Encode.Array; + +model CommaDelimitedArrayProperty { + @encode(ArrayEncoding.commaDelimited) + value: string[]; +} + +model SpaceDelimitedArrayProperty { + @encode(ArrayEncoding.spaceDelimited) + value: string[]; +} + +model PipeDelimitedArrayProperty { + @encode(ArrayEncoding.pipeDelimited) + value: string[]; +} + +model NewlineDelimitedArrayProperty { + @encode(ArrayEncoding.newlineDelimited) + value: string[]; +} + +@route("/property") +namespace Property { + @route("/comma-delimited") + @scenario + @scenarioDoc(""" + Test operation with request and response model contains a string array property with commaDelimited encode. + Expected request body: + ```json + { + "value": "blue,red,green" + } + ``` + Expected response body: + ```json + { + "value": "blue,red,green" + } + ``` + """) + @post + op commaDelimited(@body body: CommaDelimitedArrayProperty): CommaDelimitedArrayProperty; + + @route("/space-delimited") + @scenario + @scenarioDoc(""" + Test operation with request and response model contains a string array property with spaceDelimited encode. + Expected request body: + ```json + { + "value": "blue red green" + } + ``` + Expected response body: + ```json + { + "value": "blue red green" + } + ``` + """) + @post + op spaceDelimited(@body body: SpaceDelimitedArrayProperty): SpaceDelimitedArrayProperty; + + @route("/pipe-delimited") + @scenario + @scenarioDoc(""" + Test operation with request and response model contains a string array property with pipeDelimited encode. + Expected request body: + ```json + { + "value": "blue|red|green" + } + ``` + Expected response body: + ```json + { + "value": "blue|red|green" + } + ``` + """) + @post + op pipeDelimited(@body body: PipeDelimitedArrayProperty): PipeDelimitedArrayProperty; + + @route("/newline-delimited") + @scenario + @scenarioDoc(""" + Test operation with request and response model contains a string array property with newlineDelimited encode. + Expected request body: + ```json + { + "value": "blue\\nred\\ngreen" + } + ``` + Expected response body: + ```json + { + "value": "blue\\nred\\ngreen" + } + ``` + """) + @post + op newlineDelimited(@body body: NewlineDelimitedArrayProperty): NewlineDelimitedArrayProperty; +} diff --git a/packages/http-specs/specs/encode/array/mockapi.ts b/packages/http-specs/specs/encode/array/mockapi.ts new file mode 100644 index 00000000000..8b22eeb67ea --- /dev/null +++ b/packages/http-specs/specs/encode/array/mockapi.ts @@ -0,0 +1,43 @@ +import { json, passOnSuccess, ScenarioMockApi } from "@typespec/spec-api"; + +export const Scenarios: Record = {}; + +const colors = ["blue", "red", "green"]; + +function createPropertyServerTests(uri: string, delimiter: string) { + const encodedValue = colors.join(delimiter); + return passOnSuccess({ + uri, + method: "post", + request: { + body: json({ + value: encodedValue, + }), + }, + response: { + status: 200, + body: json({ value: encodedValue }), + }, + kind: "MockApiDefinition", + }); +} + +Scenarios.Encode_Array_Property_commaDelimited = createPropertyServerTests( + "/encode/array/property/comma-delimited", + ",", +); + +Scenarios.Encode_Array_Property_spaceDelimited = createPropertyServerTests( + "/encode/array/property/space-delimited", + " ", +); + +Scenarios.Encode_Array_Property_pipeDelimited = createPropertyServerTests( + "/encode/array/property/pipe-delimited", + "|", +); + +Scenarios.Encode_Array_Property_newlineDelimited = createPropertyServerTests( + "/encode/array/property/newline-delimited", + "\n", +);