-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[http-spec] add link case of server driven pagination test (#5211)
only cover next link scenario for pagination. more complex case could be added later.
- Loading branch information
Showing
4 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
changeKind: feature | ||
packages: | ||
- "@typespec/http-specs" | ||
--- | ||
|
||
add link case of server driven pagination test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { json, MockRequest, passOnSuccess, ScenarioMockApi } from "@typespec/spec-api"; | ||
|
||
export const Scenarios: Record<string, ScenarioMockApi> = {}; | ||
|
||
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", | ||
}, | ||
]); |