-
Notifications
You must be signed in to change notification settings - Fork 2k
/
extension.test.ts
360 lines (317 loc) 路 11 KB
/
extension.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import { makeExecutableSchema, addMockFunctionsToSchema } from 'graphql-tools';
import {
GraphQLExtensionStack,
enableGraphQLExtensions,
} from 'graphql-extensions';
import { graphql } from 'graphql';
import { Request } from 'node-fetch';
import {
EngineReportingExtension,
makeTraceDetails,
makeHTTPRequestHeaders,
} from '../extension';
import { Headers } from 'apollo-server-env';
import { InMemoryLRUCache } from 'apollo-server-caching';
import { AddTraceArgs } from '../agent';
import { Trace } from 'apollo-engine-reporting-protobuf';
test('trace construction', async () => {
const typeDefs = `
type User {
id: Int
name: String
posts(limit: Int): [Post]
}
type Post {
id: Int
title: String
views: Int
author: User
}
type Query {
aString: String
aBoolean: Boolean
anInt: Int
author(id: Int): User
topPosts(limit: Int): [Post]
}
`;
const query = `
query q {
author(id: 5) {
name
posts(limit: 2) {
id
}
}
aBoolean
}
`;
const schema = makeExecutableSchema({ typeDefs });
addMockFunctionsToSchema({ schema });
enableGraphQLExtensions(schema);
const traces: Array<any> = [];
async function addTrace({ trace, operationName, schemaHash }: AddTraceArgs) {
traces.push({ schemaHash, operationName, trace });
}
const reportingExtension = new EngineReportingExtension(
{},
addTrace,
'schema-hash',
);
const stack = new GraphQLExtensionStack([reportingExtension]);
const requestDidEnd = stack.requestDidStart({
request: new Request('http://localhost:123/foo') as any,
queryString: query,
requestContext: {
request: {
query,
operationName: 'q',
extensions: {
clientName: 'testing suite',
},
},
context: {},
cache: new InMemoryLRUCache(),
},
context: {},
});
await graphql({
schema,
source: query,
contextValue: { _extensionStack: stack },
});
requestDidEnd();
// XXX actually write some tests
});
/**
* TESTS FOR sendVariableValues REPORTING OPTION
*/
const variables: Record<string, any> = {
testing: 'testing',
t2: 2,
};
describe('check variableJson output for sendVariableValues null or undefined (default)', () => {
it('Case 1: No keys/values in variables to be filtered/not filtered', () => {
const emptyOutput = new Trace.Details();
expect(makeTraceDetails({})).toEqual(emptyOutput);
expect(makeTraceDetails({}, undefined)).toEqual(emptyOutput);
expect(makeTraceDetails({})).toEqual(emptyOutput);
});
it('Case 2: Filter all variables', () => {
const filteredOutput = new Trace.Details();
Object.keys(variables).forEach(name => {
filteredOutput.variablesJson[name] = '';
});
expect(makeTraceDetails(variables)).toEqual(filteredOutput);
expect(makeTraceDetails(variables)).toEqual(filteredOutput);
expect(makeTraceDetails(variables, undefined)).toEqual(filteredOutput);
});
});
describe('check variableJson output for sendVariableValues all/none type', () => {
it('Case 1: No keys/values in variables to be filtered/not filtered', () => {
const emptyOutput = new Trace.Details();
expect(makeTraceDetails({}, { all: true })).toEqual(emptyOutput);
expect(makeTraceDetails({}, { none: true })).toEqual(emptyOutput);
});
const filteredOutput = new Trace.Details();
Object.keys(variables).forEach(name => {
filteredOutput.variablesJson[name] = '';
});
const nonFilteredOutput = new Trace.Details();
Object.keys(variables).forEach(name => {
nonFilteredOutput.variablesJson[name] = JSON.stringify(variables[name]);
});
it('Case 2: Filter all variables', () => {
expect(makeTraceDetails(variables, { none: true })).toEqual(filteredOutput);
});
it('Case 3: Do not filter variables', () => {
expect(makeTraceDetails(variables, { all: true })).toEqual(
nonFilteredOutput,
);
});
it('Case 4: Check behavior for invalid inputs', () => {
expect(makeTraceDetails(variables, { none: false })).toEqual(
nonFilteredOutput,
);
expect(makeTraceDetails(variables, { all: false })).toEqual(filteredOutput);
});
});
describe('variableJson output for sendVariableValues exceptNames: Array type', () => {
it('array contains some values not in keys', () => {
const privateVariablesArray: string[] = ['testing', 'notInVariables'];
const expectedVariablesJson = {
testing: '',
t2: JSON.stringify(2),
};
expect(
makeTraceDetails(variables, { exceptNames: privateVariablesArray })
.variablesJson,
).toEqual(expectedVariablesJson);
});
it('none=true equivalent to exceptNames=[all variables]', () => {
const privateVariablesArray: string[] = ['testing', 't2'];
expect(makeTraceDetails(variables, { none: true }).variablesJson).toEqual(
makeTraceDetails(variables, { exceptNames: privateVariablesArray })
.variablesJson,
);
});
});
describe('variableJson output for sendVariableValues onlyNames: Array type', () => {
it('array contains some values not in keys', () => {
const privateVariablesArray: string[] = ['t2', 'notInVariables'];
const expectedVariablesJson = {
testing: '',
t2: JSON.stringify(2),
};
expect(
makeTraceDetails(variables, { onlyNames: privateVariablesArray })
.variablesJson,
).toEqual(expectedVariablesJson);
});
it('all=true equivalent to onlyNames=[all variables]', () => {
const privateVariablesArray: string[] = ['testing', 't2'];
expect(makeTraceDetails(variables, { all: true }).variablesJson).toEqual(
makeTraceDetails(variables, { onlyNames: privateVariablesArray })
.variablesJson,
);
});
it('none=true equivalent to onlyNames=[]', () => {
const privateVariablesArray: string[] = [];
expect(makeTraceDetails(variables, { none: true }).variablesJson).toEqual(
makeTraceDetails(variables, { onlyNames: privateVariablesArray })
.variablesJson,
);
});
});
describe('variableJson output for sendVariableValues transform: custom function type', () => {
it('test custom function that redacts every variable to some value', () => {
const modifiedValue = 100;
const customModifier = (input: {
variables: Record<string, any>;
}): Record<string, any> => {
const out: Record<string, any> = Object.create(null);
Object.keys(input.variables).map((name: string) => {
out[name] = modifiedValue;
});
return out;
};
// Expected output
const output = new Trace.Details();
Object.keys(variables).forEach(name => {
output.variablesJson[name] = JSON.stringify(modifiedValue);
});
expect(makeTraceDetails(variables, { transform: customModifier })).toEqual(
output,
);
});
const origKeys = Object.keys(variables);
const firstKey = origKeys[0];
const secondKey = origKeys[1];
const modifier = (input: {
variables: Record<string, any>;
}): Record<string, any> => {
const out: Record<string, any> = Object.create(null);
Object.keys(input.variables).map((name: string) => {
out[name] = null;
});
// remove the first key, and then add a new key
delete out[firstKey];
out['newkey'] = 'blah';
return out;
};
it('original keys in variables should match the modified keys', () => {
expect(
Object.keys(
makeTraceDetails(variables, { transform: modifier }).variablesJson,
).sort(),
).toEqual(origKeys.sort());
});
it('expect empty string for keys removed by the custom modifier', () => {
expect(
makeTraceDetails(variables, { transform: modifier }).variablesJson[
firstKey
],
).toEqual('');
});
it('expect stringify(null) for values set to null by custom modifier', () => {
expect(
makeTraceDetails(variables, { transform: modifier }).variablesJson[
secondKey
],
).toEqual(JSON.stringify(null));
});
});
describe('Catch circular reference error during JSON.stringify', () => {
const circularReference = {};
circularReference['this'] = circularReference;
const circularVariables = {
bad: circularReference,
};
expect(
makeTraceDetails(circularVariables, { all: true }).variablesJson['bad'],
).toEqual(JSON.stringify('[Unable to convert value to JSON]'));
});
function makeTestHTTP(): Trace.HTTP {
return new Trace.HTTP({
method: Trace.HTTP.Method.UNKNOWN,
host: null,
path: null,
});
}
/**
* TESTS FOR THE sendHeaders REPORTING OPTION
*/
const headers = new Headers();
headers.append('name', 'value');
headers.append('authorization', 'blahblah'); // THIS SHOULD NEVER BE SENT
const headersOutput = { name: new Trace.HTTP.Values({ value: ['value'] }) };
describe('tests for the sendHeaders reporting option', () => {
it('sendHeaders defaults to hiding all', () => {
const http = makeTestHTTP();
// sendHeaders: null is not a valid TS input, but check the output anyways
makeHTTPRequestHeaders(http, headers, null);
expect(http.requestHeaders).toEqual({});
makeHTTPRequestHeaders(http, headers, undefined);
expect(http.requestHeaders).toEqual({});
makeHTTPRequestHeaders(http, headers);
expect(http.requestHeaders).toEqual({});
});
it('sendHeaders.all and sendHeaders.none', () => {
const httpSafelist = makeTestHTTP();
makeHTTPRequestHeaders(httpSafelist, headers, { all: true });
expect(httpSafelist.requestHeaders).toEqual(headersOutput);
const httpBlocklist = makeTestHTTP();
makeHTTPRequestHeaders(httpBlocklist, headers, { none: true });
expect(httpBlocklist.requestHeaders).toEqual({});
});
it('invalid inputs for sendHeaders.all and sendHeaders.none', () => {
const httpSafelist = makeTestHTTP();
makeHTTPRequestHeaders(httpSafelist, headers, { none: false });
expect(httpSafelist.requestHeaders).toEqual(headersOutput);
const httpBlocklist = makeTestHTTP();
makeHTTPRequestHeaders(httpBlocklist, headers, { all: false });
expect(httpBlocklist.requestHeaders).toEqual({});
});
it('test sendHeaders.exceptNames', () => {
const except: String[] = ['name', 'notinheaders'];
const http = makeTestHTTP();
makeHTTPRequestHeaders(http, headers, { exceptNames: except });
expect(http.requestHeaders).toEqual({});
});
it('test sendHeaders.onlyNames', () => {
// headers that should never be sent (such as "authorization") should still be removed if in includeHeaders
const include: String[] = ['name', 'authorization', 'notinheaders'];
const http = makeTestHTTP();
makeHTTPRequestHeaders(http, headers, { onlyNames: include });
expect(http.requestHeaders).toEqual(headersOutput);
});
it('authorization, cookie, and set-cookie headers should never be sent', () => {
headers.append('cookie', 'blahblah');
headers.append('set-cookie', 'blahblah');
const http = makeTestHTTP();
makeHTTPRequestHeaders(http, headers, { all: true });
expect(http.requestHeaders['authorization']).toBe(undefined);
expect(http.requestHeaders['cookie']).toBe(undefined);
expect(http.requestHeaders['set-cookie']).toBe(undefined);
});
});