|
4 | 4 | * Copyright 2016-Present Datadog, Inc. |
5 | 5 | */ |
6 | 6 |
|
7 | | -import { getOperationName, getVariables, getOperationType } from '../helpers'; |
| 7 | +import { |
| 8 | + getOperationName, |
| 9 | + getVariables, |
| 10 | + getOperationType, |
| 11 | + getPayload |
| 12 | +} from '../helpers'; |
8 | 13 |
|
9 | 14 | import { |
10 | 15 | createCatOperation, |
@@ -62,4 +67,75 @@ describe('helpers', () => { |
62 | 67 | expect(getOperationType({ query: { definitions: [] } })).toBeNull(); |
63 | 68 | }); |
64 | 69 | }); |
| 70 | + |
| 71 | + describe('getPayload', () => { |
| 72 | + it('returns null when trackPayload is false', () => { |
| 73 | + expect(getPayload(getCountryOperation, false)).toBeNull(); |
| 74 | + }); |
| 75 | + |
| 76 | + it('returns null when trackPayload is not provided (defaults to false)', () => { |
| 77 | + expect(getPayload(getCountryOperation)).toBeNull(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('returns the query string when trackPayload is true', () => { |
| 81 | + const payload = getPayload(getCountryOperation, true); |
| 82 | + expect(payload).toBeTruthy(); |
| 83 | + expect(payload).toContain('query CountryDetails'); |
| 84 | + expect(payload).toContain('country'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('returns the query string for mutations', () => { |
| 88 | + const payload = getPayload(createCatOperation, true); |
| 89 | + expect(payload).toBeTruthy(); |
| 90 | + expect(payload).toContain('mutation CreateCat'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('trims whitespace from the query string', () => { |
| 94 | + const payload = getPayload(getCountryOperation, true); |
| 95 | + expect(payload).toBeTruthy(); |
| 96 | + // Check that there's no leading/trailing whitespace |
| 97 | + expect(payload).toBe(payload?.trim()); |
| 98 | + }); |
| 99 | + |
| 100 | + it('truncates query strings longer than 32 KiB', () => { |
| 101 | + // Create a mock operation with a very long query |
| 102 | + // Generate a query with a lot of fields to exceed 32 KiB |
| 103 | + const fields = 'a'.repeat(1000); |
| 104 | + const mockOperation: any = { |
| 105 | + query: { |
| 106 | + kind: 'Document', |
| 107 | + definitions: [ |
| 108 | + { |
| 109 | + kind: 'OperationDefinition', |
| 110 | + operation: 'query', |
| 111 | + selectionSet: { |
| 112 | + kind: 'SelectionSet', |
| 113 | + selections: Array.from( |
| 114 | + { length: 100 }, |
| 115 | + (_, i) => ({ |
| 116 | + kind: 'Field', |
| 117 | + name: { |
| 118 | + kind: 'Name', |
| 119 | + value: `field${i}_${fields}` |
| 120 | + } |
| 121 | + }) |
| 122 | + ) |
| 123 | + } |
| 124 | + } |
| 125 | + ] |
| 126 | + } |
| 127 | + }; |
| 128 | + |
| 129 | + const payload = getPayload(mockOperation, true); |
| 130 | + expect(payload).toBeTruthy(); |
| 131 | + expect(payload?.length).toBe(32 * 1024 + 3); // 32 KiB + '...' |
| 132 | + expect(payload?.endsWith('...')).toBe(true); |
| 133 | + }); |
| 134 | + |
| 135 | + it('does not crash if the operation is malformed', () => { |
| 136 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 137 | + // @ts-ignore |
| 138 | + expect(getPayload({}, true)).toBeNull(); |
| 139 | + }); |
| 140 | + }); |
65 | 141 | }); |
0 commit comments