Skip to content

Commit 13863bc

Browse files
committed
Merge pull request restify#85 from dokie/master
Bug in the conditional request plugin
2 parents f2c2c68 + 791f2e7 commit 13863bc

2 files changed

Lines changed: 230 additions & 2 deletions

File tree

lib/plugins/conditional_request.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function conditionalRequest() {
3333
var i;
3434
var matched = false;
3535

36-
if (typeof (etag) === 'string' && etag.length === 0) {
36+
if (typeof (etag) === 'string' && etag.length !== 0) {
3737
if (req.headers['if-match']) {
3838
// RFC: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.24
3939

test/plugins.test.js

Lines changed: 229 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ test('setup', function (t) {
6464
SERVER.use(plugins.dateParser());
6565
SERVER.use(plugins.queryParser());
6666

67+
6768
SERVER.get('/foo/:id', function (req, res, next) {
6869
res.send();
6970
return next();
@@ -90,7 +91,6 @@ test('406', function (t) {
9091
}).end();
9192
});
9293

93-
9494
test('authorization basic ok', function (t) {
9595
var authz = 'Basic ' + new Buffer('user:secret').toString('base64');
9696
request('/foo/bar', { authorization: authz }, function (res) {
@@ -267,6 +267,234 @@ test('date expired', function (t) {
267267
}).end();
268268
});
269269

270+
test('Conditional Request with correct Etag and headers', function (t) {
271+
SERVER.get('/bodyurl3/:id',
272+
function (req, res, next) {
273+
res.etag = 'testETag';
274+
next();
275+
},
276+
plugins.conditionalRequest(),
277+
function (req, res, next) {
278+
res.body = 'testing 304';
279+
res.send();
280+
return next();
281+
}
282+
);
283+
284+
var opts = {
285+
hostname: '127.0.0.1',
286+
port: PORT,
287+
path: '/bodyurl3/foo',
288+
agent: false,
289+
method: 'GET',
290+
headers: {
291+
'If-Match': 'testETag',
292+
'If-None-Match': 'testETag'
293+
}
294+
};
295+
var client = http.request(opts, function (res) {
296+
t.equal(res.statusCode, 304);
297+
t.end();
298+
});
299+
client.end();
300+
});
301+
302+
test('Conditional Request with mismatched Etag and If-Match', function (t) {
303+
SERVER.get('/bodyurl4/:id',
304+
function (req, res, next) {
305+
res.etag = 'testETag';
306+
next();
307+
},
308+
plugins.conditionalRequest(),
309+
function (req, res, next) {
310+
res.body = 'testing 304';
311+
res.send();
312+
return next();
313+
}
314+
);
315+
316+
var opts = {
317+
hostname: '127.0.0.1',
318+
port: PORT,
319+
path: '/bodyurl4/foo',
320+
agent: false,
321+
method: 'GET',
322+
headers: {
323+
'If-Match': 'testETag2'
324+
}
325+
};
326+
var client = http.request(opts, function (res) {
327+
t.equal(res.statusCode, 412);
328+
t.end();
329+
});
330+
client.end();
331+
});
332+
333+
test('Conditional Request with valid If-Modified header and not modified content', function (t) {
334+
var now = new Date();
335+
SERVER.get('/bodyurl5/:id',
336+
function (req, res, next) {
337+
var yesterday = new Date(now.setDate(now.getDate()-1));
338+
res.header('Last-Modified', yesterday);
339+
next();
340+
},
341+
plugins.conditionalRequest(),
342+
function (req, res, next) {
343+
res.body = 'testing 304';
344+
res.send();
345+
return next();
346+
}
347+
);
348+
349+
var opts = {
350+
hostname: '127.0.0.1',
351+
port: PORT,
352+
path: '/bodyurl5/foo',
353+
agent: false,
354+
method: 'GET',
355+
headers: {
356+
'If-Modified-Since': new Date()
357+
}
358+
};
359+
var client = http.request(opts, function (res) {
360+
t.equal(res.statusCode, 304);
361+
t.end();
362+
});
363+
client.end();
364+
});
365+
366+
test('Conditional Request with valid If-Unmodified-Since header and modified content', function (t) {
367+
var now = new Date();
368+
var yesterday = new Date(now.setDate(now.getDate()-1));
369+
SERVER.get('/bodyurl6/:id',
370+
function (req, res, next) {
371+
res.header('Last-Modified', new Date());
372+
next();
373+
},
374+
plugins.conditionalRequest(),
375+
function (req, res, next) {
376+
res.body = 'testing 412';
377+
res.send();
378+
return next();
379+
}
380+
);
381+
382+
var opts = {
383+
hostname: '127.0.0.1',
384+
port: PORT,
385+
path: '/bodyurl6/foo',
386+
agent: false,
387+
method: 'GET',
388+
headers: {
389+
'If-Unmodified-Since': yesterday
390+
}
391+
};
392+
var client = http.request(opts, function (res) {
393+
t.equal(res.statusCode, 412);
394+
t.end();
395+
});
396+
client.end();
397+
});
398+
399+
test('Conditional Request with valid headers from ahead Timezone and unmodified content should be OK', function (t) {
400+
SERVER.get('/bodyurl7/:id',
401+
function (req, res, next) {
402+
res.header('Last-Modified', new Date());
403+
next();
404+
},
405+
plugins.conditionalRequest(),
406+
function (req, res, next) {
407+
res.body = 'testing 412';
408+
res.send();
409+
return next();
410+
}
411+
);
412+
413+
var now = new Date();
414+
var ahead = new Date(now.setHours(now.getHours()+5));
415+
416+
var opts = {
417+
hostname: '127.0.0.1',
418+
port: PORT,
419+
path: '/bodyurl7/foo',
420+
agent: false,
421+
method: 'GET',
422+
headers: {
423+
'If-Modified-Since': ahead
424+
}
425+
};
426+
var client = http.request(opts, function (res) {
427+
t.equal(res.statusCode, 200);
428+
t.end();
429+
});
430+
client.end();
431+
});
432+
433+
test('Conditional Request with valid headers from ahead Timezone and modified content should be OK', function (t) {
434+
SERVER.get('/bodyurl8/:id',
435+
function (req, res, next) {
436+
res.header('Last-Modified', new Date());
437+
next();
438+
},
439+
plugins.conditionalRequest(),
440+
function (req, res, next) {
441+
res.body = 'testing 412';
442+
res.send();
443+
return next();
444+
}
445+
);
446+
447+
var now = new Date();
448+
var ahead = new Date(now.setHours(now.getHours()+5));
449+
450+
var opts = {
451+
hostname: '127.0.0.1',
452+
port: PORT,
453+
path: '/bodyurl8/foo',
454+
agent: false,
455+
method: 'GET',
456+
headers: {
457+
'If-Unmodified-Since': ahead
458+
}
459+
};
460+
var client = http.request(opts, function (res) {
461+
t.equal(res.statusCode, 200);
462+
t.end();
463+
});
464+
client.end();
465+
});
466+
467+
test('Conditional PUT with matched Etag and headers', function (t) {
468+
SERVER.put('/bodyurl9/:id',
469+
function (req, res, next) {
470+
res.etag = 'testETag';
471+
next();
472+
},
473+
plugins.conditionalRequest(),
474+
function (req, res, next) {
475+
res.body = 'testing 304';
476+
res.send();
477+
return next();
478+
}
479+
);
480+
481+
var opts = {
482+
hostname: '127.0.0.1',
483+
port: PORT,
484+
path: '/bodyurl9/foo',
485+
agent: false,
486+
method: 'PUT',
487+
headers: {
488+
'If-Match': 'testETag',
489+
'If-None-Match': 'testETag'
490+
}
491+
};
492+
var client = http.request(opts, function (res) {
493+
t.equal(res.statusCode, 412);
494+
t.end();
495+
});
496+
client.end();
497+
});
270498

271499
test('teardown', function (t) {
272500
SERVER.close(function () {

0 commit comments

Comments
 (0)