|
1 | 1 | import { PrivateKey, TokenId } from 'o1js'; |
2 | | -import { createActionsList } from './fetch.js'; |
| 2 | +import { |
| 3 | + createActionsList, |
| 4 | + fetchAccount, |
| 5 | + fetchActions, |
| 6 | + fetchEvents, |
| 7 | + setArchiveDefaultHeaders, |
| 8 | + setArchiveGraphqlEndpoint, |
| 9 | + setGraphqlEndpoint, |
| 10 | + setMinaDefaultHeaders, |
| 11 | +} from './fetch.js'; |
3 | 12 | import { mockFetchActionsResponse as fetchResponseWithTxInfo } from './fixtures/fetch-actions-response-with-transaction-info.js'; |
4 | 13 | import { mockFetchActionsResponse as fetchResponseNoTxInfo } from './fixtures/fetch-actions-response-without-transaction-info.js'; |
5 | | -import { test, describe } from 'node:test'; |
| 14 | +import { test, describe, beforeEach, afterEach } from 'node:test'; |
6 | 15 | import { removeJsonQuotes } from './graphql.js'; |
7 | 16 | import { expect } from 'expect'; |
8 | 17 |
|
@@ -183,4 +192,173 @@ describe('Fetch', () => { |
183 | 192 | }); |
184 | 193 | }); |
185 | 194 | }); |
| 195 | + |
| 196 | + const minaEndpoint = 'https://mina.dummy/graphql'; |
| 197 | + const archiveEndpoint = 'https://archive.dummy/graphql'; |
| 198 | + |
| 199 | + describe('Testing fetch headers', () => { |
| 200 | + let originalFetch: typeof global.fetch; |
| 201 | + let lastFetchOptions: any = null; |
| 202 | + |
| 203 | + beforeEach(() => { |
| 204 | + originalFetch = global.fetch; |
| 205 | + lastFetchOptions = undefined; |
| 206 | + global.fetch = (( |
| 207 | + input: RequestInfo | URL, |
| 208 | + init?: RequestInit |
| 209 | + ): Promise<Response> => { |
| 210 | + lastFetchOptions = init; |
| 211 | + let url: string; |
| 212 | + if (typeof input === 'string') { |
| 213 | + url = input; |
| 214 | + } else if (input instanceof URL) { |
| 215 | + url = input.toString(); |
| 216 | + } else { |
| 217 | + url = input.url; |
| 218 | + } |
| 219 | + |
| 220 | + if (url.includes('archive.dummy')) { |
| 221 | + return Promise.resolve({ |
| 222 | + ok: true, |
| 223 | + json: async () => ({ |
| 224 | + data: { |
| 225 | + events: [], |
| 226 | + }, |
| 227 | + }), |
| 228 | + } as Response); |
| 229 | + } else { |
| 230 | + return Promise.resolve({ |
| 231 | + ok: true, |
| 232 | + json: async () => ({ |
| 233 | + data: {}, |
| 234 | + }), |
| 235 | + } as Response); |
| 236 | + } |
| 237 | + }) as typeof fetch; |
| 238 | + |
| 239 | + setGraphqlEndpoint(minaEndpoint); |
| 240 | + setArchiveGraphqlEndpoint(archiveEndpoint); |
| 241 | + }); |
| 242 | + |
| 243 | + afterEach(() => { |
| 244 | + global.fetch = originalFetch; |
| 245 | + }); |
| 246 | + |
| 247 | + test('Mina default headers with per request headers', async () => { |
| 248 | + setMinaDefaultHeaders({ Authorization: 'Bearer mina-default-token' }); |
| 249 | + const perRequestHeaders = { 'X-Custom': 'custom-value' }; |
| 250 | + await fetchAccount( |
| 251 | + { publicKey: PrivateKey.random().toPublicKey().toBase58() }, |
| 252 | + minaEndpoint, |
| 253 | + { |
| 254 | + headers: perRequestHeaders, |
| 255 | + } |
| 256 | + ); |
| 257 | + |
| 258 | + expect(lastFetchOptions.headers).toMatchObject({ |
| 259 | + 'Content-Type': 'application/json', |
| 260 | + Authorization: 'Bearer mina-default-token', |
| 261 | + 'X-Custom': 'custom-value', |
| 262 | + }); |
| 263 | + }); |
| 264 | + |
| 265 | + test('Per request headers overrides default headers', async () => { |
| 266 | + setMinaDefaultHeaders({ Authorization: 'Bearer mina-default-token' }); |
| 267 | + |
| 268 | + const perRequestHeaders = { |
| 269 | + Authorization: 'Bearer override-token', |
| 270 | + 'X-Test': 'value', |
| 271 | + }; |
| 272 | + await fetchAccount( |
| 273 | + { publicKey: PrivateKey.random().toPublicKey().toBase58() }, |
| 274 | + minaEndpoint, |
| 275 | + { |
| 276 | + headers: perRequestHeaders, |
| 277 | + } |
| 278 | + ); |
| 279 | + |
| 280 | + expect(lastFetchOptions.headers).toMatchObject({ |
| 281 | + 'Content-Type': 'application/json', |
| 282 | + Authorization: 'Bearer override-token', |
| 283 | + 'X-Test': 'value', |
| 284 | + }); |
| 285 | + }); |
| 286 | + |
| 287 | + test('Archive default headers with per request headers', async () => { |
| 288 | + setArchiveDefaultHeaders({ |
| 289 | + Authorization: 'Bearer archive-default-token', |
| 290 | + }); |
| 291 | + |
| 292 | + const perRequestHeaders = { 'X-Another': 'another-value' }; |
| 293 | + await fetchEvents( |
| 294 | + { publicKey: PrivateKey.random().toPublicKey().toBase58() }, |
| 295 | + archiveEndpoint, |
| 296 | + {}, |
| 297 | + perRequestHeaders |
| 298 | + ); |
| 299 | + |
| 300 | + expect(lastFetchOptions.headers).toMatchObject({ |
| 301 | + 'Content-Type': 'application/json', |
| 302 | + Authorization: 'Bearer archive-default-token', |
| 303 | + 'X-Another': 'another-value', |
| 304 | + }); |
| 305 | + }); |
| 306 | + |
| 307 | + test('Only default and base headers are used', async () => { |
| 308 | + setMinaDefaultHeaders({ |
| 309 | + 'X-Default': 'default-header', |
| 310 | + Authorization: 'Bearer mina-default-token', |
| 311 | + }); |
| 312 | + await fetchAccount( |
| 313 | + { publicKey: PrivateKey.random().toPublicKey().toBase58() }, |
| 314 | + minaEndpoint, |
| 315 | + {} |
| 316 | + ); |
| 317 | + |
| 318 | + expect(lastFetchOptions.headers).toMatchObject({ |
| 319 | + 'Content-Type': 'application/json', |
| 320 | + 'X-Default': 'default-header', |
| 321 | + Authorization: 'Bearer mina-default-token', |
| 322 | + }); |
| 323 | + }); |
| 324 | + |
| 325 | + test('Default and per request headers are used for fetchActions', async () => { |
| 326 | + setMinaDefaultHeaders({ |
| 327 | + 'X-Default': 'default-header', |
| 328 | + }); |
| 329 | + |
| 330 | + const perRequestHeaders = { |
| 331 | + Authorization: 'Bearer archive-default-token', |
| 332 | + }; |
| 333 | + await fetchActions( |
| 334 | + { |
| 335 | + publicKey: PrivateKey.random().toPublicKey().toBase58(), |
| 336 | + actionStates: { |
| 337 | + fromActionState: '', |
| 338 | + endActionState: '', |
| 339 | + }, |
| 340 | + }, |
| 341 | + minaEndpoint, |
| 342 | + perRequestHeaders |
| 343 | + ); |
| 344 | + |
| 345 | + expect(lastFetchOptions.headers).toMatchObject({ |
| 346 | + 'Content-Type': 'application/json', |
| 347 | + 'X-Default': 'default-header', |
| 348 | + Authorization: 'Bearer archive-default-token', |
| 349 | + }); |
| 350 | + }); |
| 351 | + |
| 352 | + test('Only base headers are used', async () => { |
| 353 | + await fetchAccount( |
| 354 | + { publicKey: PrivateKey.random().toPublicKey().toBase58() }, |
| 355 | + minaEndpoint, |
| 356 | + {} |
| 357 | + ); |
| 358 | + |
| 359 | + expect(lastFetchOptions.headers).toMatchObject({ |
| 360 | + 'Content-Type': 'application/json', |
| 361 | + }); |
| 362 | + }); |
| 363 | + }); |
186 | 364 | }); |
0 commit comments