Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/storage/version/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,5 +354,6 @@ export async function postObjectVersion(req, env, daCtx) {
// no body
}
const label = reqJSON?.label;
return /* await */ postObjectVersionWithLabel(label, env, daCtx);
if (!label) return { status: 400, error: 'label is required' };
return postObjectVersionWithLabel(label, env, daCtx);
}
43 changes: 33 additions & 10 deletions test/storage/version/put.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2927,7 +2927,37 @@ describe('Version Put', () => {
});

describe('postObjectVersion with no JSON body', () => {
it('handles req.json() throwing (no body) and creates version with undefined label', async () => {
it('returns 400 when req.json() throws (no body)', async () => {
const { postObjectVersion } = await esmock('../../../src/storage/version/put.js', {});

const req = {
json: () => {
throw new Error('no body');
},
};
const daCtx = {
bucket: 'b', org: 'o', key: 'r/doc.html', ext: 'html', users: [],
};
const resp = await postObjectVersion(req, {}, daCtx);

assert.strictEqual(resp.status, 400);
assert.strictEqual(resp.error, 'label is required');
});

it('returns 400 when request body has label: null', async () => {
const { postObjectVersion } = await esmock('../../../src/storage/version/put.js', {});

const req = { json: async () => ({ label: null }) };
const daCtx = {
bucket: 'b', org: 'o', key: 'r/doc.html', ext: 'html', users: [],
};
const resp = await postObjectVersion(req, {}, daCtx);

assert.strictEqual(resp.status, 400);
assert.strictEqual(resp.error, 'label is required');
});

it('returns 201 when a valid label is provided', async () => {
const mockGetObject = async () => ({
body: 'content',
contentType: 'text/html',
Expand All @@ -2954,20 +2984,13 @@ describe('Version Put', () => {
'../../../src/storage/utils/config.js': { default: () => ({}) },
});

// req.json() throws → label is undefined → postObjectVersionWithLabel(undefined, ...)
const req = {
json: () => {
throw new Error('no body');
},
};
const req = { json: async () => ({ label: 'My snapshot' }) };
const daCtx = {
bucket: 'b', org: 'o', key: 'r/doc.html', ext: 'html', users: [],
};
const resp = await postObjectVersion(req, {}, daCtx);

// No label means shouldCreateVersionObject is false → versionCreated stays false → 500
assert.strictEqual(resp.status, 500);
assert.strictEqual(resp.error, 'Version was not created');
assert.strictEqual(resp.status, 201);
});
});

Expand Down
Loading