Skip to content

Instantly share code, notes, and snippets.

@bstopp
Last active August 5, 2024 17:57
Show Gist options
  • Save bstopp/0c8f9b29a73840e06eb1e906f457ca91 to your computer and use it in GitHub Desktop.
Save bstopp/0c8f9b29a73840e06eb1e906f457ca91 to your computer and use it in GitHub Desktop.
Test PUT conditional logic in Miniflare
{
"name": "miniflare-bug",
"description": "",
"type": "module",
"scripts": {
"test": "mocha --spec=test/**/*.test.js"
},
"devDependencies": {
"miniflare": "^3.20240725.0",
"mocha": "^10.7.0"
}
}
import assert from 'node:assert';
import { Miniflare } from 'miniflare';
describe('put conditional', () => {
let mf;
let env;
beforeEach(async () => {
mf = new Miniflare({
modules: true,
script: `
export default {
async fetch(request, env, ctx) {
return new Response("Hello Miniflare!");
}
}
`,
r2Buckets: { BUCKET: 'BUCKET' },
});
env = await mf.getBindings();
});
afterEach(async () => {
if (mf) await mf.dispose();
});
it("should overwrite content if found (etagMatches: '*')", async () => {
const key = 'index.html';
// Put initial object
await env.BUCKET.put(key, 'Original Content', {});
const current = await env.BUCKET.get(key)
assert(current.etag);
const onlyIf = { etagMatches: '*' };
const update = await env.BUCKET.put(key, 'Overwrite Content', { onlyIf }); // Should have overwritten.
assert(update);
});
it("should not create new content (etagMatches: '*')", async () => {
const key = 'index.html';
const current = await env.BUCKET.get(key)
assert.ifError(current);
const onlyIf = { etagMatches: '*' };
const update = await env.BUCKET.put(key, 'New Content', { onlyIf }); // Should not have created content.
assert.ifError(update);
});
it("should not create new content (If-Match: '*')", async () => {
const key = 'index.html';
const current = await env.BUCKET.get(key)
assert.ifError(current);
const onlyIf = { 'If-Match': '*' };
const update = await env.BUCKET.put(key, 'New Content', { onlyIf }); // Should not have created content
assert.ifError(update);
});
it("should not create new content (If-Match: '*') explicit onlyIf headers", async () => {
const key = 'index.html';
const current = await env.BUCKET.get(key)
assert.ifError(current);
const headers = new Headers();
headers.set('If-Match', '*')
const update = await env.BUCKET.put(key, 'New Content', { onlyIf: headers }); // Should not have created content
assert.ifError(update);
});
it("should not overwrite content if found (etagDoesNotMatch: '*')", async () => {
const key = 'index.html';
// Put initial object
await env.BUCKET.put(key, 'Original Content', {});
const current = await env.BUCKET.get(key)
assert(current.etag);
const onlyIf = { etagDoesNotMatch: '*' };
const update = await env.BUCKET.put(key, 'Overwrite Content', { onlyIf });
assert.ifError(update);
});
it("should not overwrite content if found (If-None-Match: '*')", async () => {
const key = 'index.html';
// Put initial object
await env.BUCKET.put(key, 'Original Content', {});
const current = await env.BUCKET.get(key)
assert(current);
const onlyIf = { 'If-None-Match': '*' };
const update = await env.BUCKET.put(key, 'New Content', { onlyIf });
assert.ifError(update);
});
it("should create new content if not found (etagDoesNotMatch: '*')", async () => {
const key = 'index.html';
const current = await env.BUCKET.get(key)
assert.ifError(current);
const onlyIf = { etagDoesNotMatch: '*' };
const update = await env.BUCKET.put(key, 'New Content', { onlyIf });
assert(update);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment