-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(integration): add request body tests
- Loading branch information
1 parent
f0a1513
commit 6f9e4d6
Showing
5 changed files
with
135 additions
and
2 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
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,71 @@ | ||
openapi: 3.0.2 | ||
info: | ||
title: Request Body API | ||
version: 1.0.0 | ||
servers: | ||
- url: http://localhost:3000 | ||
paths: | ||
/resource: | ||
get: | ||
summary: Get Resource | ||
operationId: getResource | ||
responses: | ||
200: | ||
description: Success | ||
content: | ||
application/json: | ||
schema: | ||
$ref: "#/components/schemas/Resource" | ||
post: | ||
summary: Create Resource | ||
operationId: createResource | ||
requestBody: | ||
required: true | ||
content: | ||
application/json: | ||
schema: | ||
$ref: "#/components/schemas/NewResource" | ||
responses: | ||
201: | ||
description: Created | ||
content: | ||
application/json: | ||
schema: | ||
$ref: "#/components/schemas/Resource" | ||
patch: | ||
summary: Patch Resource | ||
operationId: patchResource | ||
requestBody: | ||
required: false | ||
content: | ||
application/json: | ||
schema: | ||
$ref: "#/components/schemas/NewResource" | ||
responses: | ||
200: | ||
description: Success | ||
content: | ||
application/json: | ||
schema: | ||
$ref: "#/components/schemas/Resource" | ||
components: | ||
schemas: | ||
Resource: | ||
allOf: | ||
- $ref: "#/components/schemas/NewResource" | ||
- type: object | ||
required: | ||
- id | ||
properties: | ||
id: | ||
type: string | ||
NewResource: | ||
type: object | ||
required: | ||
- name | ||
- value | ||
properties: | ||
name: | ||
type: string | ||
value: | ||
type: integer |
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
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,58 @@ | ||
import { describe, expect, expectTypeOf, test } from "vitest"; | ||
import { createOpenApiHttp } from "../src/openapi-http.js"; | ||
import type { paths } from "./fixtures/request-body.api.js"; | ||
import { HttpResponse, type StrictRequest } from "msw"; | ||
|
||
describe("Given an OpenAPI schema endpoint with request content", () => { | ||
const http = createOpenApiHttp<paths>({ baseUrl: "*" }); | ||
|
||
test("When a request contains content, Then the content can be used in the response", async () => { | ||
const request = new Request(new URL("/resource", "http://localhost:3000"), { | ||
method: "post", | ||
body: JSON.stringify({ name: "test-name", value: 16 }), | ||
}); | ||
|
||
const handler = http.post("/resource", async ({ request }) => { | ||
const newResource = await request.json(); | ||
return HttpResponse.json( | ||
{ ...newResource, id: "test-id" }, | ||
{ status: 201 }, | ||
); | ||
}); | ||
const result = await handler.run({ request }); | ||
|
||
const responseBody = await result?.response?.json(); | ||
expect(result?.response?.status).toBe(201); | ||
expect(responseBody).toStrictEqual({ | ||
id: "test-id", | ||
name: "test-name", | ||
value: 16, | ||
}); | ||
}); | ||
|
||
test("When a request is expected to contain content, Then the content is strict-typed", () => { | ||
type Endpoint = typeof http.post<"/resource">; | ||
const resolver = expectTypeOf<Endpoint>().parameter(1); | ||
const request = resolver.parameter(0).toHaveProperty("request"); | ||
|
||
request.toEqualTypeOf<StrictRequest<{ name: string; value: number }>>(); | ||
}); | ||
|
||
test("When a request content is optional, Then the content is strict-typed with optional", () => { | ||
type Endpoint = typeof http.patch<"/resource">; | ||
const resolver = expectTypeOf<Endpoint>().parameter(1); | ||
const request = resolver.parameter(0).toHaveProperty("request"); | ||
|
||
request.toEqualTypeOf< | ||
StrictRequest<{ name: string; value: number } | undefined> | ||
>(); | ||
}); | ||
|
||
test("When a request is not expected to contain content, Then the content is undefined", () => { | ||
type Endpoint = typeof http.get<"/resource">; | ||
const resolver = expectTypeOf<Endpoint>().parameter(1); | ||
const request = resolver.parameter(0).toHaveProperty("request"); | ||
|
||
request.toEqualTypeOf<StrictRequest<undefined>>(); | ||
}); | ||
}); |