|
1 | 1 | import { describe, test, beforeEach, afterEach, expect } from "vitest"; |
2 | | -import { addMediaTypePlugin, removeMediaTypePlugin, setMediaTypeQuality } from "../index.js"; |
| 2 | +import { MockAgent, setGlobalDispatcher } from "undici"; |
| 3 | +import { addMediaTypePlugin, get, removeMediaTypePlugin, setMediaTypeQuality } from "../index.js"; |
3 | 4 | import { acceptableMediaTypes } from "./media-types.js"; |
4 | 5 |
|
5 | 6 |
|
@@ -76,5 +77,40 @@ describe("JSON Browser", () => { |
76 | 77 | expect(accept).to.equal("application/reference+json, application/foo; q=0.9, */*; q=0.001"); |
77 | 78 | }); |
78 | 79 | }); |
| 80 | + |
| 81 | + describe("Wildcard media type plugins", () => { |
| 82 | + const testDomain = "https://example.com"; |
| 83 | + let mockAgent: MockAgent; |
| 84 | + |
| 85 | + beforeEach(() => { |
| 86 | + addMediaTypePlugin("application/*+foo", { |
| 87 | + parse: async () => { // eslint-disable-line @typescript-eslint/require-await |
| 88 | + return { |
| 89 | + baseUri: "https://exmple.com/foo", |
| 90 | + root: null, |
| 91 | + anchorLocation: (fragment) => fragment ?? "" |
| 92 | + }; |
| 93 | + }, |
| 94 | + fileMatcher: async (path) => path.endsWith(".foo") // eslint-disable-line @typescript-eslint/require-await |
| 95 | + }); |
| 96 | + |
| 97 | + mockAgent = new MockAgent(); |
| 98 | + mockAgent.disableNetConnect(); |
| 99 | + setGlobalDispatcher(mockAgent); |
| 100 | + mockAgent.get(testDomain) |
| 101 | + .intercept({ method: "GET", path: "/foo" }) |
| 102 | + .reply(200, `{}`, { headers: { "content-type": "application/whatever+foo" } }); |
| 103 | + }); |
| 104 | + |
| 105 | + afterEach(async () => { |
| 106 | + removeMediaTypePlugin("application/*+foo"); |
| 107 | + await mockAgent.close(); |
| 108 | + }); |
| 109 | + |
| 110 | + test("should match wildcard media type", async () => { |
| 111 | + // Expect not to throw |
| 112 | + await get(`${testDomain}/foo`); |
| 113 | + }); |
| 114 | + }); |
79 | 115 | }); |
80 | 116 | }); |
0 commit comments