-
Notifications
You must be signed in to change notification settings - Fork 2
chore: test consistency
in getDeployStore
#145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,270 @@ | ||
import { Buffer } from 'node:buffer' | ||
import { env, version as nodeVersion } from 'node:process' | ||
|
||
import semver from 'semver' | ||
import { describe, test, expect, beforeAll, afterEach } from 'vitest' | ||
|
||
import { MockFetch } from '../test/mock_fetch.js' | ||
import { base64Encode } from '../test/util.js' | ||
|
||
import { getDeployStore, getStore } from './main.js' | ||
|
||
const deployID = '6527dfab35be400008332a1d' | ||
const siteID = '9a003659-aaaa-0000-aaaa-63d3720d8621' | ||
const key = '54321' | ||
const value = 'some value' | ||
const edgeToken = 'some other token' | ||
const edgeURL = 'https://edge.netlify' | ||
const uncachedEdgeURL = 'https://uncached.edge.netlify' | ||
|
||
beforeAll(async () => { | ||
if (semver.lt(nodeVersion, '18.0.0')) { | ||
const nodeFetch = await import('node-fetch') | ||
|
||
// @ts-expect-error Expected type mismatch between native implementation and node-fetch | ||
globalThis.fetch = nodeFetch.default | ||
// @ts-expect-error Expected type mismatch between native implementation and node-fetch | ||
globalThis.Request = nodeFetch.Request | ||
// @ts-expect-error Expected type mismatch between native implementation and node-fetch | ||
globalThis.Response = nodeFetch.Response | ||
// @ts-expect-error Expected type mismatch between native implementation and node-fetch | ||
globalThis.Headers = nodeFetch.Headers | ||
} | ||
}) | ||
|
||
afterEach(() => { | ||
delete env.NETLIFY_BLOBS_CONTEXT | ||
delete globalThis.netlifyBlobsContext | ||
}) | ||
|
||
describe('Consistency configuration', () => { | ||
test('Respects the consistency mode supplied in the operation methods', async () => { | ||
const mockMetadata = { | ||
name: 'Netlify', | ||
cool: true, | ||
functions: ['edge', 'serverless'], | ||
} | ||
const headers = { | ||
etag: '123456789', | ||
'x-amz-meta-user': `b64;${base64Encode(mockMetadata)}`, | ||
} | ||
const mockStore = new MockFetch() | ||
.get({ | ||
headers: { authorization: `Bearer ${edgeToken}` }, | ||
response: new Response(value), | ||
url: `${uncachedEdgeURL}/${siteID}/production/${key}`, | ||
}) | ||
.head({ | ||
headers: { authorization: `Bearer ${edgeToken}` }, | ||
response: new Response(null, { headers }), | ||
url: `${uncachedEdgeURL}/${siteID}/production/${key}`, | ||
}) | ||
.get({ | ||
headers: { authorization: `Bearer ${edgeToken}` }, | ||
response: new Response(value, { headers }), | ||
url: `${uncachedEdgeURL}/${siteID}/production/${key}`, | ||
}) | ||
|
||
globalThis.fetch = mockStore.fetch | ||
|
||
const context = { | ||
edgeURL, | ||
siteID, | ||
token: edgeToken, | ||
uncachedEdgeURL, | ||
} | ||
|
||
env.NETLIFY_BLOBS_CONTEXT = Buffer.from(JSON.stringify(context)).toString('base64') | ||
|
||
const blobs = getStore('production') | ||
|
||
const data = await blobs.get(key, { consistency: 'strong' }) | ||
expect(data).toBe(value) | ||
|
||
const meta = await blobs.getMetadata(key, { consistency: 'strong' }) | ||
expect(meta?.etag).toBe(headers.etag) | ||
expect(meta?.metadata).toEqual(mockMetadata) | ||
|
||
const dataWithMeta = await blobs.getWithMetadata(key, { consistency: 'strong' }) | ||
expect(dataWithMeta?.data).toBe(value) | ||
expect(dataWithMeta?.etag).toBe(headers.etag) | ||
expect(dataWithMeta?.metadata).toEqual(mockMetadata) | ||
|
||
expect(mockStore.fulfilled).toBeTruthy() | ||
}) | ||
|
||
test('Respects the consistency mode supplied in the `getStore()` constructor', async () => { | ||
const mockMetadata = { | ||
name: 'Netlify', | ||
cool: true, | ||
functions: ['edge', 'serverless'], | ||
} | ||
const headers = { | ||
etag: '123456789', | ||
'x-amz-meta-user': `b64;${base64Encode(mockMetadata)}`, | ||
} | ||
const mockStore = new MockFetch() | ||
.get({ | ||
headers: { authorization: `Bearer ${edgeToken}` }, | ||
response: new Response(value), | ||
url: `${uncachedEdgeURL}/${siteID}/production/${key}`, | ||
}) | ||
.head({ | ||
headers: { authorization: `Bearer ${edgeToken}` }, | ||
response: new Response(null, { headers }), | ||
url: `${uncachedEdgeURL}/${siteID}/production/${key}`, | ||
}) | ||
.get({ | ||
headers: { authorization: `Bearer ${edgeToken}` }, | ||
response: new Response(value, { headers }), | ||
url: `${uncachedEdgeURL}/${siteID}/production/${key}`, | ||
}) | ||
|
||
globalThis.fetch = mockStore.fetch | ||
|
||
const blobs = getStore({ | ||
consistency: 'strong', | ||
edgeURL, | ||
name: 'production', | ||
token: edgeToken, | ||
siteID, | ||
uncachedEdgeURL, | ||
}) | ||
|
||
const data = await blobs.get(key) | ||
expect(data).toBe(value) | ||
|
||
const meta = await blobs.getMetadata(key) | ||
expect(meta?.etag).toBe(headers.etag) | ||
expect(meta?.metadata).toEqual(mockMetadata) | ||
|
||
const dataWithMeta = await blobs.getWithMetadata(key) | ||
expect(dataWithMeta?.data).toBe(value) | ||
expect(dataWithMeta?.etag).toBe(headers.etag) | ||
expect(dataWithMeta?.metadata).toEqual(mockMetadata) | ||
|
||
expect(mockStore.fulfilled).toBeTruthy() | ||
}) | ||
|
||
test.only('Respects the consistency mode supplied in the `getDeployStore()` constructor', async () => { | ||
const mockMetadata = { | ||
name: 'Netlify', | ||
cool: true, | ||
functions: ['edge', 'serverless'], | ||
} | ||
const headers = { | ||
etag: '123456789', | ||
'x-amz-meta-user': `b64;${base64Encode(mockMetadata)}`, | ||
} | ||
const mockStore = new MockFetch() | ||
.get({ | ||
headers: { authorization: `Bearer ${edgeToken}` }, | ||
response: new Response(value), | ||
url: `${uncachedEdgeURL}/${siteID}/deploy:${deployID}/${key}`, | ||
}) | ||
.head({ | ||
headers: { authorization: `Bearer ${edgeToken}` }, | ||
response: new Response(null, { headers }), | ||
url: `${uncachedEdgeURL}/${siteID}/deploy:${deployID}/${key}`, | ||
}) | ||
.get({ | ||
headers: { authorization: `Bearer ${edgeToken}` }, | ||
response: new Response(value, { headers }), | ||
url: `${uncachedEdgeURL}/${siteID}/deploy:${deployID}/${key}`, | ||
}) | ||
|
||
globalThis.fetch = mockStore.fetch | ||
|
||
const blobs = getDeployStore({ | ||
consistency: 'strong', | ||
edgeURL, | ||
deployID, | ||
token: edgeToken, | ||
siteID, | ||
uncachedEdgeURL, | ||
}) | ||
|
||
const data = await blobs.get(key) | ||
expect(data).toBe(value) | ||
|
||
const meta = await blobs.getMetadata(key) | ||
expect(meta?.etag).toBe(headers.etag) | ||
expect(meta?.metadata).toEqual(mockMetadata) | ||
|
||
const dataWithMeta = await blobs.getWithMetadata(key) | ||
expect(dataWithMeta?.data).toBe(value) | ||
expect(dataWithMeta?.etag).toBe(headers.etag) | ||
expect(dataWithMeta?.metadata).toEqual(mockMetadata) | ||
|
||
expect(mockStore.fulfilled).toBeTruthy() | ||
}) | ||
|
||
test('The consistency mode from the operation methods takes precedence over the store configuration', async () => { | ||
const mockMetadata = { | ||
name: 'Netlify', | ||
cool: true, | ||
functions: ['edge', 'serverless'], | ||
} | ||
const headers = { | ||
etag: '123456789', | ||
'x-amz-meta-user': `b64;${base64Encode(mockMetadata)}`, | ||
} | ||
const mockStore = new MockFetch() | ||
.get({ | ||
headers: { authorization: `Bearer ${edgeToken}` }, | ||
response: new Response(value), | ||
url: `${uncachedEdgeURL}/${siteID}/production/${key}`, | ||
}) | ||
.head({ | ||
headers: { authorization: `Bearer ${edgeToken}` }, | ||
response: new Response(null, { headers }), | ||
url: `${edgeURL}/${siteID}/production/${key}`, | ||
}) | ||
.get({ | ||
headers: { authorization: `Bearer ${edgeToken}` }, | ||
response: new Response(value, { headers }), | ||
url: `${edgeURL}/${siteID}/production/${key}`, | ||
}) | ||
|
||
globalThis.fetch = mockStore.fetch | ||
|
||
const blobs = getStore({ | ||
consistency: 'strong', | ||
edgeURL, | ||
name: 'production', | ||
token: edgeToken, | ||
siteID, | ||
uncachedEdgeURL, | ||
}) | ||
|
||
const data = await blobs.get(key) | ||
expect(data).toBe(value) | ||
|
||
const meta = await blobs.getMetadata(key, { consistency: 'eventual' }) | ||
expect(meta?.etag).toBe(headers.etag) | ||
expect(meta?.metadata).toEqual(mockMetadata) | ||
|
||
const dataWithMeta = await blobs.getWithMetadata(key, { consistency: 'eventual' }) | ||
expect(dataWithMeta?.data).toBe(value) | ||
expect(dataWithMeta?.etag).toBe(headers.etag) | ||
expect(dataWithMeta?.metadata).toEqual(mockMetadata) | ||
|
||
expect(mockStore.fulfilled).toBeTruthy() | ||
}) | ||
|
||
test('Throws when strong consistency is used and no `uncachedEdgeURL` property has been defined', async () => { | ||
const context = { | ||
edgeURL, | ||
siteID, | ||
token: edgeToken, | ||
} | ||
|
||
env.NETLIFY_BLOBS_CONTEXT = Buffer.from(JSON.stringify(context)).toString('base64') | ||
|
||
const store = getStore('productin') | ||
|
||
await expect(async () => await store.get('my-key', { consistency: 'strong' })).rejects.toThrowError( | ||
"Netlify Blobs has failed to perform a read using strong consistency because the environment has not been configured with a 'uncachedEdgeURL' property", | ||
) | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.