forked from DIYgod/RSSHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.js
115 lines (101 loc) · 3.67 KB
/
router.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
process.env.SOCKET = 'socket';
const supertest = require('supertest');
jest.mock('request-promise-native');
const server = require('../lib/index');
const request = supertest(server);
const Parser = require('rss-parser');
const parser = new Parser();
const config = require('../lib/config').value;
afterAll(() => {
delete process.env.SOCKET;
});
async function checkRSS(response) {
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.ttl).toEqual(((config.cache.routeExpire / 60) | 0) + '');
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);
});
}
afterAll(() => {
server.close();
});
describe('router', () => {
// root
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');
});
// route
it(`/test/1`, async () => {
const response = await request.get('/test/1');
expect(response.status).toBe(200);
await checkRSS(response);
});
// robots.txt
it('/robots.txt', async () => {
config.disallowRobot = false;
const response404 = await request.get('/robots.txt');
expect(response404.status).toBe(404);
config.disallowRobot = true;
const response = await request.get('/robots.txt');
expect(response.status).toBe(200);
expect(response.text).toBe('User-agent: *\nDisallow: /');
expect(response.headers['content-type']).toBe('text/plain');
});
// api
it(`/api/routes/test`, async () => {
const response = await request.get('/api/routes/test');
expect(response.status).toBe(200);
expect(response.body).toEqual({
status: 0,
data: {
test: {
routes: ['/test/:id'],
},
},
message: 'request returned 1 route',
});
});
it(`/api/routes`, async () => {
const response = await request.get('/api/routes');
expect(response.status).toBe(200);
expect(response.body).toMatchObject({
status: 0,
data: {
test: {
routes: ['/test/:id'],
},
},
message: expect.stringMatching(/request returned (\d+) routes/),
});
});
});