fork of hey-api/openapi-ts because I need some additional things
at feat/skip-token 242 lines 5.5 kB view raw
1import type { Auth } from '../core/auth'; 2import { getParseAs, setAuthParams } from '../utils'; 3 4describe('getParseAs', () => { 5 const scenarios: Array<{ 6 content: Parameters<typeof getParseAs>[0]; 7 parseAs: ReturnType<typeof getParseAs>; 8 }> = [ 9 { 10 content: null, 11 parseAs: 'stream', 12 }, 13 { 14 content: 'application/json', 15 parseAs: 'json', 16 }, 17 { 18 content: 'application/ld+json', 19 parseAs: 'json', 20 }, 21 { 22 content: 'application/ld+json;charset=utf-8', 23 parseAs: 'json', 24 }, 25 { 26 content: 'application/ld+json; charset=utf-8', 27 parseAs: 'json', 28 }, 29 { 30 content: 'multipart/form-data', 31 parseAs: 'formData', 32 }, 33 { 34 content: 'application/*', 35 parseAs: 'blob', 36 }, 37 { 38 content: 'audio/*', 39 parseAs: 'blob', 40 }, 41 { 42 content: 'image/*', 43 parseAs: 'blob', 44 }, 45 { 46 content: 'video/*', 47 parseAs: 'blob', 48 }, 49 { 50 content: 'text/*', 51 parseAs: 'text', 52 }, 53 { 54 content: 'unsupported', 55 parseAs: undefined, 56 }, 57 ]; 58 59 it.each(scenarios)('detects $content as $parseAs', async ({ content, parseAs }) => { 60 expect(getParseAs(content)).toEqual(parseAs); 61 }); 62}); 63 64describe('setAuthParams', () => { 65 it('sets bearer token in headers', async () => { 66 const auth = vi.fn().mockReturnValue('foo'); 67 const headers = new Headers(); 68 const query: Record<any, unknown> = {}; 69 await setAuthParams({ 70 auth, 71 headers, 72 query, 73 security: [ 74 { 75 name: 'baz', 76 scheme: 'bearer', 77 type: 'http', 78 }, 79 ], 80 }); 81 expect(auth).toHaveBeenCalled(); 82 expect(headers.get('baz')).toBe('Bearer foo'); 83 expect(Object.keys(query).length).toBe(0); 84 }); 85 86 it('sets access token in query', async () => { 87 const auth = vi.fn().mockReturnValue('foo'); 88 const headers = new Headers(); 89 const query: Record<any, unknown> = {}; 90 await setAuthParams({ 91 auth, 92 headers, 93 query, 94 security: [ 95 { 96 in: 'query', 97 name: 'baz', 98 scheme: 'bearer', 99 type: 'http', 100 }, 101 ], 102 }); 103 expect(auth).toHaveBeenCalled(); 104 expect(headers.get('baz')).toBeNull(); 105 expect(query.baz).toBe('Bearer foo'); 106 }); 107 108 it('sets Authorization header when `in` and `name` are undefined', async () => { 109 const auth = vi.fn().mockReturnValue('foo'); 110 const headers = new Headers(); 111 const query: Record<any, unknown> = {}; 112 await setAuthParams({ 113 auth, 114 headers, 115 query, 116 security: [ 117 { 118 type: 'http', 119 }, 120 ], 121 }); 122 expect(auth).toHaveBeenCalled(); 123 expect(headers.get('Authorization')).toBe('foo'); 124 expect(query).toEqual({}); 125 }); 126 127 it('sets first scheme only', async () => { 128 const auth = vi.fn().mockReturnValue('foo'); 129 const headers = new Headers(); 130 const query: Record<any, unknown> = {}; 131 await setAuthParams({ 132 auth, 133 headers, 134 query, 135 security: [ 136 { 137 name: 'baz', 138 scheme: 'bearer', 139 type: 'http', 140 }, 141 { 142 in: 'query', 143 name: 'baz', 144 scheme: 'bearer', 145 type: 'http', 146 }, 147 ], 148 }); 149 expect(auth).toHaveBeenCalled(); 150 expect(headers.get('baz')).toBe('Bearer foo'); 151 expect(Object.keys(query).length).toBe(0); 152 }); 153 154 it('sets first scheme with token', async () => { 155 const auth = vi.fn().mockImplementation((auth: Auth) => { 156 if (auth.type === 'apiKey') { 157 return; 158 } 159 return 'foo'; 160 }); 161 const headers = new Headers(); 162 const query: Record<any, unknown> = {}; 163 await setAuthParams({ 164 auth, 165 headers, 166 query, 167 security: [ 168 { 169 name: 'baz', 170 type: 'apiKey', 171 }, 172 { 173 in: 'query', 174 name: 'baz', 175 scheme: 'bearer', 176 type: 'http', 177 }, 178 ], 179 }); 180 expect(auth).toHaveBeenCalled(); 181 expect(headers.get('baz')).toBeNull(); 182 expect(query.baz).toBe('Bearer foo'); 183 }); 184 185 it('sets an API key in a cookie', async () => { 186 const auth = vi.fn().mockReturnValue('foo'); 187 const headers = new Headers(); 188 const query: Record<any, unknown> = {}; 189 await setAuthParams({ 190 auth, 191 headers, 192 query, 193 security: [ 194 { 195 in: 'cookie', 196 name: 'baz', 197 type: 'apiKey', 198 }, 199 ], 200 }); 201 expect(auth).toHaveBeenCalled(); 202 expect(headers.get('Cookie')).toBe('baz=foo'); 203 expect(query).toEqual({}); 204 }); 205 206 it('sets only one specific header', async () => { 207 const auth = vi.fn(({ name }: Auth) => { 208 if (name === 'baz') { 209 return 'foo'; 210 } 211 return 'buz'; 212 }); 213 const headers = new Headers(); 214 const query: Record<any, unknown> = {}; 215 await setAuthParams({ 216 auth, 217 headers, 218 query, 219 security: [ 220 { 221 name: 'baz', 222 scheme: 'bearer', 223 type: 'http', 224 }, 225 { 226 name: 'fiz', 227 type: 'http', 228 }, 229 { 230 in: 'query', 231 name: 'baz', 232 scheme: 'bearer', 233 type: 'http', 234 }, 235 ], 236 }); 237 expect(auth).toHaveBeenCalled(); 238 expect(headers.get('baz')).toBe('Bearer foo'); 239 expect(headers.get('fiz')).toBe('buz'); 240 expect(Object.keys(query).length).toBe(0); 241 }); 242});