-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
77dd169
commit bf358d3
Showing
2 changed files
with
163 additions
and
118 deletions.
There are no files selected for viewing
222 changes: 104 additions & 118 deletions
222
...ges/parsers/src/openapi/3.1/paths/__test__/response/ResponsesObjectConverter.node.test.ts
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 |
---|---|---|
@@ -1,156 +1,142 @@ | ||
import { OpenAPIV3_1 } from "openapi-types"; | ||
import { createMockContext } from "../../../../../__test__/createMockContext.util"; | ||
import { ReferenceConverterNode } from "../../../schemas/ReferenceConverter.node"; | ||
import { RequestMediaTypeObjectConverterNode } from "../../request/RequestMediaTypeObjectConverter.node"; | ||
import { ResponsesObjectConverterNode } from "../../response/ResponsesObjectConverter.node"; | ||
|
||
describe("RequestMediaTypeObjectConverterNode", () => { | ||
describe("ResponsesObjectConverterNode", () => { | ||
const mockContext = createMockContext(); | ||
|
||
it("should handle application/json content type", () => { | ||
const input: OpenAPIV3_1.MediaTypeObject = { | ||
schema: { | ||
type: "object", | ||
properties: { | ||
name: { type: "string" }, | ||
it("should handle success responses", () => { | ||
const input: OpenAPIV3_1.ResponsesObject = { | ||
"200": { | ||
description: "Success response", | ||
content: { | ||
"application/json": { | ||
schema: { | ||
type: "object", | ||
properties: { | ||
data: { type: "string" }, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const converter = new RequestMediaTypeObjectConverterNode( | ||
{ | ||
input, | ||
context: mockContext, | ||
accessPath: [], | ||
pathId: "test", | ||
}, | ||
"application/json", | ||
); | ||
const converter = new ResponsesObjectConverterNode({ | ||
input, | ||
context: mockContext, | ||
accessPath: [], | ||
pathId: "test", | ||
}); | ||
|
||
expect(converter.contentType).toBe("application/json"); | ||
expect(converter.schema).toBeDefined(); | ||
const result = converter.convert(); | ||
expect(result?.responses).toBeDefined(); | ||
expect(result?.responses[0]?.response.statusCode).toBe(200); | ||
expect(result?.responses[0]?.response.description).toBe("Success response"); | ||
}); | ||
|
||
it("should handle application/octet-stream content type", () => { | ||
const input: OpenAPIV3_1.MediaTypeObject = { | ||
schema: { | ||
type: "object", | ||
required: ["data"], | ||
it("should handle error responses", () => { | ||
const input: OpenAPIV3_1.ResponsesObject = { | ||
"400": { | ||
description: "Bad request", | ||
content: { | ||
"application/json": { | ||
schema: { | ||
type: "object", | ||
properties: { | ||
error: { type: "string" }, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const converter = new RequestMediaTypeObjectConverterNode( | ||
{ | ||
input, | ||
context: mockContext, | ||
accessPath: [], | ||
pathId: "test", | ||
}, | ||
"application/octet-stream", | ||
); | ||
const converter = new ResponsesObjectConverterNode({ | ||
input, | ||
context: mockContext, | ||
accessPath: [], | ||
pathId: "test", | ||
}); | ||
|
||
expect(converter.contentType).toBe("application/octet-stream"); | ||
expect(converter.isOptional).toBe(false); | ||
const result = converter.convert(); | ||
expect(result?.errors).toBeDefined(); | ||
expect(result?.errors[0]?.statusCode).toBe(400); | ||
expect(result?.errors[0]?.description).toBe("Bad request"); | ||
}); | ||
|
||
it("should handle multipart/form-data content type", () => { | ||
const input: OpenAPIV3_1.MediaTypeObject = { | ||
schema: { | ||
type: "object", | ||
properties: { | ||
file: { | ||
type: "string", | ||
format: "binary", | ||
it("should handle responses with headers", () => { | ||
const input: OpenAPIV3_1.ResponsesObject = { | ||
"200": { | ||
description: "Success with headers", | ||
headers: { | ||
"X-Rate-Limit": { | ||
schema: { | ||
type: "integer", | ||
}, | ||
}, | ||
}, | ||
content: { | ||
"application/json": { | ||
schema: { | ||
type: "object", | ||
}, | ||
}, | ||
}, | ||
required: ["file"], | ||
}, | ||
}; | ||
|
||
const converter = new RequestMediaTypeObjectConverterNode( | ||
{ | ||
input, | ||
context: mockContext, | ||
accessPath: [], | ||
pathId: "test", | ||
}, | ||
"multipart/form-data", | ||
); | ||
const converter = new ResponsesObjectConverterNode({ | ||
input, | ||
context: mockContext, | ||
accessPath: [], | ||
pathId: "test", | ||
}); | ||
|
||
expect(converter.contentType).toBe("multipart/form-data"); | ||
expect(converter.fields).toBeDefined(); | ||
expect(converter.requiredFields).toContain("file"); | ||
const result = converter.convert(); | ||
expect(result?.responses[0]?.headers).toBeDefined(); | ||
}); | ||
|
||
it("should handle reference objects", () => { | ||
const input: OpenAPIV3_1.MediaTypeObject = { | ||
schema: { | ||
$ref: "#/components/schemas/Request", | ||
}, | ||
}; | ||
|
||
mockContext.document.components ??= {}; | ||
mockContext.document.components.schemas = { | ||
Request: { | ||
type: "object", | ||
properties: { | ||
data: { type: "string" }, | ||
it("should handle default responses", () => { | ||
const input: OpenAPIV3_1.ResponsesObject = { | ||
default: { | ||
description: "Default response", | ||
content: { | ||
"application/json": { | ||
schema: { | ||
type: "object", | ||
}, | ||
}, | ||
}, | ||
}, | ||
"200": { | ||
description: "Success", | ||
}, | ||
}; | ||
|
||
const converter = new RequestMediaTypeObjectConverterNode( | ||
{ | ||
input, | ||
context: mockContext, | ||
accessPath: [], | ||
pathId: "test", | ||
}, | ||
"application/json", | ||
); | ||
const converter = new ResponsesObjectConverterNode({ | ||
input, | ||
context: mockContext, | ||
accessPath: [], | ||
pathId: "test", | ||
}); | ||
|
||
expect(converter.schema).toBeDefined(); | ||
expect(converter.schema instanceof ReferenceConverterNode).toBe(true); | ||
const result = converter.convert(); | ||
expect(result?.responses).toBeDefined(); | ||
}); | ||
|
||
it("should warn when schema is missing", () => { | ||
const input: OpenAPIV3_1.MediaTypeObject = {}; | ||
it("should handle empty responses", () => { | ||
const input: OpenAPIV3_1.ResponsesObject = {}; | ||
|
||
new RequestMediaTypeObjectConverterNode( | ||
{ | ||
input, | ||
context: mockContext, | ||
accessPath: [], | ||
pathId: "test", | ||
}, | ||
"application/json", | ||
); | ||
|
||
expect(mockContext.errors.warning).toHaveBeenCalledWith({ | ||
message: expect.stringContaining("Expected media type schema"), | ||
path: ["test"], | ||
const converter = new ResponsesObjectConverterNode({ | ||
input, | ||
context: mockContext, | ||
accessPath: [], | ||
pathId: "test", | ||
}); | ||
}); | ||
|
||
it("should warn for unsupported content types", () => { | ||
const input: OpenAPIV3_1.MediaTypeObject = { | ||
schema: { | ||
type: "object", | ||
}, | ||
}; | ||
|
||
new RequestMediaTypeObjectConverterNode( | ||
{ | ||
input, | ||
context: mockContext, | ||
accessPath: [], | ||
pathId: "test", | ||
}, | ||
"text/plain", | ||
); | ||
|
||
expect(mockContext.errors.warning).toHaveBeenCalledWith({ | ||
message: expect.stringContaining("Expected request body"), | ||
path: ["test"], | ||
}); | ||
const result = converter.convert(); | ||
expect(result?.responses).toEqual([]); | ||
expect(result?.errors).toEqual([]); | ||
}); | ||
}); |
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