Skip to content

Commit

Permalink
添加单测试
Browse files Browse the repository at this point in the history
  • Loading branch information
huangapple committed Dec 8, 2022
1 parent 3208c7e commit 0811bbb
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/http-proxy/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}

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://www.302.com').get('/').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.post('/can302')
.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.post('/no302')
.expect(302)
.then(async response => {
assert(response.status === 302)
assert(response.body === "<html>456</html>");
});
});
});
19 changes: 19 additions & 0 deletions packages/http-proxy/test/faas.test.ts
Original file line number Diff line number Diff line change
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.post('/can302')
.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.post('/no302')
.expect(302)
.then(async response => {
assert(response.status === 302)
assert(response.body === "<html>456</html>");
});
});
});
12 changes: 12 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,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/'
}
}
},
Expand Down
12 changes: 12 additions & 0 deletions packages/http-proxy/test/fixtures/faas/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/'
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions packages/http-proxy/test/fixtures/koa/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/'
}
}
}
Expand Down
12 changes: 12 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,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/'
}
},
};
19 changes: 19 additions & 0 deletions packages/http-proxy/test/koa.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 === "<html>123</html>");
});
});
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 === "<html>456</html>");
});
});
});
20 changes: 20 additions & 0 deletions packages/http-proxy/test/web.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 === "<html>123</html>");
});
});
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 === "<html>456</html>");
});
});
});

0 comments on commit 0811bbb

Please sign in to comment.