Skip to content

Commit

Permalink
feat: 增加koa,faas内核的http-proxy功能, 支持axios的全部参数
Browse files Browse the repository at this point in the history
  • Loading branch information
huangapple committed Dec 8, 2022
1 parent 74aa8a7 commit fc1e05b
Show file tree
Hide file tree
Showing 11 changed files with 236 additions and 56 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 = {
...(proxy.extReqOptions || {}),
method,
url: url.href,
headers: reqHeaders,
Expand Down
90 changes: 59 additions & 31 deletions packages/http-proxy/test/express.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,50 @@ import * as nock from 'nock';
describe('test/express.test.ts', function () {
let app;
beforeAll(async () => {
nock('https://gw.alicdn.com').get('/tfs/TB1.1EzoBBh1e4jSZFhXXcC9VXa-48-48.png?version=123').reply(200, '123', {'content-type': 'image/png'});
nock('https://www.baidu.com').get('/').reply(200, '<html>123</html>', {'content-type': 'text/html'});
nock('https://sm.bdimg.com').get('/static/wiseindex/amd_modules/@searchfe/assert_3ed54c3.js').reply(200, '123', {'content-type': 'application/x-javascript'});
nock('https://gw.alicdn.com').get('/tfs/TB1.1EzoBBh1e4jSZFhXXcC9VXa-48-48.png?version=123').reply(200, '123', { 'content-type': 'image/png' });
nock('https://www.baidu.com').get('/').reply(200, '<html>123</html>', { 'content-type': 'text/html' });
//奇怪, 同一个链接只能用一次, 再用nock会报404
nock('https://www.baidu.com').get('/1234').reply(200, '<html>1234</html>', { 'content-type': 'text/html' });
nock('https://www.baidu.com').get('/5678').reply(200, '<html>5678</html>', { 'content-type': 'text/html' });
nock('https://sm.bdimg.com').get('/static/wiseindex/amd_modules/@searchfe/assert_3ed54c3.js').reply(200, '123', { 'content-type': 'application/x-javascript' });
nock('https://httpbin.org')
.persist()
.get('/get?name=midway').reply(200, {
"args": {
"name": "midway"
},
"headers": {
"Host": "httpbin.org",
},
"url": "https://httpbin.org/get?name=midway"
}, {'content-type': 'application/json'})
"args": {
"name": "midway"
},
"headers": {
"Host": "httpbin.org",
},
"url": "https://httpbin.org/get?name=midway"
}, { 'content-type': 'application/json' })
.post('/post').reply(200, function (uri, requestBody) {
const body = {
'headers': {
'Host': 'httpbin.org',
'Content-Type': this.req.headers['content-type'],
'url': 'https://httpbin.org/post'
},
const body = {
'headers': {
'Host': 'httpbin.org',
'Content-Type': this.req.headers['content-type'],
'url': 'https://httpbin.org/post'
} as any;
if (this.req.headers['content-type'] === 'application/x-www-form-urlencoded') {
body.form = {
"name": "midway"
};
}
if (this.req.headers['content-type'] === 'application/json') {
body.data = JSON.stringify(requestBody);
}
return body;
}, {'content-type': 'application/json'});

},
'url': 'https://httpbin.org/post'
} as any;
if (this.req.headers['content-type'] === 'application/x-www-form-urlencoded') {
body.form = {
"name": "midway"
};
}
if (this.req.headers['content-type'] === 'application/json') {
body.data = JSON.stringify(requestBody);
}
return body;
}, { 'content-type': 'application/json' });
nock('https://aliyun.com').get('/1234').reply(302, '<html>123</html>', {
'content-type': 'text/html',
Location: "https://www.baidu.com/1234"
});
nock('https://aliyun.com').get('/5678').reply(302, '<html>456</html>', {
'content-type': 'text/html',
Location: "https://www.baidu.com/5678"
});
const appDir = join(__dirname, 'fixtures/express');
app = await createApp(appDir);
})
Expand Down Expand Up @@ -98,14 +108,14 @@ describe('test/express.test.ts', function () {
it('post json to httpbin', async () => {
const request = await createHttpRequest(app);
await request.post('/httpbin/post')
.send({name: 'midway'})
.send({ name: 'midway' })
.set('Accept', 'application/json')
.expect(200)
.then(async response => {
assert(response.status === 200)
assert(response.body.url === 'https://httpbin.org/post');
assert(response.body.headers['Content-Type'] === 'application/json');
assert(response.body.data === JSON.stringify({ name: 'midway'}));
assert(response.body.data === JSON.stringify({ name: 'midway' }));
});
});

Expand All @@ -122,4 +132,22 @@ describe('test/express.test.ts', function () {
assert(response.body.form.name === 'midway');
});
});
it('canredirects', async () => {
const request = await createHttpRequest(app);
await request.get('/canredirects/1234')
.expect(200).then(async response => {

assert(response.text === "<html>1234</html>")
});
});

// express不懂为啥不支持
// it('noredirects', async () => {
// const request = await createHttpRequest(app);
// await request.get('/noredirects/5678')
// .expect(302).then(async response => {
// assert(response.text === "<html>456</html>")
// });
// });

});
40 changes: 33 additions & 7 deletions packages/http-proxy/test/faas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ describe('test/faas.test.ts', function () {
let app;
beforeAll(async () => {
// nock('https://gw.alicdn.com').get('/tfs/TB1.1EzoBBh1e4jSZFhXXcC9VXa-48-48.png?version=123').reply(200, '123', {'content-type': 'image/png;charset=utf-8'});
nock('https://www.baidu.com').get('/').reply(200, '<html>123</html>', {'content-type': 'text/html'});
nock('https://sm.bdimg.com').get('/static/wiseindex/amd_modules/@searchfe/assert_3ed54c3.js').reply(200, '123', {'content-type': 'application/x-javascript'});
nock('https://www.baidu.com').get('/').reply(200, '<html>123</html>', { 'content-type': 'text/html' });
//奇怪, 同一个链接只能用一次, 再用nock会报404
nock('https://www.baidu.com').get('/1234').reply(200, '<html>1234</html>', { 'content-type': 'text/html' });
nock('https://www.baidu.com').get('/5678').reply(200, '<html>5678</html>', { 'content-type': 'text/html' });
nock('https://sm.bdimg.com').get('/static/wiseindex/amd_modules/@searchfe/assert_3ed54c3.js').reply(200, '123', { 'content-type': 'application/x-javascript' });
nock('https://httpbin.org')
.persist()
.get('/get?name=midway').reply(200, {
Expand All @@ -20,7 +23,7 @@ describe('test/faas.test.ts', function () {
"Host": "httpbin.org",
},
"url": "https://httpbin.org/get?name=midway"
}, {'content-type': 'application/json'})
}, { 'content-type': 'application/json' })
.post('/post').reply(200, function (uri, requestBody) {
const body = {
'headers': {
Expand All @@ -39,8 +42,15 @@ describe('test/faas.test.ts', function () {
body.data = JSON.stringify(requestBody);
}
return body;
}, {'content-type': 'application/json'});

}, { 'content-type': 'application/json' });
nock('https://aliyun.com').get('/1234').reply(302, '<html>123</html>', {
'content-type': 'text/html',
Location: "https://www.baidu.com/1234"
});
nock('https://aliyun.com').get('/5678').reply(302, '<html>456</html>', {
'content-type': 'text/html',
Location: "https://www.baidu.com/5678"
});
const appDir = join(__dirname, 'fixtures/faas');
app = await createFunctionApp<ServerlessApp.Framework>(appDir, {}, ServerlessApp);
})
Expand Down Expand Up @@ -99,14 +109,14 @@ describe('test/faas.test.ts', function () {
it('post json to httpbin', async () => {
const request = await createHttpRequest(app);
await request.post('/httpbin/post')
.send({name: 'midway'})
.send({ name: 'midway' })
.set('Accept', 'application/json')
.expect(200)
.then(async response => {
assert(response.status === 200)
assert(response.body.url === 'https://httpbin.org/post');
assert(response.body.headers['Content-Type'] === 'application/json');
assert(response.body.data === JSON.stringify({ name: 'midway'}));
assert(response.body.data === JSON.stringify({ name: 'midway' }));
});
});

Expand All @@ -123,4 +133,20 @@ describe('test/faas.test.ts', function () {
assert(response.body.form.name === 'midway');
});
});
it('canredirects', async () => {
const request = await createHttpRequest(app);
await request.get('/canredirects/1234')
.expect(200).then(async response => {

assert(response.text === "<html>1234</html>")
});
});
it('noredirects', async () => {
const request = await createHttpRequest(app);
await request.get('/noredirects/5678')
.expect(302).then(async response => {
assert(response.text === "<html>456</html>")
});
});

});
14 changes: 14 additions & 0 deletions packages/http-proxy/test/fixtures/express/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ import * as proxy from '../../../../src';
d: {
match: /.*?baidu.*$/,
target: 'https://www.baidu.com/'
},
e: {
match: /\/canredirects\//,
target: "https://aliyun.com/"
},
f: {
match: /\/noredirects\//,
target: "https://aliyun.com/",
//额外的axios请求config, 会照搬过去
extReqOptions: {
// `maxRedirects` defines the maximum number of redirects to follow in node.js.
// If set to 0, no redirects will be followed.
maxRedirects: 0
}
}
}
},
Expand Down
21 changes: 18 additions & 3 deletions packages/http-proxy/test/fixtures/faas/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,35 @@ import * as proxy from '../../../../src';
d: {
match: /.*?baidu.*$/,
target: 'https://www.baidu.com/'
},
e: {
match: /\/canredirects\//,
target: "https://aliyun.com/"
},
f: {
match: /\/noredirects\//,
target: "https://aliyun.com/",
//额外的axios请求config, 会照搬过去
extReqOptions: {
// `maxRedirects` defines the maximum number of redirects to follow in node.js.
// If set to 0, no redirects will be followed.
maxRedirects: 0
}
}
}
}
}
}
]
})
export class AutoConfiguration {}
export class AutoConfiguration {
}

@Provide()
export class HelloHttpService {
@ServerlessTrigger(ServerlessTriggerType.HTTP, { path: '/*', method: 'all'})
@ServerlessTrigger(ServerlessTriggerType.HTTP, { path: '/*', method: 'all' })
async get() {
return 'hello'
return 'hello'
}
}

17 changes: 16 additions & 1 deletion packages/http-proxy/test/fixtures/koa/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,26 @@ import * as proxy from '../../../../src';
d: {
match: /.*?baidu.*$/,
target: 'https://www.baidu.com/'
},
e: {
match: /\/canredirects\//,
target: "https://aliyun.com/"
},
f: {
match: /\/noredirects\//,
target: "https://aliyun.com/",
//额外的axios请求config, 会照搬过去
extReqOptions: {
// `maxRedirects` defines the maximum number of redirects to follow in node.js.
// If set to 0, no redirects will be followed.
maxRedirects: 0
}
}
}
}
}
}
]
})
export class AutoConfiguration {}
export class AutoConfiguration {
}
14 changes: 14 additions & 0 deletions packages/http-proxy/test/fixtures/web/src/config/config.default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ export const httpProxy = {
d: {
match: /.*?baidu.*$/,
target: 'https://www.baidu.com/'
},
e: {
match: /\/canredirects\//,
target: "https://aliyun.com/"
},
f: {
match: /\/noredirects\//,
target: "https://aliyun.com/",
//额外的axios请求config, 会照搬过去
extReqOptions: {
// `maxRedirects` defines the maximum number of redirects to follow in node.js.
// If set to 0, no redirects will be followed.
maxRedirects: 0
}
}
},
};
Loading

0 comments on commit fc1e05b

Please sign in to comment.