headers.spec.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { beforeEach, describe, expect, it, vi } from 'vitest';
  2. import { configManager } from '../../config-manager';
  3. import { determineDisposition } from './headers';
  4. vi.mock('../../config-manager', () => ({
  5. configManager: {
  6. getConfig: vi.fn(),
  7. },
  8. }));
  9. describe('determineDisposition', () => {
  10. beforeEach(() => {
  11. vi.resetAllMocks();
  12. });
  13. const setupMocks = (
  14. inlineMimeTypes: string[],
  15. attachmentMimeTypes: string[],
  16. ) => {
  17. vi.mocked(configManager.getConfig).mockImplementation(((key: string) => {
  18. if (key === 'attachments:contentDisposition:inlineMimeTypes') {
  19. return { inlineMimeTypes };
  20. }
  21. if (key === 'attachments:contentDisposition:attachmentMimeTypes') {
  22. return { attachmentMimeTypes };
  23. }
  24. return {};
  25. }) as typeof configManager.getConfig);
  26. };
  27. describe('priority: attachmentMimeTypes over inlineMimeTypes', () => {
  28. it('should return attachment when MIME type is in both lists', () => {
  29. setupMocks(['image/png'], ['image/png']);
  30. const result = determineDisposition('image/png');
  31. expect(result).toBe('attachment');
  32. });
  33. });
  34. describe('case-insensitive matching', () => {
  35. it('should match attachmentMimeTypes case-insensitively', () => {
  36. setupMocks([], ['image/png']);
  37. const result = determineDisposition('IMAGE/PNG');
  38. expect(result).toBe('attachment');
  39. });
  40. it('should match inlineMimeTypes case-insensitively', () => {
  41. setupMocks(['image/png'], []);
  42. const result = determineDisposition('IMAGE/PNG');
  43. expect(result).toBe('inline');
  44. });
  45. it('should match when config has uppercase MIME type', () => {
  46. setupMocks(['IMAGE/PNG'], []);
  47. const result = determineDisposition('image/png');
  48. expect(result).toBe('inline');
  49. });
  50. });
  51. describe('defaultContentDispositionSettings fallback', () => {
  52. it('should return inline for image/png when not in admin config', () => {
  53. setupMocks([], []);
  54. const result = determineDisposition('image/png');
  55. expect(result).toBe('inline');
  56. });
  57. it('should return attachment for text/html when not in admin config', () => {
  58. setupMocks([], []);
  59. const result = determineDisposition('text/html');
  60. expect(result).toBe('attachment');
  61. });
  62. });
  63. describe('unknown MIME types', () => {
  64. it('should return attachment for unknown MIME types', () => {
  65. setupMocks([], []);
  66. const result = determineDisposition('application/x-unknown-type');
  67. expect(result).toBe('attachment');
  68. });
  69. });
  70. describe('admin config takes priority over defaults', () => {
  71. it('should return attachment for image/png when in admin attachmentMimeTypes', () => {
  72. setupMocks([], ['image/png']);
  73. const result = determineDisposition('image/png');
  74. expect(result).toBe('attachment');
  75. });
  76. it('should return inline for text/html when in admin inlineMimeTypes', () => {
  77. setupMocks(['text/html'], []);
  78. const result = determineDisposition('text/html');
  79. expect(result).toBe('inline');
  80. });
  81. });
  82. });