Skip to content

Commit fc1e05b

Browse files
committed
feat: 增加koa,faas内核的http-proxy功能, 支持axios的全部参数
1 parent 74aa8a7 commit fc1e05b

File tree

11 files changed

+236
-56
lines changed

11 files changed

+236
-56
lines changed

packages/http-proxy/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,11 @@ export const httpProxy = {
3131
host: 'http://127.0.0.1',
3232
match: /\/assets\/(.*)/,
3333
target: 'http://127.0.0.1/$1',
34+
35+
//额外的axios请求config, 会照搬过去
36+
extReqOptions:{
37+
//用来设置不校验https的ssl
38+
httpsAgent: new https.Agent({ rejectUnauthorized: false })
39+
}
3440
}
3541
```

packages/http-proxy/src/interface.ts

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export interface HttpProxyStrategy {
66
ignoreHeaders?: {
77
[key: string]: boolean;
88
}
9+
// 额外的axios请求config, 详情见 https://axios-http.com/docs/req_config
10+
extReqOptions?: { [key: string]: any }
911
}
1012

1113
export interface HttpProxyConfig extends HttpProxyStrategy {

packages/http-proxy/src/middleware.ts

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export class HttpProxyMiddleware implements IMiddleware<any, any> {
5656
const isStream = targetRes.on && targetRes.writable;
5757

5858
const reqOptions: any = {
59+
...(proxy.extReqOptions || {}),
5960
method,
6061
url: url.href,
6162
headers: reqHeaders,

packages/http-proxy/test/express.test.ts

+59-31
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,50 @@ import * as nock from 'nock';
66
describe('test/express.test.ts', function () {
77
let app;
88
beforeAll(async () => {
9-
nock('https://gw.alicdn.com').get('/tfs/TB1.1EzoBBh1e4jSZFhXXcC9VXa-48-48.png?version=123').reply(200, '123', {'content-type': 'image/png'});
10-
nock('https://www.baidu.com').get('/').reply(200, '<html>123</html>', {'content-type': 'text/html'});
11-
nock('https://sm.bdimg.com').get('/static/wiseindex/amd_modules/@searchfe/assert_3ed54c3.js').reply(200, '123', {'content-type': 'application/x-javascript'});
9+
nock('https://gw.alicdn.com').get('/tfs/TB1.1EzoBBh1e4jSZFhXXcC9VXa-48-48.png?version=123').reply(200, '123', { 'content-type': 'image/png' });
10+
nock('https://www.baidu.com').get('/').reply(200, '<html>123</html>', { 'content-type': 'text/html' });
11+
//奇怪, 同一个链接只能用一次, 再用nock会报404
12+
nock('https://www.baidu.com').get('/1234').reply(200, '<html>1234</html>', { 'content-type': 'text/html' });
13+
nock('https://www.baidu.com').get('/5678').reply(200, '<html>5678</html>', { 'content-type': 'text/html' });
14+
nock('https://sm.bdimg.com').get('/static/wiseindex/amd_modules/@searchfe/assert_3ed54c3.js').reply(200, '123', { 'content-type': 'application/x-javascript' });
1215
nock('https://httpbin.org')
1316
.persist()
1417
.get('/get?name=midway').reply(200, {
15-
"args": {
16-
"name": "midway"
17-
},
18-
"headers": {
19-
"Host": "httpbin.org",
20-
},
21-
"url": "https://httpbin.org/get?name=midway"
22-
}, {'content-type': 'application/json'})
18+
"args": {
19+
"name": "midway"
20+
},
21+
"headers": {
22+
"Host": "httpbin.org",
23+
},
24+
"url": "https://httpbin.org/get?name=midway"
25+
}, { 'content-type': 'application/json' })
2326
.post('/post').reply(200, function (uri, requestBody) {
24-
const body = {
25-
'headers': {
26-
'Host': 'httpbin.org',
27-
'Content-Type': this.req.headers['content-type'],
28-
'url': 'https://httpbin.org/post'
29-
},
27+
const body = {
28+
'headers': {
29+
'Host': 'httpbin.org',
30+
'Content-Type': this.req.headers['content-type'],
3031
'url': 'https://httpbin.org/post'
31-
} as any;
32-
if (this.req.headers['content-type'] === 'application/x-www-form-urlencoded') {
33-
body.form = {
34-
"name": "midway"
35-
};
36-
}
37-
if (this.req.headers['content-type'] === 'application/json') {
38-
body.data = JSON.stringify(requestBody);
39-
}
40-
return body;
41-
}, {'content-type': 'application/json'});
42-
32+
},
33+
'url': 'https://httpbin.org/post'
34+
} as any;
35+
if (this.req.headers['content-type'] === 'application/x-www-form-urlencoded') {
36+
body.form = {
37+
"name": "midway"
38+
};
39+
}
40+
if (this.req.headers['content-type'] === 'application/json') {
41+
body.data = JSON.stringify(requestBody);
42+
}
43+
return body;
44+
}, { 'content-type': 'application/json' });
45+
nock('https://aliyun.com').get('/1234').reply(302, '<html>123</html>', {
46+
'content-type': 'text/html',
47+
Location: "https://www.baidu.com/1234"
48+
});
49+
nock('https://aliyun.com').get('/5678').reply(302, '<html>456</html>', {
50+
'content-type': 'text/html',
51+
Location: "https://www.baidu.com/5678"
52+
});
4353
const appDir = join(__dirname, 'fixtures/express');
4454
app = await createApp(appDir);
4555
})
@@ -98,14 +108,14 @@ describe('test/express.test.ts', function () {
98108
it('post json to httpbin', async () => {
99109
const request = await createHttpRequest(app);
100110
await request.post('/httpbin/post')
101-
.send({name: 'midway'})
111+
.send({ name: 'midway' })
102112
.set('Accept', 'application/json')
103113
.expect(200)
104114
.then(async response => {
105115
assert(response.status === 200)
106116
assert(response.body.url === 'https://httpbin.org/post');
107117
assert(response.body.headers['Content-Type'] === 'application/json');
108-
assert(response.body.data === JSON.stringify({ name: 'midway'}));
118+
assert(response.body.data === JSON.stringify({ name: 'midway' }));
109119
});
110120
});
111121

@@ -122,4 +132,22 @@ describe('test/express.test.ts', function () {
122132
assert(response.body.form.name === 'midway');
123133
});
124134
});
135+
it('canredirects', async () => {
136+
const request = await createHttpRequest(app);
137+
await request.get('/canredirects/1234')
138+
.expect(200).then(async response => {
139+
140+
assert(response.text === "<html>1234</html>")
141+
});
142+
});
143+
144+
// express不懂为啥不支持
145+
// it('noredirects', async () => {
146+
// const request = await createHttpRequest(app);
147+
// await request.get('/noredirects/5678')
148+
// .expect(302).then(async response => {
149+
// assert(response.text === "<html>456</html>")
150+
// });
151+
// });
152+
125153
});

packages/http-proxy/test/faas.test.ts

+33-7
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ describe('test/faas.test.ts', function () {
88
let app;
99
beforeAll(async () => {
1010
// nock('https://gw.alicdn.com').get('/tfs/TB1.1EzoBBh1e4jSZFhXXcC9VXa-48-48.png?version=123').reply(200, '123', {'content-type': 'image/png;charset=utf-8'});
11-
nock('https://www.baidu.com').get('/').reply(200, '<html>123</html>', {'content-type': 'text/html'});
12-
nock('https://sm.bdimg.com').get('/static/wiseindex/amd_modules/@searchfe/assert_3ed54c3.js').reply(200, '123', {'content-type': 'application/x-javascript'});
11+
nock('https://www.baidu.com').get('/').reply(200, '<html>123</html>', { 'content-type': 'text/html' });
12+
//奇怪, 同一个链接只能用一次, 再用nock会报404
13+
nock('https://www.baidu.com').get('/1234').reply(200, '<html>1234</html>', { 'content-type': 'text/html' });
14+
nock('https://www.baidu.com').get('/5678').reply(200, '<html>5678</html>', { 'content-type': 'text/html' });
15+
nock('https://sm.bdimg.com').get('/static/wiseindex/amd_modules/@searchfe/assert_3ed54c3.js').reply(200, '123', { 'content-type': 'application/x-javascript' });
1316
nock('https://httpbin.org')
1417
.persist()
1518
.get('/get?name=midway').reply(200, {
@@ -20,7 +23,7 @@ describe('test/faas.test.ts', function () {
2023
"Host": "httpbin.org",
2124
},
2225
"url": "https://httpbin.org/get?name=midway"
23-
}, {'content-type': 'application/json'})
26+
}, { 'content-type': 'application/json' })
2427
.post('/post').reply(200, function (uri, requestBody) {
2528
const body = {
2629
'headers': {
@@ -39,8 +42,15 @@ describe('test/faas.test.ts', function () {
3942
body.data = JSON.stringify(requestBody);
4043
}
4144
return body;
42-
}, {'content-type': 'application/json'});
43-
45+
}, { 'content-type': 'application/json' });
46+
nock('https://aliyun.com').get('/1234').reply(302, '<html>123</html>', {
47+
'content-type': 'text/html',
48+
Location: "https://www.baidu.com/1234"
49+
});
50+
nock('https://aliyun.com').get('/5678').reply(302, '<html>456</html>', {
51+
'content-type': 'text/html',
52+
Location: "https://www.baidu.com/5678"
53+
});
4454
const appDir = join(__dirname, 'fixtures/faas');
4555
app = await createFunctionApp<ServerlessApp.Framework>(appDir, {}, ServerlessApp);
4656
})
@@ -99,14 +109,14 @@ describe('test/faas.test.ts', function () {
99109
it('post json to httpbin', async () => {
100110
const request = await createHttpRequest(app);
101111
await request.post('/httpbin/post')
102-
.send({name: 'midway'})
112+
.send({ name: 'midway' })
103113
.set('Accept', 'application/json')
104114
.expect(200)
105115
.then(async response => {
106116
assert(response.status === 200)
107117
assert(response.body.url === 'https://httpbin.org/post');
108118
assert(response.body.headers['Content-Type'] === 'application/json');
109-
assert(response.body.data === JSON.stringify({ name: 'midway'}));
119+
assert(response.body.data === JSON.stringify({ name: 'midway' }));
110120
});
111121
});
112122

@@ -123,4 +133,20 @@ describe('test/faas.test.ts', function () {
123133
assert(response.body.form.name === 'midway');
124134
});
125135
});
136+
it('canredirects', async () => {
137+
const request = await createHttpRequest(app);
138+
await request.get('/canredirects/1234')
139+
.expect(200).then(async response => {
140+
141+
assert(response.text === "<html>1234</html>")
142+
});
143+
});
144+
it('noredirects', async () => {
145+
const request = await createHttpRequest(app);
146+
await request.get('/noredirects/5678')
147+
.expect(302).then(async response => {
148+
assert(response.text === "<html>456</html>")
149+
});
150+
});
151+
126152
});

packages/http-proxy/test/fixtures/express/src/configuration.ts

+14
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ import * as proxy from '../../../../src';
3131
d: {
3232
match: /.*?baidu.*$/,
3333
target: 'https://www.baidu.com/'
34+
},
35+
e: {
36+
match: /\/canredirects\//,
37+
target: "https://aliyun.com/"
38+
},
39+
f: {
40+
match: /\/noredirects\//,
41+
target: "https://aliyun.com/",
42+
//额外的axios请求config, 会照搬过去
43+
extReqOptions: {
44+
// `maxRedirects` defines the maximum number of redirects to follow in node.js.
45+
// If set to 0, no redirects will be followed.
46+
maxRedirects: 0
47+
}
3448
}
3549
}
3650
},

packages/http-proxy/test/fixtures/faas/src/configuration.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,35 @@ import * as proxy from '../../../../src';
3030
d: {
3131
match: /.*?baidu.*$/,
3232
target: 'https://www.baidu.com/'
33+
},
34+
e: {
35+
match: /\/canredirects\//,
36+
target: "https://aliyun.com/"
37+
},
38+
f: {
39+
match: /\/noredirects\//,
40+
target: "https://aliyun.com/",
41+
//额外的axios请求config, 会照搬过去
42+
extReqOptions: {
43+
// `maxRedirects` defines the maximum number of redirects to follow in node.js.
44+
// If set to 0, no redirects will be followed.
45+
maxRedirects: 0
46+
}
3347
}
3448
}
3549
}
3650
}
3751
}
3852
]
3953
})
40-
export class AutoConfiguration {}
54+
export class AutoConfiguration {
55+
}
4156

4257
@Provide()
4358
export class HelloHttpService {
44-
@ServerlessTrigger(ServerlessTriggerType.HTTP, { path: '/*', method: 'all'})
59+
@ServerlessTrigger(ServerlessTriggerType.HTTP, { path: '/*', method: 'all' })
4560
async get() {
46-
return 'hello'
61+
return 'hello'
4762
}
4863
}
4964

packages/http-proxy/test/fixtures/koa/src/configuration.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,26 @@ import * as proxy from '../../../../src';
3131
d: {
3232
match: /.*?baidu.*$/,
3333
target: 'https://www.baidu.com/'
34+
},
35+
e: {
36+
match: /\/canredirects\//,
37+
target: "https://aliyun.com/"
38+
},
39+
f: {
40+
match: /\/noredirects\//,
41+
target: "https://aliyun.com/",
42+
//额外的axios请求config, 会照搬过去
43+
extReqOptions: {
44+
// `maxRedirects` defines the maximum number of redirects to follow in node.js.
45+
// If set to 0, no redirects will be followed.
46+
maxRedirects: 0
47+
}
3448
}
3549
}
3650
}
3751
}
3852
}
3953
]
4054
})
41-
export class AutoConfiguration {}
55+
export class AutoConfiguration {
56+
}

packages/http-proxy/test/fixtures/web/src/config/config.default.ts

+14
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ export const httpProxy = {
1919
d: {
2020
match: /.*?baidu.*$/,
2121
target: 'https://www.baidu.com/'
22+
},
23+
e: {
24+
match: /\/canredirects\//,
25+
target: "https://aliyun.com/"
26+
},
27+
f: {
28+
match: /\/noredirects\//,
29+
target: "https://aliyun.com/",
30+
//额外的axios请求config, 会照搬过去
31+
extReqOptions: {
32+
// `maxRedirects` defines the maximum number of redirects to follow in node.js.
33+
// If set to 0, no redirects will be followed.
34+
maxRedirects: 0
35+
}
2236
}
2337
},
2438
};

0 commit comments

Comments
 (0)