test: proxy

This commit is contained in:
DIYgod
2019-06-13 19:32:42 +08:00
parent 749ede3af8
commit a17c89abaf

View File

@@ -1,9 +1,9 @@
const got = require('../../lib/utils/got');
let got = require('../../lib/utils/got');
const config = require('../../lib/config');
const nock = require('nock');
describe('got', () => {
it('got headers', async () => {
nock('http://fortest.com')
it('headers', async () => {
nock('http://rsshub.test')
.get('/test')
.reply(function() {
expect(this.req.headers['user-agent']).toBe(config.ua);
@@ -16,15 +16,15 @@ describe('got', () => {
];
});
const response = await got.get('http://fortest.com/test');
const response = await got.get('http://rsshub.test/test');
expect(response.status).toBe(200);
expect(response.data.code).toBe(0);
});
it('got retry', async () => {
it('retry', async () => {
const requestRun = jest.fn();
let requestTime;
nock('http://fortest.com')
nock('http://rsshub.test')
.get('/testRerty')
.times(config.requestRetry + 1)
.reply(function() {
@@ -44,7 +44,7 @@ describe('got', () => {
});
try {
await got.get('http://fortest.com/testRerty');
await got.get('http://rsshub.test/testRerty');
} catch (error) {
expect(error.name).toBe('RequestError');
}
@@ -52,4 +52,72 @@ describe('got', () => {
// retries
expect(requestRun).toHaveBeenCalledTimes(config.requestRetry);
});
it('proxy socks', async () => {
process.env.PROXY_PROTOCOL = 'socks';
process.env.PROXY_HOST = 'rsshub.proxy';
process.env.PROXY_PORT = '2333';
jest.resetModules();
got = require('../../lib/utils/got');
nock('http://rsshub.test')
.get('/proxy')
.reply(() => [200, '']);
await got.get('http://rsshub.test/proxy', {
hooks: {
beforeRequest: [
(options) => {
expect(options.agent.constructor.name).toBe('SocksProxyAgent');
expect(options.agent.options.href).toBe('socks://rsshub.proxy:2333');
},
],
},
});
});
it('proxy http', async () => {
process.env.PROXY_PROTOCOL = 'http';
process.env.PROXY_HOST = 'rsshub.proxy';
process.env.PROXY_PORT = '2333';
jest.resetModules();
got = require('../../lib/utils/got');
nock('http://rsshub.test')
.get('/proxy')
.reply(() => [200, '']);
await got.get('http://rsshub.test/proxy', {
hooks: {
beforeRequest: [
(options) => {
expect(options.agent.constructor.name).toBe('TunnelingAgent');
expect(options.agent.options.proxy.host).toBe('rsshub.proxy');
expect(options.agent.options.proxy.port).toBe(2333);
},
],
},
});
});
it('proxy https', async () => {
process.env.PROXY_PROTOCOL = 'https';
process.env.PROXY_HOST = 'rsshub.proxy';
process.env.PROXY_PORT = '2333';
jest.resetModules();
got = require('../../lib/utils/got');
nock('http://rsshub.test')
.get('/proxy')
.reply(() => [200, '']);
await got.get('http://rsshub.test/proxy', {
hooks: {
beforeRequest: [
(options) => {
expect(options.agent.constructor.name).toBe('TunnelingAgent');
expect(options.agent.options.proxy.host).toBe('rsshub.proxy');
expect(options.agent.options.proxy.port).toBe(2333);
},
],
},
});
});
});