fix(core): RequestInProgressError cache age (#9850)

* fix(core): RequestInProgressError cache age

Signed-off-by: Rongrong <i@rong.moe>

* fix: ci test

Signed-off-by: Rongrong <i@rong.moe>
This commit is contained in:
Rongrong 2022-05-30 17:30:41 +08:00 committed by GitHub
parent 13ec6be32b
commit e1ee28267e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 2 deletions

View File

@ -62,6 +62,7 @@ module.exports = async (ctx, next) => {
if (err instanceof RequestInProgressError) {
ctx.status = 503;
message = err.message;
ctx.set('Cache-Control', `public, max-age=${config.cache.requestTimeout}`);
} else if (ctx.status === 403) {
message = err.message;
} else {

View File

@ -1,6 +1,8 @@
const config = require('@/config').value;
let cacheIndex = 0;
const got = require('@/utils/got');
const wait = require('@/utils/wait');
let cacheIndex = 0;
module.exports = async (ctx) => {
if (ctx.params.id === 'error') {
@ -166,6 +168,10 @@ module.exports = async (ctx) => {
];
}
if (ctx.params.id === 'slow') {
await wait(1000);
}
if (ctx.query.mode === 'fulltext') {
item = [
{

View File

@ -1,4 +1,5 @@
process.env.NODE_NAME = 'mock';
process.env.ALLOW_ORIGIN = 'rsshub.mock';
const supertest = require('supertest');
jest.mock('request-promise-native');
@ -11,10 +12,15 @@ afterAll(() => {
server.close();
});
afterAll(() => {
delete process.env.NODE_NAME;
delete process.env.ALLOW_ORIGIN;
});
describe('header', () => {
it(`header`, async () => {
const response = await request.get('/test/1');
expect(response.headers['access-control-allow-origin']).toBe('127.0.0.1:1200');
expect(response.headers['access-control-allow-origin']).toBe('rsshub.mock');
expect(response.headers['access-control-allow-methods']).toBe('GET');
expect(response.headers['content-type']).toBe('application/xml; charset=utf-8');
expect(response.headers['cache-control']).toBe(`public, max-age=${config.cache.routeExpire}`);

View File

@ -4,6 +4,7 @@ process.env.SENTRY_ROUTE_TIMEOUT = '0';
const supertest = require('supertest');
jest.mock('request-promise-native');
const server = require('../../lib/index');
const config = require('../../lib/config').value;
const request = supertest(server);
afterAll(() => {
@ -32,3 +33,12 @@ describe('httperror', () => {
);
});
});
describe('RequestInProgressError', () => {
it(`RequestInProgressError`, async () => {
const responses = await Promise.all([request.get('/test/slow'), request.get('/test/slow')]);
expect(new Set(responses.map((r) => r.status))).toEqual(new Set([200, 503]));
expect(new Set(responses.map((r) => r.headers['cache-control']))).toEqual(new Set([`public, max-age=${config.cache.requestTimeout}`, `public, max-age=${config.cache.routeExpire}`]));
expect(responses.filter((r) => r.text.includes('This path is currently fetching, please come back later!'))).toHaveLength(1);
});
});