|
| 1 | +import { |
| 2 | + getEventFromFetchRequest, |
| 3 | + getEventFromFetchResponse, |
| 4 | + getUrlFromFetchRequest, |
| 5 | + parseFetchHeaders, |
| 6 | +} from './fetch'; |
| 7 | + |
| 8 | +describe('fetch', () => { |
| 9 | + beforeAll(() => { |
| 10 | + jest.useFakeTimers(); |
| 11 | + jest.setSystemTime(new Date(2023, 10, 1)); |
| 12 | + }); |
| 13 | + |
| 14 | + describe('getEventFromFetchRequest', () => { |
| 15 | + it('should map a fetch request from a string url', () => { |
| 16 | + const request = getEventFromFetchRequest('1', 'http://localhost/api/path'); |
| 17 | + expect(request).toEqual({ |
| 18 | + http: { |
| 19 | + host: 'localhost', |
| 20 | + method: 'GET', |
| 21 | + path: '/api/path', |
| 22 | + port: NaN, |
| 23 | + requestHeaders: {}, |
| 24 | + state: 'sent', |
| 25 | + url: 'http://localhost/api/path', |
| 26 | + }, |
| 27 | + id: '1', |
| 28 | + timestamp: expect.any(Number), |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should parse the host port number', () => { |
| 33 | + const request = getEventFromFetchRequest('1', 'http://localhost:5671/api/path'); |
| 34 | + expect(request).toEqual({ |
| 35 | + http: { |
| 36 | + host: 'localhost:5671', |
| 37 | + method: 'GET', |
| 38 | + path: '/api/path', |
| 39 | + port: 5671, |
| 40 | + requestHeaders: {}, |
| 41 | + state: 'sent', |
| 42 | + url: 'http://localhost:5671/api/path', |
| 43 | + }, |
| 44 | + id: '1', |
| 45 | + timestamp: expect.any(Number), |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should map the fetch request headers', () => { |
| 50 | + const init: RequestInit = { |
| 51 | + headers: { |
| 52 | + 'x-array': 'value1', |
| 53 | + 'x-key': 'key1', |
| 54 | + }, |
| 55 | + }; |
| 56 | + |
| 57 | + const request = getEventFromFetchRequest('1', 'http://localhost/api/path', init); |
| 58 | + expect(request).toEqual({ |
| 59 | + http: { |
| 60 | + host: 'localhost', |
| 61 | + method: 'GET', |
| 62 | + path: '/api/path', |
| 63 | + port: NaN, |
| 64 | + requestHeaders: { |
| 65 | + 'x-array': 'value1', |
| 66 | + 'x-key': 'key1', |
| 67 | + }, |
| 68 | + state: 'sent', |
| 69 | + url: 'http://localhost/api/path', |
| 70 | + }, |
| 71 | + id: '1', |
| 72 | + timestamp: expect.any(Number), |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should map the fetch request body', () => { |
| 77 | + const init: RequestInit = { |
| 78 | + body: new URLSearchParams('key=value&name=test'), |
| 79 | + }; |
| 80 | + |
| 81 | + const request = getEventFromFetchRequest('1', 'http://localhost/api/path', init); |
| 82 | + expect(request).toEqual({ |
| 83 | + http: { |
| 84 | + host: 'localhost', |
| 85 | + method: 'GET', |
| 86 | + path: '/api/path', |
| 87 | + port: NaN, |
| 88 | + requestHeaders: {}, |
| 89 | + requestBody: 'key=value&name=test', |
| 90 | + state: 'sent', |
| 91 | + url: 'http://localhost/api/path', |
| 92 | + }, |
| 93 | + id: '1', |
| 94 | + timestamp: expect.any(Number), |
| 95 | + }); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + describe('getEventFromFetchRequest', () => { |
| 100 | + it('should map a fetch response', async () => { |
| 101 | + const request = getEventFromFetchRequest('1', 'http://localhost/api/path'); |
| 102 | + const response = await getEventFromFetchResponse(request, { |
| 103 | + headers: new MockHeaders({ key: 'value' }), |
| 104 | + status: 200, |
| 105 | + statusText: 'OK', |
| 106 | + type: 'default', |
| 107 | + text: () => Promise.resolve('test'), |
| 108 | + }); |
| 109 | + |
| 110 | + expect(response).toEqual({ |
| 111 | + http: { |
| 112 | + host: 'localhost', |
| 113 | + httpVersion: 'default', |
| 114 | + method: 'GET', |
| 115 | + path: '/api/path', |
| 116 | + port: NaN, |
| 117 | + requestBody: undefined, |
| 118 | + requestHeaders: {}, |
| 119 | + responseBody: 'test', |
| 120 | + responseHeaders: { |
| 121 | + key: 'value', |
| 122 | + }, |
| 123 | + state: 'received', |
| 124 | + statusCode: 200, |
| 125 | + statusMessage: 'OK', |
| 126 | + url: 'http://localhost/api/path', |
| 127 | + }, |
| 128 | + id: '1', |
| 129 | + parentId: undefined, |
| 130 | + timestamp: expect.any(Number), |
| 131 | + }); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + describe('getUrlFromFetchRequest', () => { |
| 136 | + it('should parse a Request type', () => { |
| 137 | + const info = new MockRequest('http://localhost/api/path'); |
| 138 | + expect(getUrlFromFetchRequest(info)?.href).toEqual('http://localhost/api/path'); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should parse a URL type', () => { |
| 142 | + const info = new URL('http://localhost/web'); |
| 143 | + expect(getUrlFromFetchRequest(info)?.href).toEqual('http://localhost/web'); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should parse a fully qualified string type', () => { |
| 147 | + const info = 'http://localhost/api/path'; |
| 148 | + expect(getUrlFromFetchRequest(info)?.href).toEqual('http://localhost/api/path'); |
| 149 | + }); |
| 150 | + |
| 151 | + it('should return a default for unqualified relative urls', () => { |
| 152 | + const info = '/api/path'; |
| 153 | + expect(getUrlFromFetchRequest(info)?.href).toBe('http://localhost/'); |
| 154 | + }); |
| 155 | + |
| 156 | + describe('jsDom', () => { |
| 157 | + beforeAll(() => { |
| 158 | + globalThis.location = { |
| 159 | + origin: 'http://localhost:2700', |
| 160 | + } as any; |
| 161 | + }); |
| 162 | + |
| 163 | + afterAll(() => { |
| 164 | + globalThis.location = {} as any; |
| 165 | + }); |
| 166 | + |
| 167 | + it('should parse a relative url when window is set', () => { |
| 168 | + const info = '/api/path'; |
| 169 | + expect(getUrlFromFetchRequest(info)?.href).toEqual('http://localhost:2700/api/path'); |
| 170 | + }); |
| 171 | + }); |
| 172 | + }); |
| 173 | + |
| 174 | + describe('parseFetchHeaders', () => { |
| 175 | + it('should map header value pairs', () => { |
| 176 | + const headers = parseFetchHeaders([ |
| 177 | + ['key', 'value'], |
| 178 | + ['name', 'test'], |
| 179 | + ]); |
| 180 | + |
| 181 | + expect(headers).toEqual({ |
| 182 | + key: 'value', |
| 183 | + name: 'test', |
| 184 | + }); |
| 185 | + }); |
| 186 | + |
| 187 | + it('should map header record', () => { |
| 188 | + const headers = parseFetchHeaders({ |
| 189 | + key: 'value', |
| 190 | + name: 'test', |
| 191 | + }); |
| 192 | + |
| 193 | + expect(headers).toEqual({ |
| 194 | + key: 'value', |
| 195 | + name: 'test', |
| 196 | + }); |
| 197 | + }); |
| 198 | + |
| 199 | + it('should map header object', () => { |
| 200 | + const headers = parseFetchHeaders( |
| 201 | + new MockHeaders({ |
| 202 | + key: 'value', |
| 203 | + name: 'test', |
| 204 | + }), |
| 205 | + ); |
| 206 | + |
| 207 | + expect(headers).toEqual({ |
| 208 | + key: 'value', |
| 209 | + name: 'test', |
| 210 | + }); |
| 211 | + }); |
| 212 | + }); |
| 213 | +}); |
| 214 | + |
| 215 | +// mock the libdom Headers class |
| 216 | +class MockHeaders { |
| 217 | + constructor(private values?: Record<string, string>) {} |
| 218 | + |
| 219 | + *entries(): IterableIterator<[string, string]> { |
| 220 | + for (const k in this.values) { |
| 221 | + yield [k, this.values[k]]; |
| 222 | + } |
| 223 | + } |
| 224 | +} |
| 225 | + |
| 226 | +// mock the libdom Request class |
| 227 | +class MockRequest { |
| 228 | + constructor(private _url: string) {} |
| 229 | + get headers(): MockHeaders { |
| 230 | + return new MockHeaders(); |
| 231 | + } |
| 232 | + get method(): string { |
| 233 | + return 'get'; |
| 234 | + } |
| 235 | + get url(): string { |
| 236 | + return this._url; |
| 237 | + } |
| 238 | +} |
0 commit comments