fieldExtractor.test.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { fieldExtractors, FieldExtractorID } from './fieldExtractors';
  2. describe('Extract fields from text', () => {
  3. it('JSON extractor', async () => {
  4. const extractor = fieldExtractors.get(FieldExtractorID.JSON);
  5. const out = extractor.parse('{"a":"148.1672","av":41923755,"c":148.25}');
  6. expect(out).toMatchInlineSnapshot(`
  7. Object {
  8. "a": "148.1672",
  9. "av": 41923755,
  10. "c": 148.25,
  11. }
  12. `);
  13. });
  14. it('Test key-values with single/double quotes', async () => {
  15. const extractor = fieldExtractors.get(FieldExtractorID.KeyValues);
  16. const out = extractor.parse('a="1", "b"=\'2\',c=3 x:y ;\r\nz="d and 4"');
  17. expect(out).toMatchInlineSnapshot(`
  18. Object {
  19. "a": "1",
  20. "b": "2",
  21. "c": "3",
  22. "x": "y",
  23. "z": "d and 4",
  24. }
  25. `);
  26. });
  27. it('Test key-values with nested single/double quotes', async () => {
  28. const extractor = fieldExtractors.get(FieldExtractorID.KeyValues);
  29. const out = extractor.parse(
  30. `a="1", "b"=\'2\',c=3 x:y ;\r\nz="dbl_quotes=\\"Double Quotes\\" sgl_quotes='Single Quotes'"`
  31. );
  32. expect(out).toMatchInlineSnapshot(`
  33. Object {
  34. "a": "1",
  35. "b": "2",
  36. "c": "3",
  37. "x": "y",
  38. "z": "dbl_quotes=\\"Double Quotes\\" sgl_quotes='Single Quotes'",
  39. }
  40. `);
  41. });
  42. it('Test key-values with nested separator characters', async () => {
  43. const extractor = fieldExtractors.get(FieldExtractorID.KeyValues);
  44. const out = extractor.parse(`a="1", "b"=\'2\',c=3 x:y ;\r\nz="This is; testing& validating, 1=:2"`);
  45. expect(out).toMatchInlineSnapshot(`
  46. Object {
  47. "a": "1",
  48. "b": "2",
  49. "c": "3",
  50. "x": "y",
  51. "z": "This is; testing& validating, 1=:2",
  52. }
  53. `);
  54. });
  55. it('Test key-values where some values are null', async () => {
  56. const extractor = fieldExtractors.get(FieldExtractorID.KeyValues);
  57. const out = extractor.parse(`a=, "b"=\'2\',c=3 x: `);
  58. expect(out).toMatchInlineSnapshot(`
  59. Object {
  60. "a": "",
  61. "b": "2",
  62. "c": "3",
  63. "x": "",
  64. }
  65. `);
  66. });
  67. it('Split key+values', async () => {
  68. const extractor = fieldExtractors.get(FieldExtractorID.KeyValues);
  69. const out = extractor.parse('a="1", "b"=\'2\',c=3 x:y ;\r\nz="7"');
  70. expect(out).toMatchInlineSnapshot(`
  71. Object {
  72. "a": "1",
  73. "b": "2",
  74. "c": "3",
  75. "x": "y",
  76. "z": "7",
  77. }
  78. `);
  79. });
  80. it('Split URL style parameters', async () => {
  81. const extractor = fieldExtractors.get(FieldExtractorID.KeyValues);
  82. const out = extractor.parse('a=b&c=d&x=123');
  83. expect(out).toMatchInlineSnapshot(`
  84. Object {
  85. "a": "b",
  86. "c": "d",
  87. "x": "123",
  88. }
  89. `);
  90. });
  91. it('Prometheus labels style (not really supported)', async () => {
  92. const extractor = fieldExtractors.get(FieldExtractorID.KeyValues);
  93. const out = extractor.parse('{foo="bar", baz="42"}');
  94. expect(out).toMatchInlineSnapshot(`
  95. Object {
  96. "baz": "42",
  97. "foo": "bar",
  98. }
  99. `);
  100. });
  101. });