test: test route for response test
This commit is contained in:
parent
83a1e4a574
commit
e12a67310f
|
|
@ -86,6 +86,28 @@ router.get('/', async (ctx) => {
|
|||
});
|
||||
});
|
||||
|
||||
router.get('/test', (ctx) => {
|
||||
ctx.state.data = {
|
||||
title: 'DIYgod',
|
||||
link: 'https://diygod.me/',
|
||||
description: '测试路由 Test route',
|
||||
item: [
|
||||
{
|
||||
title: 'Title1',
|
||||
description: 'Item1',
|
||||
pubDate: new Date('2018-4-2').toUTCString(),
|
||||
link: 'https://diygod.me/1',
|
||||
},
|
||||
{
|
||||
title: 'Title2',
|
||||
description: 'Item2',
|
||||
pubDate: new Date('2018-4-10').toUTCString(),
|
||||
link: 'https://diygod.me/2',
|
||||
},
|
||||
],
|
||||
};
|
||||
});
|
||||
|
||||
// RSSHub
|
||||
router.get('/rsshub/rss', require('./routes/rsshub/rss'));
|
||||
|
||||
|
|
|
|||
|
|
@ -82,7 +82,7 @@
|
|||
},
|
||||
"jest": {
|
||||
"testMatch": [
|
||||
"**/test/index.js"
|
||||
"**/test/*.js"
|
||||
],
|
||||
"coverageReporters": [
|
||||
"text-summary",
|
||||
|
|
|
|||
|
|
@ -1,4 +0,0 @@
|
|||
module.exports = {
|
||||
text: ['/'],
|
||||
rss: ['/bilibili/ranking/0/3'],
|
||||
};
|
||||
|
|
@ -1 +0,0 @@
|
|||
require('./response');
|
||||
|
|
@ -1,23 +1,70 @@
|
|||
const supertest = require('supertest');
|
||||
const app = require('../lib/index');
|
||||
const request = supertest(app.callback());
|
||||
const Parser = require('rss-parser');
|
||||
const parser = new Parser();
|
||||
const config = require('../lib/config');
|
||||
|
||||
const cases = require('./cases');
|
||||
const statusCheck = require('./rules/status');
|
||||
const check = require('./rules/index');
|
||||
async function checkRSS(response) {
|
||||
expect(response.headers['content-type']).toBe('application/xml; charset=utf-8');
|
||||
expect(response.headers['cache-control']).toBe(`max-age=${config.cacheExpire / 2}`);
|
||||
|
||||
const checkDate = (date) => {
|
||||
expect(date).toEqual(expect.any(String));
|
||||
expect(Date.parse(date)).toEqual(expect.any(Number));
|
||||
expect(new Date() - new Date(date)).toBeGreaterThan(-1000 * 60 * 60 * 24 * 5);
|
||||
expect(new Date() - new Date(date)).toBeLessThan(1000 * 60 * 60 * 24 * 30 * 12 * 5);
|
||||
};
|
||||
|
||||
const parsed = await parser.parseString(response.text);
|
||||
|
||||
expect(parsed).toEqual(expect.any(Object));
|
||||
expect(parsed.title).toEqual(expect.any(String));
|
||||
expect(parsed.title).not.toBe('RSSHub');
|
||||
expect(parsed.description).toEqual(expect.any(String));
|
||||
expect(parsed.link).toEqual(expect.any(String));
|
||||
expect(parsed.lastBuildDate).toEqual(expect.any(String));
|
||||
expect(parsed.items).toEqual(expect.any(Array));
|
||||
checkDate(parsed.lastBuildDate);
|
||||
|
||||
// check items
|
||||
const guids = [];
|
||||
parsed.items.forEach((item) => {
|
||||
expect(item).toEqual(expect.any(Object));
|
||||
expect(item.title).toEqual(expect.any(String));
|
||||
expect(item.link).toEqual(expect.any(String));
|
||||
expect(item.content).toEqual(expect.any(String));
|
||||
expect(item.guid).toEqual(expect.any(String));
|
||||
if (item.pubDate) {
|
||||
expect(item.pubDate).toEqual(expect.any(String));
|
||||
checkDate(item.pubDate);
|
||||
}
|
||||
|
||||
// guid must be unique
|
||||
expect(guids).not.toContain(item.guid);
|
||||
guids.push(item.guid);
|
||||
});
|
||||
}
|
||||
|
||||
describe('response', () => {
|
||||
cases.text.forEach((url) => {
|
||||
it(`GET ${url}`, async () => {
|
||||
const response = await request.get(url);
|
||||
statusCheck(response);
|
||||
});
|
||||
it(`/`, async () => {
|
||||
const response = await request.get('/');
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers['content-type']).toBe('text/html; charset=UTF-8');
|
||||
expect(response.headers['cache-control']).toBe('no-cache');
|
||||
});
|
||||
|
||||
cases.rss.forEach((url) => {
|
||||
it(`GET ${url}`, async () => {
|
||||
const response = await request.get(url);
|
||||
await check(response);
|
||||
}, 10e4);
|
||||
it(`/test (origin)`, async () => {
|
||||
const response = await request.get('/test');
|
||||
expect(response.status).toBe(200);
|
||||
|
||||
await checkRSS(response);
|
||||
});
|
||||
|
||||
it(`/test (cache)`, async () => {
|
||||
const response = await request.get('/test');
|
||||
expect(response.status).toBe(200);
|
||||
|
||||
await checkRSS(response);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,7 +0,0 @@
|
|||
const status = require('./status');
|
||||
const rss = require('./rss');
|
||||
|
||||
module.exports = async (response) => {
|
||||
status(response);
|
||||
await rss(response);
|
||||
};
|
||||
|
|
@ -1,41 +0,0 @@
|
|||
const Parser = require('rss-parser');
|
||||
const parser = new Parser();
|
||||
|
||||
function checkDate(date) {
|
||||
expect(date).toEqual(expect.any(String));
|
||||
expect(Date.parse(date)).toEqual(expect.any(Number));
|
||||
expect(new Date() - new Date(date)).toBeGreaterThan(-1000 * 60 * 60 * 24 * 5);
|
||||
// date must be in 1 year
|
||||
expect(new Date() - new Date(date)).toBeLessThan(1000 * 60 * 60 * 24 * 30 * 12 * 5);
|
||||
}
|
||||
|
||||
module.exports = async (response) => {
|
||||
const parsed = await parser.parseString(response.text);
|
||||
|
||||
expect(parsed).toEqual(expect.any(Object));
|
||||
expect(parsed.title).toEqual(expect.any(String));
|
||||
expect(parsed.title).not.toBe('RSSHub');
|
||||
expect(parsed.description).toEqual(expect.any(String));
|
||||
expect(parsed.link).toEqual(expect.any(String));
|
||||
expect(parsed.lastBuildDate).toEqual(expect.any(String));
|
||||
expect(parsed.items).toEqual(expect.any(Array));
|
||||
checkDate(parsed.lastBuildDate);
|
||||
|
||||
// check items
|
||||
const guids = [];
|
||||
parsed.items.forEach((item) => {
|
||||
expect(item).toEqual(expect.any(Object));
|
||||
expect(item.title).toEqual(expect.any(String));
|
||||
expect(item.link).toEqual(expect.any(String));
|
||||
expect(item.content).toEqual(expect.any(String));
|
||||
expect(item.guid).toEqual(expect.any(String));
|
||||
if (item.pubDate) {
|
||||
expect(item.pubDate).toEqual(expect.any(String));
|
||||
checkDate(item.pubDate);
|
||||
}
|
||||
|
||||
// guid must be unique
|
||||
expect(guids).not.toContain(item.guid);
|
||||
guids.push(item.guid);
|
||||
});
|
||||
};
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
module.exports = (response) => {
|
||||
expect(response.status).toBe(200);
|
||||
};
|
||||
Loading…
Reference in New Issue