Browse Source

Increased test coverage

master
Kornel 8 years ago
parent
commit
557fc2c1eb
  1. 33
      test/misctest.js
  2. 41
      test/revalidatetest.js
  3. 7
      test/updatetest.js

33
test/misctest.js

@ -0,0 +1,33 @@
'use strict';
const assert = require('assert');
const CachePolicy = require('..');
describe('Other', function() {
it('Thaw wrong object', function() {
assert.throws(() => {
CachePolicy.fromObject({});
});
});
it('Missing headers', function() {
assert.throws(() => {
new CachePolicy({});
});
assert.throws(() => {
new CachePolicy({headers:{}}, {});
});
const cache = new CachePolicy({headers:{}}, {headers:{}});
assert.throws(() => {
cache.satisfiesWithoutRevalidation({});
});
assert.throws(() => {
cache.revalidatedPolicy({});
});
assert.throws(() => {
cache.revalidatedPolicy({headers:{}}, {});
});
});
});

41
test/revalidatetest.js

@ -77,6 +77,30 @@ describe('Can be revalidated?', function() {
assert.equal(headers['if-none-match'], '"123456789"');
});
it('skips weak validtors on post', function() {
const postReq = simpleRequestBut({method:'POST', headers:{'if-none-match': 'W/"weak", "strong", W/"weak2"'}});
const cache = new CachePolicy(postReq, multiValidatorResponse);
const headers = cache.revalidationHeaders(postReq);
assert.equal(headers['if-none-match'], '"strong", "123456789"');
assert.strictEqual(undefined, headers['if-modified-since']);
});
it('skips weak validtors on post 2', function() {
const postReq = simpleRequestBut({method:'POST', headers:{'if-none-match': 'W/"weak"'}});
const cache = new CachePolicy(postReq, lastModifiedResponse);
const headers = cache.revalidationHeaders(postReq);
assert.strictEqual(undefined, headers['if-none-match']);
assert.strictEqual(undefined, headers['if-modified-since']);
});
it('merges validtors', function() {
const postReq = simpleRequestBut({headers:{'if-none-match': 'W/"weak", "strong", W/"weak2"'}});
const cache = new CachePolicy(postReq, multiValidatorResponse);
const headers = cache.revalidationHeaders(postReq);
assert.equal(headers['if-none-match'], 'W/"weak", "strong", W/"weak2", "123456789"');
assert.equal('Tue, 15 Nov 1994 12:45:26 GMT', headers['if-modified-since']);
});
it('when last-modified validator is present', function() {
const cache = new CachePolicy(simpleRequest, lastModifiedResponse);
const headers = cache.revalidationHeaders(simpleRequest);
@ -94,6 +118,13 @@ describe('Can be revalidated?', function() {
});
describe('Validation request', function(){
it('removes warnings', function() {
const cache = new CachePolicy({headers:{}}, {headers:{
"warning": "199 test danger",
}});
assert.strictEqual(undefined, cache.responseHeaders().warning);
});
it('must contain any etag', function(){
const cache = new CachePolicy(simpleRequest,multiValidatorResponse);
@ -102,6 +133,16 @@ describe('Validation request', function(){
assert.equal(actual,expected);
});
it('merges etags', function(){
const cache = new CachePolicy(simpleRequest, etaggedResponse);
const expected = `"foo", "bar", ${etaggedResponse.headers.etag}`;
const headers = cache.revalidationHeaders(simpleRequestBut({headers:{
host:'www.w3c.org',
'if-none-match': '"foo", "bar"',
}}));
assert.equal(headers['if-none-match'],expected);
});
it('should send the Last-Modified value', function(){
const cache = new CachePolicy(simpleRequest,multiValidatorResponse);
const expected = multiValidatorResponse.headers['last-modified'];

7
test/updatetest.js

@ -60,8 +60,11 @@ describe('Update revalidated', function() {
assertUpdates(simpleRequest, multiValidatorResponse, simpleRequest, multiValidatorResponse);
});
it('Last-mod can vary if etag matches', function(){
assertUpdates(simpleRequest, multiValidatorResponse, simpleRequest, multiValidatorResponse);
it('Checks status', function(){
const response304 = Object.assign({}, multiValidatorResponse, {status:304});
const response200 = Object.assign({}, multiValidatorResponse, {status:200});
assertUpdates(simpleRequest, multiValidatorResponse, simpleRequest, response304);
assert(!notModifiedResponseHeaders(simpleRequest, multiValidatorResponse, simpleRequest, response200));
});
it('Last-mod ignored if etag is wrong', function(){

Loading…
Cancel
Save