|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import api from '@/app/services/api'; |
| 3 | +import { apiClient as utilsApiClient } from '@/utils/apiClient'; |
| 4 | +import libApiClient from '@/lib/api/client'; |
| 5 | +import { GET as healthHandler } from '@/app/api/health/route'; |
| 6 | +import { GET as compressionHandler } from '@/app/api/compression/route'; |
| 7 | +// @ts-ignore |
| 8 | +import nextConfig from '@/next.config.js'; |
| 9 | + |
| 10 | +describe('API Response Compression Configuration', () => { |
| 11 | + describe('next.config.js compression settings', () => { |
| 12 | + it('should have compress: true explicitly enabled', () => { |
| 13 | + expect(nextConfig.compress).toBe(true); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should include Vary: Accept-Encoding in custom headers for /api routes', async () => { |
| 17 | + expect(typeof nextConfig.headers).toBe('function'); |
| 18 | + const headersConfig = (await nextConfig.headers?.()) || []; |
| 19 | + const apiHeaderRule = headersConfig.find( |
| 20 | + (rule: { source: string }) => rule.source === '/api/:path*' |
| 21 | + ); |
| 22 | + |
| 23 | + expect(apiHeaderRule).toBeDefined(); |
| 24 | + const varyHeader = apiHeaderRule?.headers?.find( |
| 25 | + (h: { key: string; value: string }) => h.key.toLowerCase() === 'vary' |
| 26 | + ); |
| 27 | + expect(varyHeader).toBeDefined(); |
| 28 | + expect(varyHeader?.value).toContain('Accept-Encoding'); |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + describe('Axios Client Compression Headers & Config', () => { |
| 33 | + it('app/services/api should configure compression headers and decompress: true', () => { |
| 34 | + expect(api.defaults.decompress).toBe(true); |
| 35 | + const headers = api.defaults.headers; |
| 36 | + const acceptEncoding = |
| 37 | + (headers as Record<string, unknown>)['Accept-Encoding'] || |
| 38 | + (headers.common as Record<string, unknown> | undefined)?.['Accept-Encoding']; |
| 39 | + expect(acceptEncoding).toBe('gzip, deflate, br'); |
| 40 | + const accept = |
| 41 | + (headers as Record<string, unknown>)['Accept'] || |
| 42 | + (headers.common as Record<string, unknown> | undefined)?.['Accept']; |
| 43 | + expect(accept).toBe('application/json'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('utils/apiClient should configure compression headers and decompress: true', () => { |
| 47 | + expect(utilsApiClient.defaults.decompress).toBe(true); |
| 48 | + const headers = utilsApiClient.defaults.headers; |
| 49 | + const acceptEncoding = |
| 50 | + (headers as Record<string, unknown>)['Accept-Encoding'] || |
| 51 | + (headers.common as Record<string, unknown> | undefined)?.['Accept-Encoding']; |
| 52 | + expect(acceptEncoding).toBe('gzip, deflate, br'); |
| 53 | + const accept = |
| 54 | + (headers as Record<string, unknown>)['Accept'] || |
| 55 | + (headers.common as Record<string, unknown> | undefined)?.['Accept']; |
| 56 | + expect(accept).toBe('application/json'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('lib/api/client should configure compression headers and decompress: true', () => { |
| 60 | + expect(libApiClient.defaults.decompress).toBe(true); |
| 61 | + const headers = libApiClient.defaults.headers; |
| 62 | + const acceptEncoding = |
| 63 | + (headers as Record<string, unknown>)['Accept-Encoding'] || |
| 64 | + (headers.common as Record<string, unknown> | undefined)?.['Accept-Encoding']; |
| 65 | + expect(acceptEncoding).toBe('gzip, deflate, br'); |
| 66 | + const accept = |
| 67 | + (headers as Record<string, unknown>)['Accept'] || |
| 68 | + (headers.common as Record<string, unknown> | undefined)?.['Accept']; |
| 69 | + expect(accept).toBe('application/json'); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + describe('Route Handlers', () => { |
| 74 | + it('/api/health route handler should return status ok with Vary: Accept-Encoding', async () => { |
| 75 | + const response = await healthHandler(); |
| 76 | + expect(response.status).toBe(200); |
| 77 | + expect(response.headers.get('vary')).toBe('Accept-Encoding'); |
| 78 | + expect(response.headers.get('content-type')).toContain('application/json'); |
| 79 | + |
| 80 | + const data = await response.json(); |
| 81 | + expect(data.status).toBe('ok'); |
| 82 | + expect(data.service).toBe('stellarAid-api'); |
| 83 | + expect(data.compression.enabled).toBe(true); |
| 84 | + expect(data.compression.supportedEncodings).toEqual(['gzip', 'deflate', 'br']); |
| 85 | + }); |
| 86 | + |
| 87 | + it('/api/compression route handler should return structured dataset with Vary: Accept-Encoding', async () => { |
| 88 | + const response = await compressionHandler(); |
| 89 | + expect(response.status).toBe(200); |
| 90 | + expect(response.headers.get('vary')).toBe('Accept-Encoding'); |
| 91 | + expect(response.headers.get('content-type')).toContain('application/json'); |
| 92 | + |
| 93 | + const data = await response.json(); |
| 94 | + expect(data.success).toBe(true); |
| 95 | + expect(data.totalCount).toBe(100); |
| 96 | + expect(Array.isArray(data.data)).toBe(true); |
| 97 | + expect(data.data.length).toBe(100); |
| 98 | + }); |
| 99 | + }); |
| 100 | +}); |
0 commit comments