|
| 1 | +import { fetchRequest, fetchHead, HttpError } from '../../src/common/request.library.js'; |
| 2 | + |
| 3 | +describe('request.library', () => { |
| 4 | + const mockFetch = vi.fn(); |
| 5 | + |
| 6 | + beforeEach(() => { |
| 7 | + globalThis.fetch = mockFetch; |
| 8 | + }); |
| 9 | + |
| 10 | + afterEach(() => { |
| 11 | + vi.clearAllMocks(); |
| 12 | + }); |
| 13 | + |
| 14 | + describe('fetchRequest', () => { |
| 15 | + it('should parse JSON when Content-Type is application/json', async () => { |
| 16 | + const mockData = { message: 'hello' }; |
| 17 | + mockFetch.mockResolvedValueOnce({ |
| 18 | + ok: true, |
| 19 | + headers: new Headers({ 'Content-Type': 'application/json; charset=utf-8' }), |
| 20 | + json: async () => mockData |
| 21 | + } as unknown as Response); |
| 22 | + |
| 23 | + const result = await fetchRequest('https://example.com/api'); |
| 24 | + expect(result).toEqual(mockData); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should return raw text when Content-Type is not JSON', async () => { |
| 28 | + const mockText = 'hello world'; |
| 29 | + mockFetch.mockResolvedValueOnce({ |
| 30 | + ok: true, |
| 31 | + headers: new Headers({ 'Content-Type': 'text/plain' }), |
| 32 | + text: async () => mockText |
| 33 | + } as unknown as Response); |
| 34 | + |
| 35 | + const result = await fetchRequest('https://example.com/text'); |
| 36 | + expect(result).toEqual(mockText); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should strip prefix if provided', async () => { |
| 40 | + const prefix = ')]}\'\n'; |
| 41 | + const rawText = `${prefix}{"a":1}`; |
| 42 | + mockFetch.mockResolvedValueOnce({ |
| 43 | + ok: true, |
| 44 | + headers: new Headers({ 'Content-Type': 'application/json' }), |
| 45 | + text: async () => rawText |
| 46 | + } as unknown as Response); |
| 47 | + |
| 48 | + const result = await fetchRequest('https://example.com/data', {}, { prefix }); |
| 49 | + expect(result).toEqual({ a: 1 }); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should throw HttpError with JSON body on 400', async () => { |
| 53 | + const errorBody = { error: 'Bad request' }; |
| 54 | + mockFetch.mockResolvedValueOnce({ |
| 55 | + ok: false, |
| 56 | + status: 400, |
| 57 | + statusText: 'Bad Request', |
| 58 | + text: async () => JSON.stringify(errorBody) |
| 59 | + } as unknown as Response); |
| 60 | + |
| 61 | + try { |
| 62 | + await fetchRequest('https://example.com/api'); |
| 63 | + expect.fail('Should have thrown HttpError'); |
| 64 | + } catch (err: any) { |
| 65 | + expect(err).toBeInstanceOf(HttpError); |
| 66 | + expect(err.status).toBe(400); |
| 67 | + expect(err.statusText).toBe('Bad Request'); |
| 68 | + expect(err.body).toEqual(errorBody); |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + it('should throw HttpError with text body on 500 when JSON parsing fails', async () => { |
| 73 | + const errorText = 'Internal Server Error Occurred'; |
| 74 | + mockFetch.mockResolvedValueOnce({ |
| 75 | + ok: false, |
| 76 | + status: 500, |
| 77 | + statusText: 'Server Error', |
| 78 | + text: async () => errorText |
| 79 | + } as unknown as Response); |
| 80 | + |
| 81 | + try { |
| 82 | + await fetchRequest('https://example.com/api'); |
| 83 | + expect.fail('Should have thrown HttpError'); |
| 84 | + } catch (err: any) { |
| 85 | + expect(err).toBeInstanceOf(HttpError); |
| 86 | + expect(err.status).toBe(500); |
| 87 | + expect(err.body).toBe(errorText); |
| 88 | + } |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe('fetchHead', () => { |
| 93 | + it('should return status and headers on ok response', async () => { |
| 94 | + const mockHeaders = new Headers({ 'Content-Length': '123' }); |
| 95 | + mockFetch.mockResolvedValueOnce({ |
| 96 | + ok: true, |
| 97 | + status: 200, |
| 98 | + headers: mockHeaders |
| 99 | + } as unknown as Response); |
| 100 | + |
| 101 | + const result = await fetchHead('https://example.com/api'); |
| 102 | + expect(result.status).toBe(200); |
| 103 | + expect(result.headers).toBe(mockHeaders); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should throw Error on non-ok response (except Forbidden)', async () => { |
| 107 | + mockFetch.mockResolvedValueOnce({ |
| 108 | + ok: false, |
| 109 | + status: 404, |
| 110 | + statusText: 'Not Found' |
| 111 | + } as unknown as Response); |
| 112 | + |
| 113 | + await expect(fetchHead('https://example.com/api')).rejects.toThrow('404: Not Found'); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should return status and headers on Forbidden (403)', async () => { |
| 117 | + const mockHeaders = new Headers(); |
| 118 | + mockFetch.mockResolvedValueOnce({ |
| 119 | + ok: false, |
| 120 | + status: 403, |
| 121 | + headers: mockHeaders |
| 122 | + } as unknown as Response); |
| 123 | + |
| 124 | + const result = await fetchHead('https://example.com/api'); |
| 125 | + expect(result.status).toBe(403); |
| 126 | + expect(result.headers).toBe(mockHeaders); |
| 127 | + }); |
| 128 | + }); |
| 129 | +}); |
0 commit comments