@@ -231,34 +231,105 @@ describe('Version Put', () => {
231231 assert . strictEqual ( resp . error , 'testing 123' ) ;
232232 } ) ;
233233
234- describe ( 'If-Match on a missing source' , ( ) => {
235- async function putMissing ( conditionals ) {
236- const sendCalls = [ ] ;
237- const mockClient = {
238- async send ( cmd ) {
239- sendCalls . push ( cmd ) ;
240- return { $metadata : { httpStatusCode : 200 } , ETag : '"new-etag"' } ;
241- } ,
242- } ;
243- const { putObjectWithVersion } = await esmock ( '../../../src/storage/version/put.js' , {
244- '../../../src/storage/object/get.js' : {
245- default : async ( ) => ( { status : 404 , metadata : { } } ) ,
246- } ,
247- '../../../src/storage/utils/version.js' : {
248- ifMatch : ( ) => mockClient ,
249- ifNoneMatch : ( ) => mockClient ,
234+ async function putWithConditionals ( current , conditionals ) {
235+ const sendCalls = [ ] ;
236+ const ifMatchValues = [ ] ;
237+ const mockClient = {
238+ async send ( cmd ) {
239+ sendCalls . push ( cmd ) ;
240+ return { $metadata : { httpStatusCode : 200 } , ETag : '"new-etag"' } ;
241+ } ,
242+ } ;
243+ const { putObjectWithVersion } = await esmock ( '../../../src/storage/version/put.js' , {
244+ '../../../src/storage/object/get.js' : { default : async ( ) => current } ,
245+ '../../../src/storage/utils/version.js' : {
246+ ifMatch : ( config , value ) => {
247+ ifMatchValues . push ( value ) ;
248+ return mockClient ;
250249 } ,
251- } ) ;
252- const resp = await putObjectWithVersion (
253- { } ,
254- { users : [ { email : 'a@b.com' } ] } ,
255- { org : 'org' , key : 'site/doc.html' , type : 'text/html' } ,
256- null ,
257- null ,
258- conditionals ,
259- ) ;
260- return { resp, sendCalls } ;
261- }
250+ ifNoneMatch : ( ) => mockClient ,
251+ } ,
252+ '../../../src/storage/version/audit.js' : { writeAuditEntry : async ( ) => { } } ,
253+ } ) ;
254+ const resp = await putObjectWithVersion (
255+ { } ,
256+ { users : [ { email : 'a@b.com' } ] } ,
257+ { org : 'org' , key : 'site/doc.html' , type : 'text/html' } ,
258+ null ,
259+ null ,
260+ conditionals ,
261+ ) ;
262+ return { resp, sendCalls, ifMatchValues } ;
263+ }
264+
265+ describe ( 'Client conditionals on an existing source' , ( ) => {
266+ const existing = {
267+ status : 200 , metadata : { id : 'doc-id' } , etag : '"current"' , contentType : 'text/html' ,
268+ } ;
269+
270+ it ( 'If-Match: * sends the current ETag to storage' , async ( ) => {
271+ const { resp, ifMatchValues } = await putWithConditionals ( existing , { ifMatch : '*' } ) ;
272+
273+ assert . strictEqual ( resp . status , 200 ) ;
274+ assert . deepStrictEqual ( ifMatchValues , [ '"current"' ] ) ;
275+ } ) ;
276+
277+ it ( 'If-Match with a specific ETag sends that ETag to storage' , async ( ) => {
278+ const { resp, ifMatchValues } = await putWithConditionals ( existing , { ifMatch : '"client"' } ) ;
279+
280+ assert . strictEqual ( resp . status , 200 ) ;
281+ assert . deepStrictEqual ( ifMatchValues , [ '"client"' ] ) ;
282+ } ) ;
283+
284+ it ( 'If-None-Match with the current ETag returns 412 without writing' , async ( ) => {
285+ const { resp, sendCalls } = await putWithConditionals ( existing , { ifNoneMatch : '"current"' } ) ;
286+
287+ assert . strictEqual ( resp . status , 412 ) ;
288+ assert . strictEqual ( sendCalls . length , 0 ) ;
289+ } ) ;
290+
291+ it ( 'If-None-Match with a weak form of the current ETag returns 412' , async ( ) => {
292+ const { resp, sendCalls } = await putWithConditionals ( existing , { ifNoneMatch : 'W/"current"' } ) ;
293+
294+ assert . strictEqual ( resp . status , 412 ) ;
295+ assert . strictEqual ( sendCalls . length , 0 ) ;
296+ } ) ;
297+
298+ it ( 'If-None-Match with a list that contains the current ETag returns 412' , async ( ) => {
299+ const { resp, sendCalls } = await putWithConditionals ( existing , { ifNoneMatch : '"other", "current"' } ) ;
300+
301+ assert . strictEqual ( resp . status , 412 ) ;
302+ assert . strictEqual ( sendCalls . length , 0 ) ;
303+ } ) ;
304+
305+ it ( 'If-None-Match with a different ETag updates the source' , async ( ) => {
306+ const { resp, sendCalls, ifMatchValues } = await putWithConditionals ( existing , { ifNoneMatch : '"other"' } ) ;
307+
308+ assert . strictEqual ( resp . status , 200 ) ;
309+ assert . strictEqual ( sendCalls . length , 1 ) ;
310+ assert . deepStrictEqual ( ifMatchValues , [ '"current"' ] ) ;
311+ } ) ;
312+ } ) ;
313+
314+ describe ( 'If-Match on a missing source' , ( ) => {
315+ const putMissing = ( conditionals ) => putWithConditionals (
316+ { status : 404 , metadata : { } } ,
317+ conditionals ,
318+ ) ;
319+
320+ it ( 'returns 412 without writing when both If-Match and If-None-Match are sent' , async ( ) => {
321+ const { resp, sendCalls } = await putMissing ( { ifMatch : '"stale-etag"' , ifNoneMatch : '*' } ) ;
322+
323+ assert . strictEqual ( resp . status , 412 ) ;
324+ assert . strictEqual ( sendCalls . length , 0 ) ;
325+ } ) ;
326+
327+ it ( 'creates the source for If-None-Match with a specific ETag' , async ( ) => {
328+ const { resp, sendCalls } = await putMissing ( { ifNoneMatch : '"some-etag"' } ) ;
329+
330+ assert . strictEqual ( resp . status , 201 ) ;
331+ assert . strictEqual ( sendCalls . length , 1 ) ;
332+ } ) ;
262333
263334 it ( 'returns 412 without writing for a specific ETag' , async ( ) => {
264335 const { resp, sendCalls } = await putMissing ( { ifMatch : '"stale-etag"' } ) ;
0 commit comments