test: errors

This commit is contained in:
DIYgod
2024-02-26 18:36:34 +08:00
parent 0b5b83c88d
commit b58b6e60cf
2 changed files with 10 additions and 16 deletions
@@ -1,22 +1,15 @@
process.env.SENTRY = 'https://mock@mock.com/1';
process.env.SENTRY_ROUTE_TIMEOUT = '0';
import { describe, expect, it, afterAll } from '@jest/globals';
import supertest from 'supertest';
import server from '@/index';
import { load } from 'cheerio';
import { config } from '@/config';
const supertest = require('supertest');
jest.mock('request-promise-native');
const server = require('../../lib/index');
const config = require('../../lib/config').value;
const request = supertest(server);
const cheerio = require('cheerio');
afterAll(() => {
server.close();
});
afterAll(() => {
delete process.env.SENTRY;
delete process.env.SENTRY_ROUTE_TIMEOUT;
});
describe('error', () => {
it(`error`, async () => {
const response = await request.get('/test/error');
@@ -39,7 +32,7 @@ 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(new Set(responses.map((r) => r.headers['cache-control']))).toEqual(new Set([`public, max-age=${config.cache.routeExpire}`, `public, max-age=${config.requestTimeout / 1000}`]));
expect(responses.filter((r) => r.text.includes('This path is currently fetching, please come back later!'))).toHaveLength(1);
});
});
@@ -50,10 +43,10 @@ describe('v2 route throws an error', () => {
await request.get('/thisDoesNotExist');
const response = await request.get('/');
const $ = cheerio.load(response.text);
const $ = load(response.text);
$('.debug-item').each((index, item) => {
const key = $(item).find('.debug-key').text().trim();
const value = $(item).find('.debug-value').html().trim();
const value = $(item).find('.debug-value').html()?.trim();
switch (key) {
case 'Request Amount:':
expect(value).toBe('7');
@@ -70,6 +63,7 @@ describe('v2 route throws an error', () => {
case 'Hot Error Paths:':
expect(value).toBe('2 /test/error<br>1 /test/httperror<br>1 /test/slow<br>1 /thisDoesNotExist<br>');
break;
default:
}
});
});
+1 -1
View File
@@ -43,9 +43,9 @@ export const errorHandler: ErrorHandler = (error, ctx) => {
ctx.header('Content-Type', 'text/html; charset=UTF-8');
if (error instanceof RequestInProgressError) {
ctx.header('Cache-Control', `public, max-age=${config.requestTimeout / 1000}`);
ctx.status(503);
message = error.message;
ctx.set('Cache-Control', `public, max-age=${config.cache.requestTimeout}`);
} else if (error instanceof RejectError) {
ctx.status(403);
message = error.message;