Files
RSSHub/test/middleware/template.js
T
Tony 6d901cb7dc style(eslint): add eslint-unicorn (#14257)
* style: add eslint-unicorn

* style: fix unicorn/no-useless-spread

* style: fix unicorn/no-useless-promise-resolve-reject

* style: fix unicorn/no-for-loop

* fix: codeql bad HTML filtering regexp

* fix: codeql incomplete replace

* fix: unicorn/no-abusive-eslint-disable

* style: fix unicorn/no-new-array

* style: fix unicorn/no-typeof-undefined

* style: fix unicorn/no-zero-fractions

* style: fix unicorn/no-empty-file

* style: fix unicorn/prefer-date-now

* revert: auto fix unicorn/prefer-switch on lib/v2/kuaidi100/utils.js

* style: fix unicorn/prefer-array-find

* style: fix unicorn/prefer-array-flat

* style: fix unicorn/prefer-array-flat-map

* style: fix unicorn/prefer-at

* style: fix unicorn/prefer-string-starts-ends-with

* style: fix unicorn/prefer-includes

* fix: codeql URL substring sanitization

* style: fix unicorn/prefer-optional-catch-binding

* style: fix unicorn/catch-error-name

* style: fix unicorn/escape-case

* style: fix unicorn/prefer-native-coercion-functions

* style: fix unicorn/prefer-regexp-test

* style: fix unicorn/require-array-join-separator

* style: fix unicorn/prefer-math-trunc

* style: fix unicorn/prefer-negative-index

* style: fix unicorn/prefer-dom-node-dataset

* style: fix unicorn/prefer-dom-node-text-content

* style: fix unicorn/prefer-query-selector

* style: fix unicorn/no-array-for-each

* style: fix unicorn/no-negated-condition

* style: fix unicorn/prefer-add-event-listener

* style: fix unicorn/import-style

* style: fix prefer-regex-literals

* style: disable unicorn/no-useless-switch-case

* style: disable unicorn/text-encoding-identifier-case

* style: fix unicorn/prefer-set-has

* style: fix unicorn/prefer-spread

* revert: auto fix on lib/routes/universities/ynnu/edu/base64.js

* style: fix unicorn/no-useless-undefined

* style: fix unicorn/no-array-push-push

* style: fix unicorn/no-useless-undefined again

* style: fix unicorn/no-lonely-if

* style: fix unicorn/prefer-reflect-apply

* style: fix unicorn/switch-case-braces

* style: fix unicorn/prefer-switch

* style: fix unicorn/prefer-array-some

* fix: deepscan UNUSED_VAR_ASSIGN

* style: fix unicorn/prefer-ternary

* fix: follow-up of unicorn/prefer-ternary

* revert: auto fix of unicorn/prefer-string-slice for substring()

* style: disable unicorn/prefer-string-slice

fix: auto fix slice over deprecated substr

* style: fix unicorn/throw-new-error

* style: fix unicorn/filename-case

* test: fix dateParser renaming

* style: fix unicorn/better-regex

* style: fix unicorn/prefer-string-replace-all

* fix(deps): add sanitize-html

* style: fix no-prototype-builtins

* style: fix unicorn/consistent-destructuring

* style: fix unicorn/consistent-function-scoping

* style: fix unicorn/prefer-regexp-test

* style: fix unicorn/prefer-logical-operator-over-ternary

* style: fix unicorn/no-array-callback-reference

* style: add prefer-object-has-own

* style: warn unicorn/no-empty-file

* style: fix unicorn/prefer-number-properties

* style: fix no-useless-undefined again

* style: fix unicorn/numeric-separators-style

* style: disable unicorn/no-array-callback-reference

false postive with cheerio
2024-01-18 20:43:40 +08:00

117 lines
5.4 KiB
JavaScript

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();
afterAll(() => {
server.close();
});
describe('template', () => {
const expectPubDate = new Date(1_546_272_000_000 - 10 * 1000);
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).toBe(expectPubDate.toUTCString());
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);
delete parsed1.lastBuildDate;
delete parsed2.lastBuildDate;
delete parsed1.feedUrl;
delete parsed2.feedUrl;
delete parsed1.paginationLinks;
delete parsed2.paginationLinks;
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).toBe(expectPubDate.toISOString());
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 jsonResponse = await request.get('/test/1.json');
const rssResponse = await request.get('/test/1.rss');
const jsonParsed = JSON.parse(jsonResponse.text);
const rssParsed = await parser.parseString(rssResponse.text);
expect(jsonResponse.headers['content-type']).toBe('application/feed+json; charset=UTF-8');
expect(jsonParsed.items[0].title).toEqual(rssParsed.items[0].title);
expect(jsonParsed.items[0].url).toEqual(rssParsed.items[0].link);
expect(jsonParsed.items[0].id).toEqual(rssParsed.items[0].guid);
expect(jsonParsed.items[0].date_published).toEqual(expectPubDate.toISOString());
expect(jsonParsed.items[0].content_html).toEqual(rssParsed.items[0].content);
expect(jsonParsed.items[0].authors[0].name).toEqual(rssParsed.items[0].author);
expect(jsonParsed.items.every((item) => item.authors.every((author) => author.name.includes(' ')))).toBe(false);
});
it('.debug.html', async () => {
const jsonResponse = await request.get('/test/1.json');
const jsonParsed = JSON.parse(jsonResponse.text);
const debugHTMLResponse0 = await request.get('/test/1.0.debug.html');
expect(debugHTMLResponse0.headers['content-type']).toBe('text/html; charset=UTF-8');
expect(debugHTMLResponse0.text).toBe(jsonParsed.items[0].content_html);
const debugHTMLResponseNotExist = await request.get(`/test/1.${jsonParsed.items.length}.debug.html`);
expect(debugHTMLResponseNotExist.text).toBe(`ctx.state.data.item[${jsonParsed.items.length}] not found`);
});
it('flatten author object', async () => {
const response = await request.get('/test/json');
const parsed = await parser.parseString(response.text);
expect(parsed.items[2].author).toBe(['DIYgod1', 'DIYgod2'].map((name) => name).join(', '));
expect(parsed.items[3].author).toBe(['DIYgod3', 'DIYgod4', 'DIYgod5'].map((name) => name).join(', '));
expect(parsed.items[4].author).toBeUndefined();
});
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(153);
});
it(`enclosure`, async () => {
const response = await request.get('/test/enclosure');
const parsed = await parser.parseString(response.text);
expect(parsed.itunes.author).toBe('DIYgod');
expect(parsed.items[0].enclosure.url).toBe('https://github.com/DIYgod/RSSHub/issues/1');
expect(parsed.items[0].enclosure.length).toBe('3661');
expect(parsed.items[0].itunes.duration).toBe('10:10:10');
});
});