|
| 1 | +import {ExpressKit, Request, Response} from '..'; |
| 2 | +import {NodeKit} from '@gravity-ui/nodekit'; |
| 3 | +import request from 'supertest'; |
| 4 | + |
| 5 | +describe('Caching Control', () => { |
| 6 | + it('should set no-cache headers by default', async () => { |
| 7 | + const nodekit = new NodeKit({config: {}}); |
| 8 | + const app = new ExpressKit(nodekit, { |
| 9 | + 'GET /test': (_req: Request, res: Response) => { |
| 10 | + res.json({ok: true}); |
| 11 | + }, |
| 12 | + }); |
| 13 | + |
| 14 | + const res = await request.agent(app.express).get('/test'); |
| 15 | + expect(res.headers['cache-control']).toBe( |
| 16 | + 'no-store, max-age=0, must-revalidate, proxy-revalidate', |
| 17 | + ); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should allow caching when expressEnableCaching is true', async () => { |
| 21 | + const nodekit = new NodeKit({config: {expressEnableCaching: true}}); |
| 22 | + const app = new ExpressKit(nodekit, { |
| 23 | + 'GET /test': (_req: Request, res: Response) => { |
| 24 | + res.json({ok: true}); |
| 25 | + }, |
| 26 | + }); |
| 27 | + |
| 28 | + const res = await request.agent(app.express).get('/test'); |
| 29 | + expect(res.headers['cache-control']).toBeUndefined(); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should respect route-level enableCaching flag', async () => { |
| 33 | + const nodekit = new NodeKit({config: {}}); |
| 34 | + const app = new ExpressKit(nodekit, { |
| 35 | + 'GET /cached': { |
| 36 | + enableCaching: true, |
| 37 | + handler: (_req: Request, res: Response) => { |
| 38 | + res.json({ok: true}); |
| 39 | + }, |
| 40 | + }, |
| 41 | + 'GET /not-cached': { |
| 42 | + enableCaching: false, |
| 43 | + handler: (_req: Request, res: Response) => { |
| 44 | + res.json({ok: true}); |
| 45 | + }, |
| 46 | + }, |
| 47 | + }); |
| 48 | + |
| 49 | + const cached = await request.agent(app.express).get('/cached'); |
| 50 | + expect(cached.headers['cache-control']).toBeUndefined(); |
| 51 | + |
| 52 | + const notCached = await request.agent(app.express).get('/not-cached'); |
| 53 | + expect(notCached.headers['cache-control']).toBe( |
| 54 | + 'no-store, max-age=0, must-revalidate, proxy-revalidate', |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should allow route override of global config', async () => { |
| 59 | + const nodekit = new NodeKit({config: {expressEnableCaching: true}}); |
| 60 | + const app = new ExpressKit(nodekit, { |
| 61 | + 'GET /override': { |
| 62 | + enableCaching: false, |
| 63 | + handler: (_req: Request, res: Response) => { |
| 64 | + res.json({ok: true}); |
| 65 | + }, |
| 66 | + }, |
| 67 | + }); |
| 68 | + |
| 69 | + const res = await request.agent(app.express).get('/override'); |
| 70 | + expect(res.headers['cache-control']).toBe( |
| 71 | + 'no-store, max-age=0, must-revalidate, proxy-revalidate', |
| 72 | + ); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should set enableCaching in routeInfo', async () => { |
| 76 | + const nodekit = new NodeKit({config: {expressEnableCaching: true}}); |
| 77 | + const app = new ExpressKit(nodekit, { |
| 78 | + 'GET /info': (req: Request, res: Response) => { |
| 79 | + res.json({enableCaching: req.routeInfo.enableCaching}); |
| 80 | + }, |
| 81 | + }); |
| 82 | + |
| 83 | + const res = await request.agent(app.express).get('/info'); |
| 84 | + expect(res.body.enableCaching).toBe(true); |
| 85 | + }); |
| 86 | +}); |
0 commit comments