test: middleware/template

This commit is contained in:
DIYgod
2019-01-31 11:45:00 +08:00
parent 5daa65673b
commit 0a62389eaa
6 changed files with 90 additions and 13 deletions

View File

@@ -91,10 +91,19 @@ router.get('/test/:id', (ctx) => {
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: `Item${i}`,
description: `Description${i}`,
pubDate: new Date(`2018-4-${i}`).toUTCString(),
link: `https://github.com/DIYgod/RSSHub/issues/${i}`,
author: `DIYgod${i}`,

View File

@@ -17,7 +17,7 @@
</author>
{{if contributor }}
{{ each contributor }}
{{ each contributor }}
<contributor>
<name><![CDATA[{{@ $value }}]]></name>
</contributor>
@@ -37,13 +37,13 @@
<id>{{ $e.id || $e.link }}</id>
<title><![CDATA[{{@ $e.title }}]]></title>
<published>{{ $e.published || $e.updated }}</published>
<published>{{ $e.pubDate || $e.updated }}</published>
<updated>{{ $e.updated }}</updated>
{{ if $e.author }}
<author>
<name><![CDATA[{{@ $e.author || 'RSSHub' }}]]></name>
</author>
</author>
{{ /if }}
<link href="{{ $e.link }}" />
@@ -65,7 +65,7 @@
<category term="{{ $c }}"></category>
{{ /each }}
{{ /if }}
</entry>
{{ /each }}

View File

@@ -10,7 +10,7 @@ afterAll(() => {
describe('filter', () => {
it(`filter`, async () => {
const response = await request.get('/test/1?filter=Item4|Title5');
const response = await request.get('/test/1?filter=Description4|Title5');
const parsed = await parser.parseString(response.text);
expect(parsed.items.length).toBe(2);
expect(parsed.items[0].title).toBe('Title4');
@@ -18,14 +18,14 @@ describe('filter', () => {
});
it(`filter_title`, async () => {
const response = await request.get('/test/1?filter_title=Item4|Title5');
const response = await request.get('/test/1?filter_title=Description4|Title5');
const parsed = await parser.parseString(response.text);
expect(parsed.items.length).toBe(1);
expect(parsed.items[0].title).toBe('Title5');
});
it(`filter_description`, async () => {
const response = await request.get('/test/1?filter_description=Item4|Title5');
const response = await request.get('/test/1?filter_description=Description4|Title5');
const parsed = await parser.parseString(response.text);
expect(parsed.items.length).toBe(1);
expect(parsed.items[0].title).toBe('Title4');
@@ -40,7 +40,7 @@ describe('filter', () => {
});
it(`filterout`, async () => {
const response = await request.get('/test/1?filterout=Item4|Title5');
const response = await request.get('/test/1?filterout=Description4|Title5');
const parsed = await parser.parseString(response.text);
expect(parsed.items.length).toBe(3);
expect(parsed.items[0].title).toBe('Title1');
@@ -49,7 +49,7 @@ describe('filter', () => {
});
it(`filterout_title`, async () => {
const response = await request.get('/test/1?filterout_title=Item4|Title5');
const response = await request.get('/test/1?filterout_title=Description4|Title5');
const parsed = await parser.parseString(response.text);
expect(parsed.items.length).toBe(4);
expect(parsed.items[0].title).toBe('Title1');
@@ -59,7 +59,7 @@ describe('filter', () => {
});
it(`filterout_description`, async () => {
const response = await request.get('/test/1?filterout_description=Item4|Title5');
const response = await request.get('/test/1?filterout_description=Description4|Title5');
const parsed = await parser.parseString(response.text);
expect(parsed.items.length).toBe(4);
expect(parsed.items[0].title).toBe('Title1');

View File

@@ -0,0 +1,67 @@
const supertest = require('supertest');
const { server } = require('../../lib/index');
const request = supertest(server);
const Parser = require('rss-parser');
const parser = new Parser();
afterAll(() => {
server.close();
});
describe('template', () => {
it(`.rss`, async () => {
const response1 = await request.get('/test/1.rss');
const parsed1 = await parser.parseString(response1.text);
expect(parsed1).toEqual(expect.any(Object));
expect(parsed1.title).toEqual(expect.any(String));
expect(parsed1.description).toEqual(expect.any(String));
expect(parsed1.link).toEqual(expect.any(String));
expect(parsed1.lastBuildDate).toEqual(expect.any(String));
expect(parsed1.ttl).toEqual(expect.any(String));
expect(parsed1.items).toEqual(expect.any(Array));
expect(parsed1.items[0]).toEqual(expect.any(Object));
expect(parsed1.items[0].title).toEqual(expect.any(String));
expect(parsed1.items[0].link).toEqual(expect.any(String));
expect(parsed1.items[0].pubDate).toEqual(expect.any(String));
expect(parsed1.items[0].author).toEqual(expect.any(String));
expect(parsed1.items[0].content).toEqual(expect.any(String));
expect(parsed1.items[0].guid).toEqual(expect.any(String));
const response2 = await request.get('/test/1');
const parsed2 = await parser.parseString(response2.text);
expect(parsed2).toMatchObject(parsed1);
});
it(`.atom`, async () => {
const response = await request.get('/test/1.atom');
const parsed = await parser.parseString(response.text);
expect(parsed).toEqual(expect.any(Object));
expect(parsed.title).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));
expect(parsed.items[0]).toEqual(expect.any(Object));
expect(parsed.items[0].title).toEqual(expect.any(String));
expect(parsed.items[0].link).toEqual(expect.any(String));
expect(parsed.items[0].pubDate).toEqual(expect.any(String));
expect(parsed.items[0].author).toEqual(expect.any(String));
expect(parsed.items[0].content).toEqual(expect.any(String));
expect(parsed.items[0].id).toEqual(expect.any(String));
});
it(`.json`, async () => {
const response = await request.get('/test/1.json');
expect(response.status).toBe(404);
expect(response.text).toMatch(/RSSHub 发生了一些意外: <pre>Error: <b>JSON output had been removed/);
});
it(`long title`, async () => {
const response = await request.get('/test/long');
const parsed = await parser.parseString(response.text);
expect(parsed.items[0].title.length).toBe(103);
});
});

View File

@@ -24,6 +24,7 @@ async function checkRSS(response) {
expect(parsed.description).toEqual(expect.any(String));
expect(parsed.link).toEqual(expect.any(String));
expect(parsed.lastBuildDate).toEqual(expect.any(String));
expect(parsed.ttl).toEqual(config.cacheExpire);
expect(parsed.items).toEqual(expect.any(Array));
checkDate(parsed.lastBuildDate);

View File

@@ -13,5 +13,5 @@ describe('puppeteer', () => {
expect(html.length).toBeGreaterThan(0);
await browser.close();
});
}, 10000);
});