Skip to content

Commit

Permalink
feat: 增强 http-proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
huangapple committed Dec 8, 2022
1 parent 74aa8a7 commit 4f7810a
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 2 deletions.
6 changes: 6 additions & 0 deletions packages/http-proxy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}
}
```
2 changes: 2 additions & 0 deletions packages/http-proxy/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions packages/http-proxy/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export class HttpProxyMiddleware implements IMiddleware<any, any> {
const isStream = targetRes.on && targetRes.writable;

const reqOptions: any = {
...(this.httpProxy.extReqOptions || {}),
method,
url: url.href,
headers: reqHeaders,
Expand Down
21 changes: 20 additions & 1 deletion packages/http-proxy/test/express.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('test/express.test.ts', function () {
}
return body;
}, {'content-type': 'application/json'});

nock('https://midwayjs.org').get('/123').reply(302, '<html>456</html>', {'location': 'https://www.baidu.com/'});
const appDir = join(__dirname, 'fixtures/express');
app = await createApp(appDir);
})
Expand Down Expand Up @@ -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 === "<html>123</html>");
});
});
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 === "<html>456</html>");
});
});
});
21 changes: 20 additions & 1 deletion packages/http-proxy/test/faas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('test/faas.test.ts', function () {
}
return body;
}, {'content-type': 'application/json'});

nock('https://midwayjs.org').get('/123').reply(302, '<html>456</html>', {'location': 'https://www.baidu.com/'});
const appDir = join(__dirname, 'fixtures/faas');
app = await createFunctionApp<ServerlessApp.Framework>(appDir, {}, ServerlessApp);
})
Expand Down Expand Up @@ -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 === "<html>123</html>");
});
});
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 === "<html>456</html>");
});
});
});
17 changes: 17 additions & 0 deletions packages/http-proxy/test/fixtures/express/src/configuration.ts
Original file line number Diff line number Diff line change
@@ -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: [
Expand Down Expand Up @@ -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 })
}
}
}
},
Expand Down
17 changes: 17 additions & 0 deletions packages/http-proxy/test/fixtures/faas/src/configuration.ts
Original file line number Diff line number Diff line change
@@ -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: [
Expand Down Expand Up @@ -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 })
}
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions packages/http-proxy/test/fixtures/koa/src/configuration.ts
Original file line number Diff line number Diff line change
@@ -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: [
Expand Down Expand Up @@ -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 })
}
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions packages/http-proxy/test/fixtures/web/src/config/config.default.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as https from "https";

export const keys = 'test';
export const httpProxy = {
strategy: {
Expand All @@ -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 })
}
}
},
};
20 changes: 20 additions & 0 deletions packages/http-proxy/test/koa.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe('test/koa.test.ts', function () {
}
return body;
}, {'content-type': 'application/json'});
nock('https://midwayjs.org').get('/123').reply(302, '<html>456</html>', {'location': 'https://www.baidu.com/'});
const appDir = join(__dirname, 'fixtures/koa');
app = await createApp(appDir);
})
Expand Down Expand Up @@ -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 === "<html>123</html>");
});
});
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 === "<html>456</html>");
});
});
});
21 changes: 21 additions & 0 deletions packages/http-proxy/test/web.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe('test/web.test.ts', function () {
}
return body;
}, {'content-type': 'application/json'});
nock('https://midwayjs.org').get('/123').reply(302, '<html>456</html>', {'location': 'https://www.baidu.com/'});
const appDir = join(__dirname, 'fixtures/web');
app = await createApp(appDir);
})
Expand Down Expand Up @@ -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 === "<html>123</html>");
});
});
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 === "<html>456</html>");
});
});
});

0 comments on commit 4f7810a

Please sign in to comment.