test: got axios options

This commit is contained in:
DIYgod
2019-06-14 17:51:03 +08:00
parent 6dfaecaf58
commit 56e74742fc
3 changed files with 38 additions and 27 deletions

View File

@@ -69,21 +69,12 @@ const custom = got.extend({
return response; return response;
}, },
], ],
onError: [
(error) => {
error.response.status = error.response.statusCode;
return error;
},
],
init: [ init: [
(options) => { (options) => {
// compatible with axios api // compatible with axios api
if (options.data) { if (options.data) {
options.body = options.body || options.data; options.body = options.body || options.data;
} }
if (options.responseType === 'buffer') {
options.encoding = null;
}
if (options.params) { if (options.params) {
options.query = options.query || queryString.stringify(options.params); options.query = options.query || queryString.stringify(options.params);
options.searchParams = options.query; // for Got v11 after options.searchParams = options.query; // for Got v11 after

View File

@@ -121,9 +121,9 @@ describe('empty', () => {
describe('allow_empty', () => { describe('allow_empty', () => {
it(`allow_empty`, async () => { it(`allow_empty`, async () => {
const response1 = await request.get('/test/allow_empty'); const response = await request.get('/test/allow_empty');
expect(response1.status).toBe(200); expect(response.status).toBe(200);
const parsed = await parser.parseString(response1.text); const parsed = await parser.parseString(response.text);
expect(parsed.items.length).toBe(0); expect(parsed.items.length).toBe(0);
}); });
}); });

View File

@@ -17,17 +17,10 @@ describe('got', () => {
.reply(function() { .reply(function() {
expect(this.req.headers['user-agent']).toBe(config.ua); expect(this.req.headers['user-agent']).toBe(config.ua);
expect(this.req.headers['x-app']).toBe('RSSHub'); expect(this.req.headers['x-app']).toBe('RSSHub');
return [ return [200, ''];
200,
{
code: 0,
},
];
}); });
const response = await got.get('http://rsshub.test/test'); await got.get('http://rsshub.test/test');
expect(response.status).toBe(200);
expect(response.data.code).toBe(0);
}); });
it('retry', async () => { it('retry', async () => {
@@ -44,12 +37,7 @@ describe('got', () => {
expect(now - requestTime).toBeLessThan(120); expect(now - requestTime).toBeLessThan(120);
} }
requestTime = new Date(); requestTime = new Date();
return [ return [404, '0'];
404,
{
code: 1,
},
];
}); });
try { try {
@@ -62,6 +50,38 @@ describe('got', () => {
expect(requestRun).toHaveBeenCalledTimes(config.requestRetry); expect(requestRun).toHaveBeenCalledTimes(config.requestRetry);
}); });
it('axios', async () => {
nock('http://rsshub.test')
.post('/post')
.reply(function() {
return [200, '{"code": 0}'];
});
const response1 = await got.post('http://rsshub.test/post', {
form: true,
data: {
test: 1,
},
});
expect(response1.statusCode).toBe(200);
expect(response1.status).toBe(200);
expect(response1.body).toBe('{"code": 0}');
expect(response1.data.code).toBe(0);
nock('http://rsshub.test')
.get(/^\/params/)
.reply(function() {
expect(this.req.path).toBe('/params?test=1');
return [200, ''];
});
await got.get('http://rsshub.test/params', {
params: {
test: 1,
},
});
});
it('proxy socks', async () => { it('proxy socks', async () => {
process.env.PROXY_PROTOCOL = 'socks'; process.env.PROXY_PROTOCOL = 'socks';
process.env.PROXY_HOST = 'rsshub.proxy'; process.env.PROXY_HOST = 'rsshub.proxy';