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