|
1 |
| - |
2 |
| - |
3 | 1 | import { DummyFetchHandler } from "./dummyFetchHandler";
|
4 | 2 | import { CompressionHandlerOptions } from "../../../src/middlewares/options/compressionHandlerOptions";
|
5 | 3 | import { CompressionHandler } from "../../../src/middlewares/compressionHandler";
|
6 | 4 |
|
7 | 5 | const defaultOptions = new CompressionHandlerOptions();
|
8 | 6 |
|
9 |
| -import { assert, describe, it, expect, beforeEach, vi } from 'vitest'; |
| 7 | +import { assert, describe, it, expect, beforeEach, vi } from "vitest"; |
10 | 8 |
|
11 |
| -describe('CompressionHandler', () => { |
12 |
| - let compressionHandler: CompressionHandler; |
13 |
| - let nextMiddleware: DummyFetchHandler; |
| 9 | +describe("CompressionHandler", () => { |
| 10 | + let compressionHandler: CompressionHandler; |
| 11 | + let nextMiddleware: DummyFetchHandler; |
14 | 12 |
|
15 |
| - beforeEach(() => { |
16 |
| - nextMiddleware = new DummyFetchHandler(); |
17 |
| - compressionHandler = new CompressionHandler(); |
18 |
| - compressionHandler.next = nextMiddleware; |
19 |
| - }); |
| 13 | + beforeEach(() => { |
| 14 | + nextMiddleware = new DummyFetchHandler(); |
| 15 | + compressionHandler = new CompressionHandler(); |
| 16 | + compressionHandler.next = nextMiddleware; |
| 17 | + }); |
20 | 18 |
|
21 |
| - describe("constructor", () => { |
22 |
| - it("Should create an instance with given options", () => { |
23 |
| - const handler = new CompressionHandler(defaultOptions); |
24 |
| - assert.isDefined(handler["handlerOptions"]); |
25 |
| - }); |
| 19 | + describe("constructor", () => { |
| 20 | + it("Should create an instance with given options", () => { |
| 21 | + const handler = new CompressionHandler(defaultOptions); |
| 22 | + assert.isDefined(handler["handlerOptions"]); |
| 23 | + }); |
26 | 24 |
|
27 |
| - it("Should create an instance with default set of options", () => { |
28 |
| - const handler = new CompressionHandler(); |
29 |
| - assert.isDefined(handler["handlerOptions"]); |
30 |
| - }); |
31 |
| - }); |
| 25 | + it("Should create an instance with default set of options", () => { |
| 26 | + const handler = new CompressionHandler(); |
| 27 | + assert.isDefined(handler["handlerOptions"]); |
| 28 | + }); |
| 29 | + }); |
32 | 30 |
|
33 |
| - it('should throw an error if handlerOptions is undefined', () => { |
34 |
| - expect(() => new CompressionHandler(null as any)).toThrow('handlerOptions cannot be undefined'); |
35 |
| - }); |
| 31 | + it("should throw an error if handlerOptions is undefined", () => { |
| 32 | + expect(() => new CompressionHandler(null as any)).toThrow("handlerOptions cannot be undefined"); |
| 33 | + }); |
36 | 34 |
|
37 |
| - it('should not compress if ShouldCompress is false', async () => { |
38 |
| - const options = new CompressionHandlerOptions({ enableCompression: false }); |
39 |
| - compressionHandler = new CompressionHandler(options); |
| 35 | + it("should not compress if ShouldCompress is false", async () => { |
| 36 | + const options = new CompressionHandlerOptions({ enableCompression: false }); |
| 37 | + compressionHandler = new CompressionHandler(options); |
40 | 38 |
|
41 |
| - compressionHandler.next = nextMiddleware; |
42 |
| - vi.spyOn(nextMiddleware, 'execute'); |
43 |
| - nextMiddleware.setResponses([new Response("ok", { status: 200 })]); |
| 39 | + compressionHandler.next = nextMiddleware; |
| 40 | + vi.spyOn(nextMiddleware, "execute"); |
| 41 | + nextMiddleware.setResponses([new Response("ok", { status: 200 })]); |
44 | 42 |
|
45 |
| - const requestInit = { headers: new Headers(), body: 'test' }; |
46 |
| - const response = await compressionHandler.execute('http://example.com', requestInit); |
| 43 | + const requestInit = { headers: new Headers(), body: "test" }; |
| 44 | + const response = await compressionHandler.execute("http://example.com", requestInit); |
47 | 45 |
|
48 |
| - expect(requestInit.headers.has('Content-Encoding')).toBe(false); |
49 |
| - expect(nextMiddleware.execute).toHaveBeenCalled(); |
50 |
| - expect(response).toBeInstanceOf(Response); |
51 |
| - }); |
| 46 | + expect(requestInit.headers.has("Content-Encoding")).toBe(false); |
| 47 | + expect(nextMiddleware.execute).toHaveBeenCalled(); |
| 48 | + expect(response).toBeInstanceOf(Response); |
| 49 | + }); |
52 | 50 |
|
53 |
| - it('should compress the request body if ShouldCompress is true', async () => { |
54 |
| - const options = new CompressionHandlerOptions({ enableCompression: true }); |
55 |
| - compressionHandler = new CompressionHandler(options); |
| 51 | + it("should compress the request body if ShouldCompress is true", async () => { |
| 52 | + const options = new CompressionHandlerOptions({ enableCompression: true }); |
| 53 | + compressionHandler = new CompressionHandler(options); |
56 | 54 |
|
57 |
| - const requestInit = { headers: new Headers(), body: 'test' }; |
58 |
| - await compressionHandler.execute('http://example.com', requestInit); |
| 55 | + const requestInit = { headers: new Headers(), body: "test" }; |
| 56 | + await compressionHandler.execute("http://example.com", requestInit); |
59 | 57 |
|
60 |
| - expect(requestInit.headers.get('Content-Encoding')).toBe('gzip'); |
61 |
| - }); |
| 58 | + expect(requestInit.headers.get("Content-Encoding")).toBe("gzip"); |
| 59 | + }); |
62 | 60 |
|
63 |
| - it('should handle 415 response and retry without compression', async () => { |
64 |
| - const options = new CompressionHandlerOptions({ enableCompression: true }); |
65 |
| - compressionHandler = new CompressionHandler(options); |
| 61 | + it("should handle 415 response and retry without compression", async () => { |
| 62 | + const options = new CompressionHandlerOptions({ enableCompression: true }); |
| 63 | + compressionHandler = new CompressionHandler(options); |
66 | 64 |
|
67 |
| - compressionHandler.next = nextMiddleware; |
68 |
| - nextMiddleware.setResponses([new Response("nope", { status: 415 }), new Response("ok", { status: 200 })]); |
| 65 | + compressionHandler.next = nextMiddleware; |
| 66 | + nextMiddleware.setResponses([new Response("nope", { status: 415 }), new Response("ok", { status: 200 })]); |
69 | 67 |
|
70 |
| - //(nextMiddleware.execute as any).mockResolvedValueOnce(new Response(null, { status: 415 })); |
| 68 | + //(nextMiddleware.execute as any).mockResolvedValueOnce(new Response(null, { status: 415 })); |
71 | 69 |
|
72 |
| - const requestInit = { headers: new Headers(), body: 'test' }; |
73 |
| - const response = await compressionHandler.execute('http://example.com', requestInit); |
| 70 | + const requestInit = { headers: new Headers(), body: "test" }; |
| 71 | + const response = await compressionHandler.execute("http://example.com", requestInit); |
74 | 72 |
|
75 |
| - expect(requestInit.headers.has('Content-Encoding')).toBe(false); |
76 |
| - expect(response).toBeInstanceOf(Response); |
77 |
| - }); |
| 73 | + expect(requestInit.headers.has("Content-Encoding")).toBe(false); |
| 74 | + expect(response).toBeInstanceOf(Response); |
| 75 | + }); |
78 | 76 | });
|
0 commit comments