diff --git a/.chronus/changes/list_operation_test-2024-10-27-18-41-40.md b/.chronus/changes/list_operation_test-2024-10-27-18-41-40.md new file mode 100644 index 0000000000..f542056d7f --- /dev/null +++ b/.chronus/changes/list_operation_test-2024-10-27-18-41-40.md @@ -0,0 +1,7 @@ +--- +changeKind: feature +packages: + - "@typespec/http-specs" +--- + +add link case of server driven pagination test \ No newline at end of file diff --git a/packages/http-specs/spec-summary.md b/packages/http-specs/spec-summary.md index 86b49c1983..9fbaa6e661 100644 --- a/packages/http-specs/spec-summary.md +++ b/packages/http-specs/spec-summary.md @@ -1661,6 +1661,43 @@ Content-Type: application/octet-stream --abcde12345-- ``` +### Payload_Pageable_ServerDrivenPagination_link + +- Endpoint: `get /payload/pageable/server-driven-pagination/link` + +Test case for using link as pagination. + +Two requests need to be tested. + +1. Initial request: + Expected route: /payload/pageable/server-driven-pagination/link + Expected response body: + +```json +{ + "pets": [ + { "id": "1", "name": "dog" }, + { "id": "2", "name": "cat" } + ], + "links": { + "next": "http://[host]:[port]/payload/pageable/server-driven-pagination/link/nextPage" + } +} +``` + +2. Next page request: + Expected route: /payload/pageable/server-driven-pagination/link/nextPage + Expected response body: + +```json +{ + "pets": [ + { "id": "3", "name": "bird" }, + { "id": "4", "name": "fish" } + ] +} +``` + ### Payload_Xml_ModelWithArrayOfModelValue_get - Endpoint: `get /payload/xml/modelWithArrayOfModel` diff --git a/packages/http-specs/specs/payload/pageable/main.tsp b/packages/http-specs/specs/payload/pageable/main.tsp new file mode 100644 index 0000000000..b170d7e663 --- /dev/null +++ b/packages/http-specs/specs/payload/pageable/main.tsp @@ -0,0 +1,61 @@ +import "@typespec/http"; +import "@typespec/spector"; + +using Http; +using Spector; + +/** + * Test for pageable payload. + */ +@scenarioService("/payload/pageable") +namespace Payload.Pageable; + +model Pet { + id: string; + name: string; +} + +@route("/server-driven-pagination") +namespace ServerDrivenPagination { + @scenario + @scenarioDoc(""" + Test case for using link as pagination. + + Two requests need to be tested. + 1. Initial request: + Expected route: /payload/pageable/server-driven-pagination/link + Expected response body: + ```json + { "pets": [ + { "id": "1", "name": "dog" }, + { "id": "2", "name": "cat" } + ], + "links": { + "next": "http://[host]:[port]/payload/pageable/server-driven-pagination/link/nextPage" + } + } + ``` + 2. Next page request: + Expected route: /payload/pageable/server-driven-pagination/link/nextPage + Expected response body: + ```json + { "pets": [ + { "id": "3", "name": "bird" }, + { "id": "4", "name": "fish" } + ] + } + ``` + """) + @route("/link") + op link(): { + @pageItems + pets: Pet[]; + + links: { + @nextLink next?: url; + @prevLink prev?: url; + @firstLink first?: url; + @lastLink last?: url; + }; + }; +} diff --git a/packages/http-specs/specs/payload/pageable/mockapi.ts b/packages/http-specs/specs/payload/pageable/mockapi.ts new file mode 100644 index 0000000000..16be648a81 --- /dev/null +++ b/packages/http-specs/specs/payload/pageable/mockapi.ts @@ -0,0 +1,53 @@ +import { json, MockRequest, passOnSuccess, ScenarioMockApi } from "@typespec/spec-api"; + +export const Scenarios: Record = {}; + +Scenarios.Payload_Pageable_ServerDrivenPagination_link = passOnSuccess([ + { + uri: "/payload/pageable/server-driven-pagination/link", + method: "get", + request: {}, + response: { + status: 200, + body: json({ + pets: [ + { id: "1", name: "dog" }, + { id: "2", name: "cat" }, + ], + links: { + next: "/payload/pageable/server-driven-pagination/link/nextPage", + }, + }), + }, + handler: (req: MockRequest) => { + return { + status: 200, + body: json({ + pets: [ + { id: "1", name: "dog" }, + { id: "2", name: "cat" }, + ], + links: { + next: `${req.baseUrl}/payload/pageable/server-driven-pagination/link/nextPage`, + }, + }), + }; + }, + kind: "MockApiDefinition", + }, + { + uri: "/payload/pageable/server-driven-pagination/link/nextPage", + method: "get", + request: {}, + response: { + status: 200, + body: json({ + pets: [ + { id: "3", name: "bird" }, + { id: "4", name: "fish" }, + ], + }), + }, + kind: "MockApiDefinition", + }, +]);