mirror of
https://github.com/DIYgod/RSSHub.git
synced 2026-09-24 23:12:34 +08:00
test
This commit is contained in:
+12
-2
@@ -25,11 +25,14 @@ if (config.proxy && config.proxy.protocol && typeof config.proxy.protocol === 's
|
||||
}
|
||||
|
||||
const custom = got.extend({
|
||||
retry: config.requestRetry,
|
||||
retry: {
|
||||
retries: config.requestRetry,
|
||||
statusCodes: [408, 413, 429, 500, 502, 503, 504, 404] // add 404 to default for unit test
|
||||
},
|
||||
hooks: {
|
||||
beforeRetry: [
|
||||
(options, err, count) => {
|
||||
logger.error(`Request ${options.requestUrl} fail, retry attempt #${count}: ${err}`);
|
||||
logger.error(`Request ${err.url} fail, retry attempt #${count}: ${err}`);
|
||||
},
|
||||
],
|
||||
afterResponse: [
|
||||
@@ -43,9 +46,16 @@ const custom = got.extend({
|
||||
} else {
|
||||
response.data = response.body;
|
||||
}
|
||||
response.status = response.statusCode;
|
||||
return response;
|
||||
},
|
||||
],
|
||||
onError: [
|
||||
error => {
|
||||
error.response.status = error.response.statusCode;
|
||||
return error;
|
||||
}
|
||||
],
|
||||
},
|
||||
agent: agent,
|
||||
headers: {
|
||||
|
||||
@@ -71,6 +71,7 @@
|
||||
"luxon": "^1.7.1",
|
||||
"lz-string": "^1.4.4",
|
||||
"markdown-it": "^8.4.2",
|
||||
"nock": "^10.0.6",
|
||||
"node-fetch": "^2.3.0",
|
||||
"path-to-regexp": "3.0.0",
|
||||
"pidusage": "^2.0.17",
|
||||
|
||||
+35
-37
@@ -1,22 +1,22 @@
|
||||
const axios = require('../../lib/utils/axios');
|
||||
const MockAdapter = require('axios-mock-adapter');
|
||||
const mock = new MockAdapter(axios);
|
||||
const config = require('../../lib/config');
|
||||
|
||||
const nock = require('nock');
|
||||
describe('axios', () => {
|
||||
it('axios headers', async () => {
|
||||
mock.onGet('/test').reply((axiosConfig) => {
|
||||
expect(axiosConfig.headers['User-Agent']).toBe(config.ua);
|
||||
expect(axiosConfig.headers['X-APP']).toBe('RSSHub');
|
||||
return [
|
||||
200,
|
||||
{
|
||||
code: 0,
|
||||
},
|
||||
];
|
||||
});
|
||||
nock('http://fortest.com')
|
||||
.get('/test')
|
||||
.reply(function() {
|
||||
expect(this.req.headers['user-agent']).toBe(config.ua);
|
||||
expect(this.req.headers['x-app']).toBe('RSSHub');
|
||||
return [
|
||||
200,
|
||||
{
|
||||
code: 0,
|
||||
},
|
||||
];
|
||||
});
|
||||
|
||||
const response = await axios.get('/test');
|
||||
const response = await axios.get('http://fortest.com/test');
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.data.code).toBe(0);
|
||||
});
|
||||
@@ -24,34 +24,32 @@ describe('axios', () => {
|
||||
it('axios retry', async () => {
|
||||
const requestRun = jest.fn();
|
||||
let requestTime;
|
||||
|
||||
mock.onGet('/test').reply(() => {
|
||||
requestRun();
|
||||
|
||||
// retryDelay
|
||||
const now = new Date();
|
||||
if (requestTime) {
|
||||
expect(now - requestTime).toBeGreaterThanOrEqual(100);
|
||||
expect(now - requestTime).toBeLessThan(120);
|
||||
}
|
||||
requestTime = new Date();
|
||||
|
||||
return [
|
||||
404,
|
||||
{
|
||||
code: 1,
|
||||
},
|
||||
];
|
||||
});
|
||||
nock('http://fortest.com')
|
||||
.get('/testRerty')
|
||||
.times(config.requestRetry + 1)
|
||||
.reply(function() {
|
||||
requestRun();
|
||||
const now = new Date();
|
||||
if (requestTime) {
|
||||
expect(now - requestTime).toBeGreaterThanOrEqual(100);
|
||||
expect(now - requestTime).toBeLessThan(120);
|
||||
}
|
||||
requestTime = new Date();
|
||||
return [
|
||||
404,
|
||||
{
|
||||
code: 1,
|
||||
},
|
||||
];
|
||||
});
|
||||
|
||||
try {
|
||||
await axios.get('/test');
|
||||
await axios.get('http://fortest.com/testRerty');
|
||||
} catch (error) {
|
||||
expect(error.response.status).toBe(404);
|
||||
expect(error.response.data.code).toBe(1);
|
||||
expect(error.name).toBe('RequestError');
|
||||
}
|
||||
|
||||
// retries
|
||||
expect(requestRun).toHaveBeenCalledTimes(config.requestRetry + 1);
|
||||
expect(requestRun).toHaveBeenCalledTimes(config.requestRetry);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user