From a17c89abafe2b3097fc0b33e7c1715433d155b41 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Thu, 13 Jun 2019 19:32:42 +0800 Subject: [PATCH] test: proxy --- test/utils/got.js | 82 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 75 insertions(+), 7 deletions(-) diff --git a/test/utils/got.js b/test/utils/got.js index 592a36cb85..14b2ee907f 100644 --- a/test/utils/got.js +++ b/test/utils/got.js @@ -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); + }, + ], + }, + }); + }); });