koa-lite/test/request/fresh.js

51 lines
1.3 KiB
JavaScript
Raw Permalink Normal View History

'use strict';
2017-05-11 03:30:32 +00:00
const assert = require('assert');
const context = require('../helpers/context');
describe('ctx.fresh', () => {
describe('the request method is not GET and HEAD', () => {
it('should return false', () => {
const ctx = context();
2014-07-06 08:43:14 +00:00
ctx.req.method = 'POST';
2017-05-11 03:30:32 +00:00
assert.equal(ctx.fresh, false);
2015-10-12 20:36:41 +00:00
});
});
2014-07-06 08:43:14 +00:00
describe('the response is non-2xx', () => {
it('should return false', () => {
const ctx = context();
ctx.status = 404;
ctx.req.method = 'GET';
ctx.req.headers['if-none-match'] = '123';
ctx.set('ETag', '123');
2017-05-11 03:30:32 +00:00
assert.equal(ctx.fresh, false);
2015-10-12 20:36:41 +00:00
});
});
describe('the response is 2xx', () => {
describe('and etag matches', () => {
it('should return true', () => {
const ctx = context();
ctx.status = 200;
ctx.req.method = 'GET';
ctx.req.headers['if-none-match'] = '123';
ctx.set('ETag', '123');
2017-05-11 03:30:32 +00:00
assert.equal(ctx.fresh, true);
2015-10-12 20:36:41 +00:00
});
});
describe('and etag do not match', () => {
it('should return false', () => {
const ctx = context();
ctx.status = 200;
ctx.req.method = 'GET';
ctx.req.headers['if-none-match'] = '123';
ctx.set('ETag', 'hey');
2017-05-11 03:30:32 +00:00
assert.equal(ctx.fresh, false);
2015-10-12 20:36:41 +00:00
});
});
});
});