From 0811bbb3a2346c9a93a4a7715f98fafb91e2f90f Mon Sep 17 00:00:00 2001 From: huangapple Date: Thu, 8 Dec 2022 15:54:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8D=95=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/http-proxy/src/interface.ts | 2 +- packages/http-proxy/test/express.test.ts | 21 ++++++++++++++++++- packages/http-proxy/test/faas.test.ts | 19 +++++++++++++++++ .../fixtures/express/src/configuration.ts | 12 +++++++++++ .../test/fixtures/faas/src/configuration.ts | 12 +++++++++++ .../test/fixtures/koa/src/configuration.ts | 12 +++++++++++ .../fixtures/web/src/config/config.default.ts | 12 +++++++++++ packages/http-proxy/test/koa.test.ts | 19 +++++++++++++++++ packages/http-proxy/test/web.test.ts | 20 ++++++++++++++++++ 9 files changed, 127 insertions(+), 2 deletions(-) diff --git a/packages/http-proxy/src/interface.ts b/packages/http-proxy/src/interface.ts index 40681747b152..aaba95d9a60d 100644 --- a/packages/http-proxy/src/interface.ts +++ b/packages/http-proxy/src/interface.ts @@ -6,7 +6,7 @@ export interface HttpProxyStrategy { ignoreHeaders?: { [key: string]: boolean; } - //额外的axios请求config + // 额外的axios请求config, 详情见 https://axios-http.com/docs/req_config extReqOptions?: { [key: string]: any } } diff --git a/packages/http-proxy/test/express.test.ts b/packages/http-proxy/test/express.test.ts index efae93168a0b..644d88ccf1e6 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://www.302.com').get('/').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.post('/can302') + .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.post('/no302') + .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..19d4d73dd99d 100644 --- a/packages/http-proxy/test/faas.test.ts +++ b/packages/http-proxy/test/faas.test.ts @@ -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.post('/can302') + .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.post('/no302') + .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..216b1101cdc5 100644 --- a/packages/http-proxy/test/fixtures/express/src/configuration.ts +++ b/packages/http-proxy/test/fixtures/express/src/configuration.ts @@ -31,6 +31,18 @@ import * as proxy from '../../../../src'; d: { match: /.*?baidu.*$/, target: 'https://www.baidu.com/' + }, + e: { + match: /.*?no302.*$/, + target: 'https://www.no302.com/', + extReqOptions: { + //表示不重定义 + maxRedirects: 0, + } + }, + f: { + match: /.*?can302.*$/, + target: 'https://www.302.com/' } } }, diff --git a/packages/http-proxy/test/fixtures/faas/src/configuration.ts b/packages/http-proxy/test/fixtures/faas/src/configuration.ts index 225e9b333d18..2301a9c8ba5b 100644 --- a/packages/http-proxy/test/fixtures/faas/src/configuration.ts +++ b/packages/http-proxy/test/fixtures/faas/src/configuration.ts @@ -30,6 +30,18 @@ import * as proxy from '../../../../src'; d: { match: /.*?baidu.*$/, target: 'https://www.baidu.com/' + }, + e: { + match: /.*?no302.*$/, + target: 'https://www.no302.com/', + extReqOptions: { + //表示不重定义 + maxRedirects: 0, + } + }, + f: { + match: /.*?can302.*$/, + target: 'https://www.302.com/' } } } diff --git a/packages/http-proxy/test/fixtures/koa/src/configuration.ts b/packages/http-proxy/test/fixtures/koa/src/configuration.ts index 1ded1e15abb7..1bbb87d85e5c 100644 --- a/packages/http-proxy/test/fixtures/koa/src/configuration.ts +++ b/packages/http-proxy/test/fixtures/koa/src/configuration.ts @@ -31,6 +31,18 @@ import * as proxy from '../../../../src'; d: { match: /.*?baidu.*$/, target: 'https://www.baidu.com/' + }, + e: { + match: /.*?no302.*$/, + target: 'https://www.no302.com/', + extReqOptions: { + //表示不重定义 + maxRedirects: 0, + } + }, + f: { + match: /.*?can302.*$/, + target: 'https://www.302.com/' } } } 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..15defe5f6cba 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 @@ -19,6 +19,18 @@ export const httpProxy = { d: { match: /.*?baidu.*$/, target: 'https://www.baidu.com/' + }, + e: { + match: /.*?no302.*$/, + target: 'https://www.no302.com/', + extReqOptions: { + //表示不重定义 + maxRedirects: 0, + } + }, + f: { + match: /.*?can302.*$/, + target: 'https://www.302.com/' } }, }; diff --git a/packages/http-proxy/test/koa.test.ts b/packages/http-proxy/test/koa.test.ts index bbba635c80b2..4b136585a28b 100644 --- a/packages/http-proxy/test/koa.test.ts +++ b/packages/http-proxy/test/koa.test.ts @@ -122,4 +122,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.post('/can302') + .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.post('/no302') + .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..60ca4b7cc8a7 100644 --- a/packages/http-proxy/test/web.test.ts +++ b/packages/http-proxy/test/web.test.ts @@ -121,4 +121,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.post('/can302') + .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.post('/no302') + .expect(302) + .then(async response => { + assert(response.status === 302) + assert(response.body === "456"); + }); + }); });