diff --git a/test/middleware/onerror.js b/lib/errors/index.test.ts similarity index 79% rename from test/middleware/onerror.js rename to lib/errors/index.test.ts index e92fcea13..11326e707 100644 --- a/test/middleware/onerror.js +++ b/lib/errors/index.test.ts @@ -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
1 /test/httperror
1 /test/slow
1 /thisDoesNotExist
'); break; + default: } }); }); diff --git a/lib/errors/index.ts b/lib/errors/index.ts index 055300e9a..a16cfe091 100644 --- a/lib/errors/index.ts +++ b/lib/errors/index.ts @@ -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;