diff --git a/packages/http-proxy/README.md b/packages/http-proxy/README.md index 80cb01b9edb0..8fe973b44bf6 100644 --- a/packages/http-proxy/README.md +++ b/packages/http-proxy/README.md @@ -31,5 +31,11 @@ export const httpProxy = { host: 'http://127.0.0.1', match: /\/assets\/(.*)/, target: 'http://127.0.0.1/$1', + + //额外的axios请求config, 会照搬过去 + extReqOptions:{ + //用来设置不校验https的ssl + httpsAgent: new https.Agent({ rejectUnauthorized: false }) + } } ``` diff --git a/packages/http-proxy/src/interface.ts b/packages/http-proxy/src/interface.ts index 9733185ec2db..aaba95d9a60d 100644 --- a/packages/http-proxy/src/interface.ts +++ b/packages/http-proxy/src/interface.ts @@ -6,6 +6,8 @@ export interface HttpProxyStrategy { ignoreHeaders?: { [key: string]: boolean; } + // 额外的axios请求config, 详情见 https://axios-http.com/docs/req_config + extReqOptions?: { [key: string]: any } } export interface HttpProxyConfig extends HttpProxyStrategy { diff --git a/packages/http-proxy/src/middleware.ts b/packages/http-proxy/src/middleware.ts index 1af6d23543f7..57f5be35e2d8 100644 --- a/packages/http-proxy/src/middleware.ts +++ b/packages/http-proxy/src/middleware.ts @@ -56,6 +56,7 @@ export class HttpProxyMiddleware implements IMiddleware { const isStream = targetRes.on && targetRes.writable; const reqOptions: any = { + ...(this.httpProxy.extReqOptions || {}), method, url: url.href, headers: reqHeaders, diff --git a/packages/http-proxy/test/express.test.ts b/packages/http-proxy/test/express.test.ts index efae93168a0b..c3190e14053e 100644 --- a/packages/http-proxy/test/express.test.ts +++ b/packages/http-proxy/test/express.test.ts @@ -39,7 +39,7 @@ describe('test/express.test.ts', function () { } return body; }, {'content-type': 'application/json'}); - + nock('https://midwayjs.org').get('/123').reply(302, '456', {'location': 'https://www.baidu.com/'}); const appDir = join(__dirname, 'fixtures/express'); app = await createApp(appDir); }) @@ -122,4 +122,23 @@ describe('test/express.test.ts', function () { assert(response.body.form.name === 'midway'); }); }); + it('get can redirect host', async () => { + const request = await createHttpRequest(app); + await request.get('/can302/123') + .expect(200) + .then(async response => { + assert(response.status === 200) + assert(response.body.url === 'https://www.baidu.com'); + assert(response.body === "123"); + }); + }); + it('get no redirect host', async () => { + const request = await createHttpRequest(app); + await request.get('/no302/123') + .expect(302) + .then(async response => { + assert(response.status === 302) + assert(response.body === "456"); + }); + }); }); diff --git a/packages/http-proxy/test/faas.test.ts b/packages/http-proxy/test/faas.test.ts index c2c30d9a0db5..225b3c00bf07 100644 --- a/packages/http-proxy/test/faas.test.ts +++ b/packages/http-proxy/test/faas.test.ts @@ -40,7 +40,7 @@ describe('test/faas.test.ts', function () { } return body; }, {'content-type': 'application/json'}); - + nock('https://midwayjs.org').get('/123').reply(302, '456', {'location': 'https://www.baidu.com/'}); const appDir = join(__dirname, 'fixtures/faas'); app = await createFunctionApp(appDir, {}, ServerlessApp); }) @@ -123,4 +123,23 @@ describe('test/faas.test.ts', function () { assert(response.body.form.name === 'midway'); }); }); + it('get can redirect host', async () => { + const request = await createHttpRequest(app); + await request.get('/can302/123') + .expect(200) + .then(async response => { + assert(response.status === 200) + assert(response.body.url === 'https://www.baidu.com'); + assert(response.body === "123"); + }); + }); + it('get no redirect host', async () => { + const request = await createHttpRequest(app); + await request.get('/no302/123') + .expect(302) + .then(async response => { + assert(response.status === 302) + assert(response.body === "456"); + }); + }); }); diff --git a/packages/http-proxy/test/fixtures/express/src/configuration.ts b/packages/http-proxy/test/fixtures/express/src/configuration.ts index 65fa78d0d6ba..88ab1bfbd7d2 100644 --- a/packages/http-proxy/test/fixtures/express/src/configuration.ts +++ b/packages/http-proxy/test/fixtures/express/src/configuration.ts @@ -1,6 +1,7 @@ import { All, Configuration, Controller, Inject } from '@midwayjs/core'; import * as express from '@midwayjs/express'; import * as proxy from '../../../../src'; +import * as https from "https"; @Configuration({ imports: [ @@ -31,6 +32,22 @@ import * as proxy from '../../../../src'; d: { match: /.*?baidu.*$/, target: 'https://www.baidu.com/' + }, + e: { + match: /\/no302\//, + target: 'https://midwayjs.org/', + extReqOptions: { + //表示不重定向 + maxRedirects: 0, + httpsAgent: new https.Agent({ rejectUnauthorized: false }) + } + }, + f: { + match: /\/can302\//, + target: 'https://midwayjs.org/', + extReqOptions: { + httpsAgent: new https.Agent({ rejectUnauthorized: false }) + } } } }, diff --git a/packages/http-proxy/test/fixtures/faas/src/configuration.ts b/packages/http-proxy/test/fixtures/faas/src/configuration.ts index 225e9b333d18..862779ce2e65 100644 --- a/packages/http-proxy/test/fixtures/faas/src/configuration.ts +++ b/packages/http-proxy/test/fixtures/faas/src/configuration.ts @@ -1,6 +1,7 @@ import { Configuration, Provide, ServerlessTrigger, ServerlessTriggerType } from '@midwayjs/core'; import * as faas from '@midwayjs/faas'; import * as proxy from '../../../../src'; +import * as https from "https"; @Configuration({ imports: [ @@ -30,6 +31,22 @@ import * as proxy from '../../../../src'; d: { match: /.*?baidu.*$/, target: 'https://www.baidu.com/' + }, + e: { + match: /\/no302\//, + target: 'https://midwayjs.org/', + extReqOptions: { + //表示不重定向 + maxRedirects: 0, + httpsAgent: new https.Agent({ rejectUnauthorized: false }) + } + }, + f: { + match: /\/can302\//, + target: 'https://midwayjs.org/', + extReqOptions: { + httpsAgent: new https.Agent({ rejectUnauthorized: false }) + } } } } diff --git a/packages/http-proxy/test/fixtures/koa/src/configuration.ts b/packages/http-proxy/test/fixtures/koa/src/configuration.ts index 1ded1e15abb7..0fceb21c057c 100644 --- a/packages/http-proxy/test/fixtures/koa/src/configuration.ts +++ b/packages/http-proxy/test/fixtures/koa/src/configuration.ts @@ -1,6 +1,7 @@ import { Configuration } from '@midwayjs/core'; import * as koa from '@midwayjs/koa'; import * as proxy from '../../../../src'; +import * as https from "https"; @Configuration({ imports: [ @@ -31,6 +32,22 @@ import * as proxy from '../../../../src'; d: { match: /.*?baidu.*$/, target: 'https://www.baidu.com/' + }, + e: { + match: /\/no302\//, + target: 'https://midwayjs.org/', + extReqOptions: { + //表示不重定向 + maxRedirects: 0, + httpsAgent: new https.Agent({ rejectUnauthorized: false }) + } + }, + f: { + match: /\/can302\//, + target: 'https://midwayjs.org/', + extReqOptions: { + httpsAgent: new https.Agent({ rejectUnauthorized: false }) + } } } } diff --git a/packages/http-proxy/test/fixtures/web/src/config/config.default.ts b/packages/http-proxy/test/fixtures/web/src/config/config.default.ts index dc7dd93dcbe2..30c4659fbf77 100644 --- a/packages/http-proxy/test/fixtures/web/src/config/config.default.ts +++ b/packages/http-proxy/test/fixtures/web/src/config/config.default.ts @@ -1,3 +1,5 @@ +import * as https from "https"; + export const keys = 'test'; export const httpProxy = { strategy: { @@ -19,6 +21,22 @@ export const httpProxy = { d: { match: /.*?baidu.*$/, target: 'https://www.baidu.com/' + }, + e: { + match: /\/no302\//, + target: 'https://midwayjs.org/', + extReqOptions: { + //表示不重定向 + maxRedirects: 0, + httpsAgent: new https.Agent({ rejectUnauthorized: false }) + } + }, + f: { + match: /\/can302\//, + target: 'https://midwayjs.org/', + extReqOptions: { + httpsAgent: new https.Agent({ rejectUnauthorized: false }) + } } }, }; diff --git a/packages/http-proxy/test/koa.test.ts b/packages/http-proxy/test/koa.test.ts index bbba635c80b2..74a11da54ad9 100644 --- a/packages/http-proxy/test/koa.test.ts +++ b/packages/http-proxy/test/koa.test.ts @@ -39,6 +39,7 @@ describe('test/koa.test.ts', function () { } return body; }, {'content-type': 'application/json'}); + nock('https://midwayjs.org').get('/123').reply(302, '456', {'location': 'https://www.baidu.com/'}); const appDir = join(__dirname, 'fixtures/koa'); app = await createApp(appDir); }) @@ -122,4 +123,23 @@ describe('test/koa.test.ts', function () { assert(response.body.form.name === 'midway'); }); }); + it('get can redirect host', async () => { + const request = await createHttpRequest(app); + await request.get('/can302/123') + .expect(200) + .then(async response => { + assert(response.status === 200) + assert(response.body.url === 'https://www.baidu.com'); + assert(response.body === "123"); + }); + }); + it('get no redirect host', async () => { + const request = await createHttpRequest(app); + await request.get('/no302/123') + .expect(302) + .then(async response => { + assert(response.status === 302) + assert(response.body === "456"); + }); + }); }); diff --git a/packages/http-proxy/test/web.test.ts b/packages/http-proxy/test/web.test.ts index 747674f2e007..6b36e132e60c 100644 --- a/packages/http-proxy/test/web.test.ts +++ b/packages/http-proxy/test/web.test.ts @@ -38,6 +38,7 @@ describe('test/web.test.ts', function () { } return body; }, {'content-type': 'application/json'}); + nock('https://midwayjs.org').get('/123').reply(302, '456', {'location': 'https://www.baidu.com/'}); const appDir = join(__dirname, 'fixtures/web'); app = await createApp(appDir); }) @@ -121,4 +122,24 @@ describe('test/web.test.ts', function () { assert(response.body.form.name === 'midway'); }); }); + + it('get can redirect host', async () => { + const request = await createHttpRequest(app); + await request.get('/can302/123') + .expect(200) + .then(async response => { + assert(response.status === 200) + assert(response.body.url === 'https://www.baidu.com'); + assert(response.body === "123"); + }); + }); + it('get no redirect host', async () => { + const request = await createHttpRequest(app); + await request.get('/no302/123') + .expect(302) + .then(async response => { + assert(response.status === 302) + assert(response.body === "456"); + }); + }); });