test: middleware/cache

This commit is contained in:
DIYgod
2019-01-31 18:29:26 +08:00
parent c08d4481f0
commit 991c2407a4
3 changed files with 59 additions and 13 deletions

54
test/middleware/cache.js Normal file
View File

@@ -0,0 +1,54 @@
const supertest = require('supertest');
const Parser = require('rss-parser');
const parser = new Parser();
const wait = require('../../lib/utils/wait');
let server;
beforeAll(() => {
process.env.CACHE_EXPIRE = 1;
});
afterEach(() => {
delete process.env.CACHE_TYPE;
jest.resetModules();
server.close();
});
afterAll(() => {
delete process.env.CACHE_EXPIRE;
});
async function check(type) {
process.env.CACHE_TYPE = type;
server = require('../../lib/index').server;
const request = supertest(server);
const response1 = await request.get('/test/1');
const parsed1 = await parser.parseString(response1.text);
const response2 = await request.get('/test/1');
const parsed2 = await parser.parseString(response2.text);
delete parsed1.lastBuildDate;
delete parsed2.lastBuildDate;
expect(parsed2).toMatchObject(parsed1);
expect(response2.status).toBe(200);
expect(response2.headers[`x-koa-${type}-cache`]).toBe('true');
expect(response3.headers).not.toHaveProperty(`x-koa-${type === 'redis' ? 'memory' : 'redis'}-cache`);
await wait(1 * 1000 + 10);
const response3 = await request.get('/test/1');
expect(response3.headers).not.toHaveProperty('x-koa-redis-cache');
expect(response3.headers).not.toHaveProperty('x-koa-memory-cache');
}
describe('cache', () => {
it('memory', async () => {
check('memory');
});
it('redis', async () => {
check('redis');
});
});