mirror of
https://github.com/DIYgod/RSSHub.git
synced 2026-09-24 23:12:34 +08:00
* 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
389 lines
13 KiB
JavaScript
389 lines
13 KiB
JavaScript
const got = require('../../lib/utils/got');
|
|
const parser = require('../../lib/utils/rss-parser');
|
|
const nock = require('nock');
|
|
require('../../lib/utils/request-wrapper');
|
|
let check = () => {};
|
|
const simpleResponse = '<rss version="2.0"><channel><item></item></channel></rss>';
|
|
|
|
beforeEach(() => {
|
|
delete process.env.PAC_URI;
|
|
delete process.env.PAC_SCRIPT;
|
|
});
|
|
|
|
afterEach(() => {
|
|
delete process.env.PROXY_URI;
|
|
delete process.env.PROXY_PROTOCOL;
|
|
delete process.env.PROXY_HOST;
|
|
delete process.env.PROXY_PORT;
|
|
delete process.env.PROXY_AUTH;
|
|
delete process.env.PROXY_URL_REGEX;
|
|
|
|
nock.restore();
|
|
nock.activate();
|
|
check = () => {};
|
|
|
|
const http = require('http');
|
|
const httpWrap = (func) => {
|
|
const origin = func;
|
|
return function (url, request) {
|
|
if (typeof url === 'object') {
|
|
if (url instanceof URL) {
|
|
check(request);
|
|
} else {
|
|
check(url);
|
|
}
|
|
} else {
|
|
check(request);
|
|
}
|
|
return Reflect.apply(origin, this, arguments);
|
|
};
|
|
};
|
|
http.get = httpWrap(http.get);
|
|
http.request = httpWrap(http.request);
|
|
});
|
|
|
|
describe('got', () => {
|
|
it('headers', async () => {
|
|
nock(/rsshub\.test/)
|
|
.get(/.*/)
|
|
.times(3)
|
|
.reply(function () {
|
|
expect(this.req.headers.referer).toBe('http://api.rsshub.test');
|
|
expect(this.req.headers.host).toBe('api.rsshub.test');
|
|
return [200, simpleResponse];
|
|
});
|
|
|
|
await got.get('http://api.rsshub.test/test');
|
|
await got.get('http://api.rsshub.test');
|
|
|
|
await parser.parseURL('http://api.rsshub.test/test');
|
|
});
|
|
|
|
it('proxy-uri socks', async () => {
|
|
process.env.PROXY_URI = 'socks5://user:pass@rsshub.proxy:2333';
|
|
|
|
jest.resetModules();
|
|
require('../../lib/utils/request-wrapper');
|
|
check = (request) => {
|
|
expect(request.agent.constructor.name).toBe('SocksProxyAgent');
|
|
expect(request.agent.proxy.host).toBe('rsshub.proxy');
|
|
expect(request.agent.proxy.port).toBe(2333);
|
|
expect(request.agent.proxy.type).toBe(5);
|
|
};
|
|
|
|
nock(/rsshub\.test/)
|
|
.get('/proxy')
|
|
.times(2)
|
|
.reply(200, simpleResponse);
|
|
|
|
await got.get('http://rsshub.test/proxy');
|
|
await parser.parseURL('http://rsshub.test/proxy');
|
|
});
|
|
|
|
it('proxy-uri http', async () => {
|
|
process.env.PROXY_URI = 'http://user:pass@rsshub.proxy:2333';
|
|
|
|
jest.resetModules();
|
|
require('../../lib/utils/request-wrapper');
|
|
check = (request) => {
|
|
expect(request.agent.constructor.name).toBe('HttpsProxyAgent');
|
|
expect(request.agent.proxy.protocol).toBe('http:');
|
|
expect(request.agent.proxy.username).toBe('user');
|
|
expect(request.agent.proxy.password).toBe('pass');
|
|
expect(request.agent.proxy.host).toBe('rsshub.proxy:2333');
|
|
expect(request.agent.proxy.hostname).toBe('rsshub.proxy');
|
|
expect(request.agent.proxy.port).toBe('2333');
|
|
};
|
|
|
|
nock(/rsshub\.test/)
|
|
.get('/proxy')
|
|
.times(2)
|
|
.reply(200, simpleResponse);
|
|
|
|
await got.get('http://rsshub.test/proxy');
|
|
await parser.parseURL('http://rsshub.test/proxy');
|
|
});
|
|
|
|
it('proxy-uri https', async () => {
|
|
process.env.PROXY_URI = 'https://rsshub.proxy:2333';
|
|
|
|
jest.resetModules();
|
|
require('../../lib/utils/request-wrapper');
|
|
check = (request) => {
|
|
expect(request.agent.constructor.name).toBe('HttpsProxyAgent');
|
|
expect(request.agent.proxy.protocol).toBe('https:');
|
|
expect(request.agent.proxy.host).toBe('rsshub.proxy:2333');
|
|
expect(request.agent.proxy.hostname).toBe('rsshub.proxy');
|
|
expect(request.agent.proxy.port).toBe('2333');
|
|
};
|
|
|
|
nock(/rsshub\.test/)
|
|
.get('/proxy')
|
|
.times(2)
|
|
.reply(200, simpleResponse);
|
|
|
|
await got.get('http://rsshub.test/proxy');
|
|
await parser.parseURL('http://rsshub.test/proxy');
|
|
});
|
|
|
|
it('proxy socks', async () => {
|
|
process.env.PROXY_PROTOCOL = 'socks';
|
|
process.env.PROXY_HOST = 'rsshub.proxy';
|
|
process.env.PROXY_PORT = '2333';
|
|
|
|
jest.resetModules();
|
|
require('../../lib/utils/request-wrapper');
|
|
check = (request) => {
|
|
expect(request.agent.constructor.name).toBe('SocksProxyAgent');
|
|
expect(request.agent.proxy.host).toBe('rsshub.proxy');
|
|
expect(request.agent.proxy.port).toBe(2333);
|
|
expect(request.agent.proxy.type).toBe(5);
|
|
};
|
|
|
|
nock(/rsshub\.test/)
|
|
.get('/proxy')
|
|
.times(2)
|
|
.reply(200, simpleResponse);
|
|
|
|
await got.get('http://rsshub.test/proxy');
|
|
await parser.parseURL('http://rsshub.test/proxy');
|
|
});
|
|
|
|
it('proxy http', async () => {
|
|
process.env.PROXY_PROTOCOL = 'http';
|
|
process.env.PROXY_HOST = 'rsshub.proxy';
|
|
process.env.PROXY_PORT = '2333';
|
|
|
|
jest.resetModules();
|
|
require('../../lib/utils/request-wrapper');
|
|
check = (request) => {
|
|
expect(request.agent.constructor.name).toBe('HttpsProxyAgent');
|
|
expect(request.agent.proxy.protocol).toBe('http:');
|
|
expect(request.agent.proxy.host).toBe('rsshub.proxy:2333');
|
|
expect(request.agent.proxy.hostname).toBe('rsshub.proxy');
|
|
expect(request.agent.proxy.port).toBe('2333');
|
|
};
|
|
|
|
nock(/rsshub\.test/)
|
|
.get('/proxy')
|
|
.times(2)
|
|
.reply(200, simpleResponse);
|
|
|
|
await got.get('http://rsshub.test/proxy');
|
|
await parser.parseURL('http://rsshub.test/proxy');
|
|
});
|
|
|
|
it('proxy https', async () => {
|
|
process.env.PROXY_PROTOCOL = 'https';
|
|
process.env.PROXY_HOST = 'rsshub.proxy';
|
|
process.env.PROXY_PORT = '2333';
|
|
|
|
jest.resetModules();
|
|
require('../../lib/utils/request-wrapper');
|
|
check = (request) => {
|
|
expect(request.agent.constructor.name).toBe('HttpsProxyAgent');
|
|
expect(request.agent.proxy.protocol).toBe('https:');
|
|
expect(request.agent.proxy.host).toBe('rsshub.proxy:2333');
|
|
expect(request.agent.proxy.hostname).toBe('rsshub.proxy');
|
|
expect(request.agent.proxy.port).toBe('2333');
|
|
};
|
|
|
|
nock(/rsshub\.test/)
|
|
.get('/proxy')
|
|
.times(2)
|
|
.reply(200, simpleResponse);
|
|
|
|
await got.get('http://rsshub.test/proxy');
|
|
await parser.parseURL('http://rsshub.test/proxy');
|
|
});
|
|
|
|
it('proxy reverse proxy', async () => {
|
|
process.env.REVERSE_PROXY_URL = 'http://rsshub.test/?target=';
|
|
const url = 'http://rsshub.test/proxy';
|
|
|
|
jest.resetModules();
|
|
require('../../lib/utils/request-wrapper');
|
|
check = (request) => {
|
|
expect(request.url.toString()).toBe(`${process.env.REVERSE_PROXY_URL}${encodeURIComponent(url)}`);
|
|
};
|
|
|
|
nock(/rsshub\.test/)
|
|
.get(`/?target=${encodeURIComponent(url)}`)
|
|
.times(2)
|
|
.reply(200, simpleResponse);
|
|
nock(/rsshub\.test/)
|
|
.get('/proxy')
|
|
.times(2)
|
|
.reply(200, simpleResponse);
|
|
|
|
await got.get(url);
|
|
await parser.parseURL(url);
|
|
});
|
|
|
|
it('pac-uri http', async () => {
|
|
process.env.PAC_URI = 'http://rsshub.proxy:2333';
|
|
|
|
jest.resetModules();
|
|
require('../../lib/utils/request-wrapper');
|
|
|
|
check = (request) => {
|
|
expect(request.agent.constructor.name).toBe('PacProxyAgent');
|
|
expect(request.agent.uri.protocol).toBe('http:');
|
|
expect(request.agent.uri.host).toBe('rsshub.proxy:2333');
|
|
expect(request.agent.uri.hostname).toBe('rsshub.proxy');
|
|
expect(request.agent.uri.port).toBe('2333');
|
|
};
|
|
|
|
nock(/rsshub\.test/)
|
|
.get('/proxy')
|
|
.times(2)
|
|
.reply(200, simpleResponse);
|
|
|
|
await got.get('http://rsshub.test/proxy');
|
|
await parser.parseURL('http://rsshub.test/proxy');
|
|
});
|
|
|
|
it('pac-uri https', async () => {
|
|
process.env.PAC_URI = 'https://rsshub.proxy:2333';
|
|
|
|
jest.resetModules();
|
|
require('../../lib/utils/request-wrapper');
|
|
|
|
check = (request) => {
|
|
expect(request.agent.constructor.name).toBe('PacProxyAgent');
|
|
expect(request.agent.uri.protocol).toBe('https:');
|
|
expect(request.agent.uri.host).toBe('rsshub.proxy:2333');
|
|
expect(request.agent.uri.hostname).toBe('rsshub.proxy');
|
|
expect(request.agent.uri.port).toBe('2333');
|
|
};
|
|
|
|
nock(/rsshub\.test/)
|
|
.get('/proxy')
|
|
.times(2)
|
|
.reply(200, simpleResponse);
|
|
|
|
await got.get('http://rsshub.test/proxy');
|
|
await parser.parseURL('http://rsshub.test/proxy');
|
|
});
|
|
|
|
it('pac-uri ftp', async () => {
|
|
process.env.PAC_URI = 'ftp://rsshub.proxy:2333';
|
|
|
|
jest.resetModules();
|
|
require('../../lib/utils/request-wrapper');
|
|
|
|
check = (request) => {
|
|
expect(request.agent.constructor.name).toBe('PacProxyAgent');
|
|
expect(request.agent.uri.protocol).toBe('ftp:');
|
|
expect(request.agent.uri.host).toBe('rsshub.proxy:2333');
|
|
expect(request.agent.uri.hostname).toBe('rsshub.proxy');
|
|
expect(request.agent.uri.port).toBe('2333');
|
|
};
|
|
|
|
nock(/rsshub\.test/)
|
|
.get('/proxy')
|
|
.times(2)
|
|
.reply(200, simpleResponse);
|
|
|
|
await got.get('http://rsshub.test/proxy');
|
|
await parser.parseURL('http://rsshub.test/proxy');
|
|
});
|
|
|
|
it('pac-uri file', async () => {
|
|
process.env.PAC_URI = 'file:///D:/rsshub/proxy';
|
|
|
|
jest.resetModules();
|
|
require('../../lib/utils/request-wrapper');
|
|
|
|
check = (request) => {
|
|
expect(request.agent.constructor.name).toBe('PacProxyAgent');
|
|
expect(request.agent.uri.protocol).toBe('file:');
|
|
expect(request.agent.uri.pathname).toBe('/D:/rsshub/proxy');
|
|
};
|
|
|
|
nock(/rsshub\.test/)
|
|
.get('/proxy')
|
|
.times(2)
|
|
.reply(200, simpleResponse);
|
|
|
|
await got.get('http://rsshub.test/proxy');
|
|
await parser.parseURL('http://rsshub.test/proxy');
|
|
});
|
|
|
|
it('pac-script data', async () => {
|
|
process.env.PAC_SCRIPT = "function FindProxyForURL(url,host){return 'DIRECT';}";
|
|
|
|
jest.resetModules();
|
|
require('../../lib/utils/request-wrapper');
|
|
|
|
check = (request) => {
|
|
expect(request.agent.constructor.name).toBe('PacProxyAgent');
|
|
expect(request.agent.uri.protocol).toBe('data:');
|
|
expect(request.agent.uri.pathname).toBe("text/javascript;charset=utf-8,function%20FindProxyForURL(url%2Chost)%7Breturn%20'DIRECT'%3B%7D");
|
|
};
|
|
|
|
nock(/rsshub\.test/)
|
|
.get('/proxy')
|
|
.times(2)
|
|
.reply(200, simpleResponse);
|
|
|
|
await got.get('http://rsshub.test/proxy');
|
|
await parser.parseURL('http://rsshub.test/proxy');
|
|
});
|
|
|
|
it('auth', async () => {
|
|
process.env.PROXY_AUTH = 'testtest';
|
|
process.env.PROXY_PROTOCOL = 'http'; // only http(s) proxies extract auth from Headers
|
|
process.env.PROXY_HOST = 'rsshub.proxy';
|
|
process.env.PROXY_PORT = '2333';
|
|
|
|
jest.resetModules();
|
|
require('../../lib/utils/request-wrapper');
|
|
|
|
nock(/rsshub\.test/)
|
|
.get('/auth')
|
|
.times(2)
|
|
.reply(function () {
|
|
expect(this.req.headers['proxy-authorization']).toBe('Basic testtest');
|
|
return [200, simpleResponse];
|
|
});
|
|
|
|
await got.get('http://rsshub.test/auth');
|
|
await parser.parseURL('http://rsshub.test/auth');
|
|
});
|
|
|
|
it('url_regex', async () => {
|
|
process.env.PROXY_URL_REGEX = 'url_regex';
|
|
process.env.PROXY_PROTOCOL = 'socks';
|
|
process.env.PROXY_HOST = 'rsshub.proxy';
|
|
process.env.PROXY_PORT = '2333';
|
|
|
|
jest.resetModules();
|
|
require('../../lib/utils/request-wrapper');
|
|
check = (request) => {
|
|
if (request.path === '/url_regex') {
|
|
expect(request.agent.constructor.name).toBe('SocksProxyAgent');
|
|
expect(request.agent.proxy.host).toBe('rsshub.proxy');
|
|
expect(request.agent.proxy.port).toBe(2333);
|
|
} else if (request.path === '/proxy') {
|
|
expect(request.agent).toBe(undefined);
|
|
}
|
|
};
|
|
|
|
nock(/rsshub\.test/)
|
|
.get('/url_regex')
|
|
.times(2)
|
|
.reply(() => [200, simpleResponse]);
|
|
nock(/rsshub\.test/)
|
|
.get('/proxy')
|
|
.times(2)
|
|
.reply(() => [200, simpleResponse]);
|
|
|
|
await got.get('http://rsshub.test/url_regex');
|
|
await parser.parseURL('http://rsshub.test/url_regex');
|
|
|
|
await got.get('http://rsshub.test/proxy');
|
|
await parser.parseURL('http://rsshub.test/proxy');
|
|
});
|
|
});
|