|
| 1 | +import type { MediaTypeObject } from "openapi3-ts/oas30"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { findCompatibleMediaType } from "./findCompatibleMediaType"; |
| 4 | + |
| 5 | +describe("findCompatibleMediaType", () => { |
| 6 | + it("should return undefined if content is empty", () => { |
| 7 | + expect(findCompatibleMediaType({ content: {} })).toBeUndefined(); |
| 8 | + }); |
| 9 | + |
| 10 | + it("should return MediaTypeObject for application/json", () => { |
| 11 | + const mediaTypeObject: MediaTypeObject = { schema: { type: "object" } }; |
| 12 | + const result = findCompatibleMediaType({ |
| 13 | + content: { |
| 14 | + "application/json": mediaTypeObject, |
| 15 | + }, |
| 16 | + }); |
| 17 | + expect(result).toBe(mediaTypeObject); |
| 18 | + }); |
| 19 | + |
| 20 | + it("should return MediaTypeObject for application/json with charset", () => { |
| 21 | + const mediaTypeObject: MediaTypeObject = { |
| 22 | + schema: { type: "object" }, |
| 23 | + }; |
| 24 | + const result = findCompatibleMediaType({ |
| 25 | + content: { |
| 26 | + "application/json; charset=utf-8": mediaTypeObject, |
| 27 | + }, |
| 28 | + }); |
| 29 | + expect(result).toBe(mediaTypeObject); |
| 30 | + }); |
| 31 | + |
| 32 | + it("should return MediaTypeObject for */*", () => { |
| 33 | + const mediaTypeObject: MediaTypeObject = { |
| 34 | + schema: { type: "string" }, |
| 35 | + }; |
| 36 | + const result = findCompatibleMediaType({ |
| 37 | + content: { |
| 38 | + "*/*": mediaTypeObject, |
| 39 | + }, |
| 40 | + }); |
| 41 | + expect(result).toBe(mediaTypeObject); |
| 42 | + }); |
| 43 | + |
| 44 | + it("should return MediaTypeObject for application/octet-stream", () => { |
| 45 | + const mediaTypeObject: MediaTypeObject = { |
| 46 | + schema: { type: "string", format: "binary" }, |
| 47 | + }; |
| 48 | + const result = findCompatibleMediaType({ |
| 49 | + content: { |
| 50 | + "application/octet-stream": mediaTypeObject, |
| 51 | + }, |
| 52 | + }); |
| 53 | + expect(result).toBe(mediaTypeObject); |
| 54 | + }); |
| 55 | + |
| 56 | + it("should return MediaTypeObject for multipart/form-data", () => { |
| 57 | + const mediaTypeObject: MediaTypeObject = { |
| 58 | + schema: { |
| 59 | + type: "object", |
| 60 | + properties: { |
| 61 | + file: { type: "string", format: "binary" }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }; |
| 65 | + const result = findCompatibleMediaType({ |
| 66 | + content: { |
| 67 | + "multipart/form-data": mediaTypeObject, |
| 68 | + }, |
| 69 | + }); |
| 70 | + expect(result).toBe(mediaTypeObject); |
| 71 | + }); |
| 72 | + |
| 73 | + it("should return MediaTypeObject for text/csv", () => { |
| 74 | + const mediaTypeObject: MediaTypeObject = { |
| 75 | + schema: { type: "string" }, |
| 76 | + }; |
| 77 | + const result = findCompatibleMediaType({ |
| 78 | + content: { |
| 79 | + "text/csv": mediaTypeObject, |
| 80 | + }, |
| 81 | + }); |
| 82 | + expect(result).toBe(mediaTypeObject); |
| 83 | + }); |
| 84 | + |
| 85 | + it("should return MediaTypeObject for text/csv with charset", () => { |
| 86 | + const mediaTypeObject: MediaTypeObject = { |
| 87 | + schema: { type: "string" }, |
| 88 | + }; |
| 89 | + const result = findCompatibleMediaType({ |
| 90 | + content: { |
| 91 | + "text/csv; charset=utf-8": mediaTypeObject, |
| 92 | + }, |
| 93 | + }); |
| 94 | + expect(result).toBe(mediaTypeObject); |
| 95 | + }); |
| 96 | + |
| 97 | + it("should return undefined for unsupported media type", () => { |
| 98 | + const result = findCompatibleMediaType({ |
| 99 | + content: { |
| 100 | + "text/html": { schema: { type: "string" } }, |
| 101 | + }, |
| 102 | + }); |
| 103 | + expect(result).toBeUndefined(); |
| 104 | + }); |
| 105 | + |
| 106 | + it("should return the first compatible media type when multiple are present", () => { |
| 107 | + const jsonMediaType: MediaTypeObject = { |
| 108 | + schema: { type: "object" }, |
| 109 | + }; |
| 110 | + const csvMediaType: MediaTypeObject = { |
| 111 | + schema: { type: "string" }, |
| 112 | + }; |
| 113 | + const result = findCompatibleMediaType({ |
| 114 | + content: { |
| 115 | + "text/html": { schema: { type: "string" } }, |
| 116 | + "application/json": jsonMediaType, |
| 117 | + "text/csv": csvMediaType, |
| 118 | + }, |
| 119 | + }); |
| 120 | + expect(result).toBeDefined(); |
| 121 | + expect([jsonMediaType, csvMediaType]).toContain(result); |
| 122 | + }); |
| 123 | +}); |
0 commit comments