mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-09 14:40:23 +08:00
test: route cache in middleware/cache
This commit is contained in:
@@ -86,35 +86,8 @@ router.get('/', async (ctx) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get('/test/:id', (ctx) => {
|
// test
|
||||||
if (ctx.params.id === '0') {
|
router.get('/test/:id', require('./routes/test'));
|
||||||
throw Error('Error test');
|
|
||||||
}
|
|
||||||
const item = [];
|
|
||||||
if (ctx.params.id === 'long') {
|
|
||||||
item.push({
|
|
||||||
title: `Long Title `.repeat(10),
|
|
||||||
description: `Long Description `.repeat(10),
|
|
||||||
pubDate: new Date(`2018-3-1`).toUTCString(),
|
|
||||||
link: `https://github.com/DIYgod/RSSHub/issues/0`,
|
|
||||||
author: `DIYgod0`,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
for (let i = 1; i < 6; i++) {
|
|
||||||
item.push({
|
|
||||||
title: `Title${i}`,
|
|
||||||
description: `Description${i}`,
|
|
||||||
pubDate: new Date(`2018-4-${i}`).toUTCString(),
|
|
||||||
link: `https://github.com/DIYgod/RSSHub/issues/${i}`,
|
|
||||||
author: `DIYgod${i}`,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
ctx.state.data = {
|
|
||||||
title: `Test ${ctx.params.id}`,
|
|
||||||
link: 'https://github.com/DIYgod/RSSHub',
|
|
||||||
item: item,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
// RSSHub
|
// RSSHub
|
||||||
router.get('/rsshub/rss', require('./routes/rsshub/rss'));
|
router.get('/rsshub/rss', require('./routes/rsshub/rss'));
|
||||||
|
|||||||
41
lib/routes/test/index.js
Normal file
41
lib/routes/test/index.js
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
const config = require('../../config');
|
||||||
|
let cacheIndex = 0;
|
||||||
|
|
||||||
|
module.exports = async (ctx) => {
|
||||||
|
if (ctx.params.id === '0') {
|
||||||
|
throw Error('Error test');
|
||||||
|
}
|
||||||
|
const item = [];
|
||||||
|
if (ctx.params.id === 'long') {
|
||||||
|
item.push({
|
||||||
|
title: `Long Title `.repeat(10),
|
||||||
|
description: `Long Description `.repeat(10),
|
||||||
|
pubDate: new Date(`2018-3-1`).toUTCString(),
|
||||||
|
link: `https://github.com/DIYgod/RSSHub/issues/0`,
|
||||||
|
author: `DIYgod0`,
|
||||||
|
});
|
||||||
|
} else if (ctx.params.id === 'cache') {
|
||||||
|
const description = await ctx.cache.tryGet('test', () => `Cache${++cacheIndex}`, config.cacheExpire * 2);
|
||||||
|
item.push({
|
||||||
|
title: 'Cache Title',
|
||||||
|
description: description,
|
||||||
|
pubDate: new Date(`2018-3-1`).toUTCString(),
|
||||||
|
link: `https://github.com/DIYgod/RSSHub/issues/0`,
|
||||||
|
author: `DIYgod0`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
for (let i = 1; i < 6; i++) {
|
||||||
|
item.push({
|
||||||
|
title: `Title${i}`,
|
||||||
|
description: `Description${i}`,
|
||||||
|
pubDate: new Date(`2018-4-${i}`).toUTCString(),
|
||||||
|
link: `https://github.com/DIYgod/RSSHub/issues/${i}`,
|
||||||
|
author: `DIYgod${i}`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
ctx.state.data = {
|
||||||
|
title: `Test ${ctx.params.id}`,
|
||||||
|
link: 'https://github.com/DIYgod/RSSHub',
|
||||||
|
item: item,
|
||||||
|
};
|
||||||
|
};
|
||||||
@@ -24,10 +24,10 @@ describe('cache', () => {
|
|||||||
server = require('../../lib/index').server;
|
server = require('../../lib/index').server;
|
||||||
const request = supertest(server);
|
const request = supertest(server);
|
||||||
|
|
||||||
const response1 = await request.get('/test/1');
|
const response1 = await request.get('/test/cache');
|
||||||
const parsed1 = await parser.parseString(response1.text);
|
const parsed1 = await parser.parseString(response1.text);
|
||||||
|
|
||||||
const response2 = await request.get('/test/1');
|
const response2 = await request.get('/test/cache');
|
||||||
const parsed2 = await parser.parseString(response2.text);
|
const parsed2 = await parser.parseString(response2.text);
|
||||||
|
|
||||||
delete parsed1.lastBuildDate;
|
delete parsed1.lastBuildDate;
|
||||||
@@ -38,10 +38,20 @@ describe('cache', () => {
|
|||||||
expect(response2.headers['x-koa-memory-cache']).toBe('true');
|
expect(response2.headers['x-koa-memory-cache']).toBe('true');
|
||||||
expect(response2.headers).not.toHaveProperty('x-koa-redis-cache');
|
expect(response2.headers).not.toHaveProperty('x-koa-redis-cache');
|
||||||
|
|
||||||
await wait(1 * 1000 + 10);
|
await wait(1 * 1000 + 100);
|
||||||
const response3 = await request.get('/test/1');
|
const response3 = await request.get('/test/cache');
|
||||||
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');
|
||||||
|
const parsed3 = await parser.parseString(response3.text);
|
||||||
|
|
||||||
|
await wait(1 * 1000 + 100);
|
||||||
|
const response4 = await request.get('/test/cache');
|
||||||
|
const parsed4 = await parser.parseString(response4.text);
|
||||||
|
|
||||||
|
expect(parsed1.items[0].content).toBe('Cache1');
|
||||||
|
expect(parsed2.items[0].content).toBe('Cache1');
|
||||||
|
expect(parsed3.items[0].content).toBe('Cache1');
|
||||||
|
expect(parsed4.items[0].content).toBe('Cache1');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('redis', async () => {
|
it('redis', async () => {
|
||||||
@@ -49,10 +59,10 @@ describe('cache', () => {
|
|||||||
server = require('../../lib/index').server;
|
server = require('../../lib/index').server;
|
||||||
const request = supertest(server);
|
const request = supertest(server);
|
||||||
|
|
||||||
const response1 = await request.get('/test/1');
|
const response1 = await request.get('/test/cache');
|
||||||
const parsed1 = await parser.parseString(response1.text);
|
const parsed1 = await parser.parseString(response1.text);
|
||||||
|
|
||||||
const response2 = await request.get('/test/1');
|
const response2 = await request.get('/test/cache');
|
||||||
const parsed2 = await parser.parseString(response2.text);
|
const parsed2 = await parser.parseString(response2.text);
|
||||||
|
|
||||||
delete parsed1.lastBuildDate;
|
delete parsed1.lastBuildDate;
|
||||||
@@ -63,9 +73,19 @@ describe('cache', () => {
|
|||||||
expect(response2.headers['x-koa-redis-cache']).toBe('true');
|
expect(response2.headers['x-koa-redis-cache']).toBe('true');
|
||||||
expect(response2.headers).not.toHaveProperty('x-koa-memory-cache');
|
expect(response2.headers).not.toHaveProperty('x-koa-memory-cache');
|
||||||
|
|
||||||
await wait(1 * 1000 + 10);
|
await wait(1 * 1000 + 100);
|
||||||
const response3 = await request.get('/test/1');
|
const response3 = await request.get('/test/cache');
|
||||||
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');
|
||||||
|
const parsed3 = await parser.parseString(response3.text);
|
||||||
|
|
||||||
|
await wait(1 * 1000 + 100);
|
||||||
|
const response4 = await request.get('/test/cache');
|
||||||
|
const parsed4 = await parser.parseString(response4.text);
|
||||||
|
|
||||||
|
expect(parsed1.items[0].content).toBe('Cache1');
|
||||||
|
expect(parsed2.items[0].content).toBe('Cache1');
|
||||||
|
expect(parsed3.items[0].content).toBe('Cache1');
|
||||||
|
expect(parsed4.items[0].content).toBe('Cache1');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user