|
| 1 | +import { env, version as nodeVersion } from 'node:process' |
| 2 | + |
| 3 | +import semver from 'semver' |
| 4 | +import { describe, test, expect, beforeAll, afterEach } from 'vitest' |
| 5 | + |
| 6 | +import { MockFetch } from '../test/mock_fetch.js' |
| 7 | +import { base64Encode, streamToString } from '../test/util.js' |
| 8 | + |
| 9 | +import { connectLambda } from './lambda_compat.js' |
| 10 | +import { getStore } from './main.js' |
| 11 | + |
| 12 | +beforeAll(async () => { |
| 13 | + if (semver.lt(nodeVersion, '18.0.0')) { |
| 14 | + const nodeFetch = await import('node-fetch') |
| 15 | + |
| 16 | + // @ts-expect-error Expected type mismatch between native implementation and node-fetch |
| 17 | + globalThis.fetch = nodeFetch.default |
| 18 | + // @ts-expect-error Expected type mismatch between native implementation and node-fetch |
| 19 | + globalThis.Request = nodeFetch.Request |
| 20 | + // @ts-expect-error Expected type mismatch between native implementation and node-fetch |
| 21 | + globalThis.Response = nodeFetch.Response |
| 22 | + // @ts-expect-error Expected type mismatch between native implementation and node-fetch |
| 23 | + globalThis.Headers = nodeFetch.Headers |
| 24 | + } |
| 25 | +}) |
| 26 | + |
| 27 | +afterEach(() => { |
| 28 | + delete env.NETLIFY_BLOBS_CONTEXT |
| 29 | +}) |
| 30 | + |
| 31 | +const deployID = '6527dfab35be400008332a1d' |
| 32 | +const siteID = '9a003659-aaaa-0000-aaaa-63d3720d8621' |
| 33 | +const key = '54321' |
| 34 | +const value = 'some value' |
| 35 | +const edgeToken = 'some other token' |
| 36 | +const edgeURL = 'https://edge.netlify' |
| 37 | + |
| 38 | +describe('With edge credentials', () => { |
| 39 | + test('Loads the credentials set via the `connectLambda` method', async () => { |
| 40 | + const mockLambdaEvent = { |
| 41 | + blobs: base64Encode({ token: edgeToken, url: edgeURL }), |
| 42 | + headers: { |
| 43 | + 'x-nf-deploy-id': deployID, |
| 44 | + 'x-nf-site-id': siteID, |
| 45 | + }, |
| 46 | + } |
| 47 | + const mockStore = new MockFetch() |
| 48 | + .get({ |
| 49 | + headers: { authorization: `Bearer ${edgeToken}` }, |
| 50 | + response: new Response(value), |
| 51 | + url: `${edgeURL}/${siteID}/production/${key}`, |
| 52 | + }) |
| 53 | + .get({ |
| 54 | + headers: { authorization: `Bearer ${edgeToken}` }, |
| 55 | + response: new Response(value), |
| 56 | + url: `${edgeURL}/${siteID}/production/${key}`, |
| 57 | + }) |
| 58 | + |
| 59 | + globalThis.fetch = mockStore.fetch |
| 60 | + |
| 61 | + connectLambda(mockLambdaEvent) |
| 62 | + |
| 63 | + const blobs = getStore({ |
| 64 | + edgeURL, |
| 65 | + name: 'production', |
| 66 | + token: edgeToken, |
| 67 | + siteID, |
| 68 | + }) |
| 69 | + |
| 70 | + const string = await blobs.get(key) |
| 71 | + expect(string).toBe(value) |
| 72 | + |
| 73 | + const stream = await blobs.get(key, { type: 'stream' }) |
| 74 | + expect(await streamToString(stream as unknown as NodeJS.ReadableStream)).toBe(value) |
| 75 | + |
| 76 | + expect(mockStore.fulfilled).toBeTruthy() |
| 77 | + }) |
| 78 | +}) |
0 commit comments