test: fix ECONNREFUSED

This commit is contained in:
DIYgod
2019-02-01 15:03:13 +08:00
parent 5436a0bc18
commit b36838ac8c

View File

@@ -18,8 +18,9 @@ afterAll(() => {
delete process.env.CACHE_EXPIRE; delete process.env.CACHE_EXPIRE;
}); });
async function check(type) { describe('cache', () => {
process.env.CACHE_TYPE = type; it('memory', async () => {
process.env.CACHE_TYPE = 'memory';
server = require('../../lib/index').server; server = require('../../lib/index').server;
const request = supertest(server); const request = supertest(server);
@@ -34,21 +35,37 @@ async function check(type) {
expect(parsed2).toMatchObject(parsed1); expect(parsed2).toMatchObject(parsed1);
expect(response2.status).toBe(200); expect(response2.status).toBe(200);
expect(response2.headers[`x-koa-${type}-cache`]).toBe('true'); expect(response2.headers['x-koa-memory-cache']).toBe('true');
expect(response3.headers).not.toHaveProperty(`x-koa-${type === 'redis' ? 'memory' : 'redis'}-cache`); expect(response2.headers).not.toHaveProperty('x-koa-redis-cache');
await wait(1 * 1000 + 10); await wait(1 * 1000 + 10);
const response3 = await request.get('/test/1'); const response3 = await request.get('/test/1');
expect(response3.headers).not.toHaveProperty('x-koa-redis-cache'); expect(response3.headers).not.toHaveProperty('x-koa-redis-cache');
expect(response3.headers).not.toHaveProperty('x-koa-memory-cache'); expect(response3.headers).not.toHaveProperty('x-koa-memory-cache');
}
describe('cache', () => {
it('memory', async () => {
check('memory');
}); });
it('redis', async () => { it('redis', async () => {
check('redis'); process.env.CACHE_TYPE = 'redis';
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-redis-cache']).toBe('true');
expect(response2.headers).not.toHaveProperty('x-koa-memory-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');
}); });
}); });