From e12a67310f3692ae79306fbefb76dcd8f3bed51a Mon Sep 17 00:00:00 2001 From: DIYgod Date: Thu, 17 Jan 2019 16:00:56 +0800 Subject: [PATCH] test: test route for response test --- lib/router.js | 22 +++++++++++++ package.json | 2 +- test/cases.js | 4 --- test/index.js | 1 - test/response.js | 73 ++++++++++++++++++++++++++++++++++++-------- test/rules/index.js | 7 ----- test/rules/rss.js | 41 ------------------------- test/rules/status.js | 3 -- 8 files changed, 83 insertions(+), 70 deletions(-) delete mode 100644 test/cases.js delete mode 100644 test/index.js delete mode 100644 test/rules/index.js delete mode 100644 test/rules/rss.js delete mode 100644 test/rules/status.js diff --git a/lib/router.js b/lib/router.js index 562f6df1a..283a1c389 100644 --- a/lib/router.js +++ b/lib/router.js @@ -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')); diff --git a/package.json b/package.json index 4bef78057..4999604c9 100644 --- a/package.json +++ b/package.json @@ -82,7 +82,7 @@ }, "jest": { "testMatch": [ - "**/test/index.js" + "**/test/*.js" ], "coverageReporters": [ "text-summary", diff --git a/test/cases.js b/test/cases.js deleted file mode 100644 index 069985d95..000000000 --- a/test/cases.js +++ /dev/null @@ -1,4 +0,0 @@ -module.exports = { - text: ['/'], - rss: ['/bilibili/ranking/0/3'], -}; diff --git a/test/index.js b/test/index.js deleted file mode 100644 index 8b2dc0c91..000000000 --- a/test/index.js +++ /dev/null @@ -1 +0,0 @@ -require('./response'); diff --git a/test/response.js b/test/response.js index e6b6de88f..645218de7 100644 --- a/test/response.js +++ b/test/response.js @@ -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); }); }); diff --git a/test/rules/index.js b/test/rules/index.js deleted file mode 100644 index 91d6ddb2f..000000000 --- a/test/rules/index.js +++ /dev/null @@ -1,7 +0,0 @@ -const status = require('./status'); -const rss = require('./rss'); - -module.exports = async (response) => { - status(response); - await rss(response); -}; diff --git a/test/rules/rss.js b/test/rules/rss.js deleted file mode 100644 index 517f06378..000000000 --- a/test/rules/rss.js +++ /dev/null @@ -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); - }); -}; diff --git a/test/rules/status.js b/test/rules/status.js deleted file mode 100644 index 53029d4e3..000000000 --- a/test/rules/status.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = (response) => { - expect(response.status).toBe(200); -};