anonymize-query-params.spec.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { describe, expect, it } from 'vitest';
  2. import { anonymizeQueryParams } from './anonymize-query-params';
  3. describe('anonymizeQueryParams', () => {
  4. it.each`
  5. description | target | paramNames | expected
  6. ${'no matching parameters'} | ${'/_api/v3/test?other=value&another=test'} | ${['nonexistent']} | ${'/_api/v3/test?other=value&another=test'}
  7. ${'single string parameter'} | ${'/_api/v3/search?q=sensitive-query'} | ${['q']} | ${'/_api/v3/search?q=%5BANONYMIZED%5D'}
  8. ${'array-style parameters'} | ${'/_api/v3/page/test?paths[]=/user/john&paths[]=/user/jane'} | ${['paths']} | ${'/_api/v3/page/test?paths%5B%5D=%5BANONYMIZED%5D'}
  9. ${'JSON array format'} | ${'/_api/v3/test?paths=["/user/john","/user/jane"]'} | ${['paths']} | ${'/_api/v3/test?paths=%5B%22%5BANONYMIZED%5D%22%5D'}
  10. ${'multiple parameters'} | ${'/_api/v3/test?q=secret&path=/user/john&other=keep'} | ${['q', 'path']} | ${'/_api/v3/test?q=%5BANONYMIZED%5D&path=%5BANONYMIZED%5D&other=keep'}
  11. ${'empty parameter value'} | ${'/_api/v3/test?q=&other=value'} | ${['q']} | ${'/_api/v3/test?q=%5BANONYMIZED%5D&other=value'}
  12. ${'parameter without value'} | ${'/_api/v3/test?q&other=value'} | ${['q']} | ${'/_api/v3/test?q=%5BANONYMIZED%5D&other=value'}
  13. ${'mixed array and single'} | ${'/_api/v3/test?q=search&paths[]=/user/john&paths[]=/user/jane'} | ${['q', 'paths']} | ${'/_api/v3/test?q=%5BANONYMIZED%5D&paths%5B%5D=%5BANONYMIZED%5D'}
  14. ${'with section'} | ${'/_api/v3/test?q=search#section'} | ${['q']} | ${'/_api/v3/test?q=%5BANONYMIZED%5D#section'}
  15. ${'malformed JSON array'} | ${'/_api/v3/test?paths=["/user/john"'} | ${['paths']} | ${'/_api/v3/test?paths=%5BANONYMIZED%5D'}
  16. ${'empty JSON array'} | ${'/_api/v3/test?paths=[]'} | ${['paths']} | ${'/_api/v3/test?paths=%5BANONYMIZED%5D'}
  17. ${'single item JSON array'} | ${'/_api/v3/test?paths=["/user/john"]'} | ${['paths']} | ${'/_api/v3/test?paths=%5B%22%5BANONYMIZED%5D%22%5D'}
  18. ${'URL with no query params'} | ${'/_api/v3/test'} | ${['q']} | ${'/_api/v3/test'}
  19. ${'complex path with encoding'} | ${'/_api/v3/test?path=%2Fuser%2Fjohn%20doe'} | ${['path']} | ${'/_api/v3/test?path=%5BANONYMIZED%5D'}
  20. `('should handle $description', ({ target, paramNames, expected }) => {
  21. const result = anonymizeQueryParams(target, paramNames);
  22. expect(result).toBe(expected);
  23. });
  24. it.each`
  25. description | target | paramNames | expected
  26. ${'invalid URL format'} | ${'not-a-valid-url'} | ${['q']} | ${'not-a-valid-url'}
  27. ${'empty string target'} | ${''} | ${['q']} | ${''}
  28. ${'empty paramNames array'} | ${'/_api/v3/test?q=secret'} | ${[]} | ${'/_api/v3/test?q=secret'}
  29. `(
  30. 'should handle edge cases: $description',
  31. ({ target, paramNames, expected }) => {
  32. const result = anonymizeQueryParams(target, paramNames);
  33. expect(result).toBe(expected);
  34. },
  35. );
  36. });