-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathcurl.spec.ts
More file actions
97 lines (80 loc) · 3.22 KB
/
Copy pathcurl.spec.ts
File metadata and controls
97 lines (80 loc) · 3.22 KB
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
import { buildCurlCommand } from './curl';
const ENDPOINT = 'https://api.example.com/graphql';
describe('buildCurlCommand', () => {
it('posts the operation as JSON', () => {
const command = buildCurlCommand({ endpoint: ENDPOINT, query: '{ me { id } }' });
expect(command).toContain('-X POST');
expect(command).toContain(`'${ENDPOINT}'`);
expect(command).toContain(`-H 'content-type: application/json'`);
expect(command).toContain('"query":"{\\n me {\\n id\\n }\\n}"');
});
it('lets operation headers win over preflight headers', () => {
const command = buildCurlCommand({
endpoint: ENDPOINT,
query: '{ me { id } }',
preflightHeaders: { authorization: 'from-preflight', 'x-trace': 'keep-me' },
headers: '{"authorization":"from-operation"}',
});
expect(command).toContain(`-H 'authorization: from-operation'`);
expect(command).not.toContain('from-preflight');
expect(command).toContain(`-H 'x-trace: keep-me'`);
});
it('substitutes env and plugin templates the way the executor does', () => {
const command = buildCurlCommand({
endpoint: ENDPOINT,
query: '{ me { id } }',
headers: '{"authorization":"Bearer {{token}}"}',
variables: '{"slug":"{{plugins.target.slug}}"}',
env: { token: 'secret' },
pluginsState: { target: { slug: 'my-target' } },
});
expect(command).toContain('Bearer secret');
expect(command).toContain('"slug":"my-target"');
});
it('keeps fragments but drops the operations that were not selected', () => {
const command = buildCurlCommand({
endpoint: ENDPOINT,
query: `query A { me { ...F } } query B { other } fragment F on User { id }`,
operationName: 'A',
});
expect(command).toContain('query A');
expect(command).toContain('fragment F on User');
expect(command).not.toContain('query B');
expect(command).toContain('"operationName":"A"');
});
it('moves the document onto the query string for GET', () => {
const command = buildCurlCommand({
endpoint: ENDPOINT,
query: '{ me { id } }',
variables: '{"a":1}',
settings: { fetch: { credentials: 'same-origin', useGETForQueries: true } },
});
expect(command).not.toContain('-X POST');
expect(command).not.toContain('-d ');
expect(command).not.toContain('content-type');
expect(command).toContain('query=');
expect(command).toContain('variables=');
});
it('escapes single quotes so the command stays one shell token', () => {
const command = buildCurlCommand({
endpoint: ENDPOINT,
query: '{ me { id } }',
headers: `{"x-note":"it's here"}`,
});
expect(command).toContain(`-H 'x-note: it'\\''s here'`);
});
it('omits variables and extensions when empty rather than sending {}', () => {
const command = buildCurlCommand({
endpoint: ENDPOINT,
query: '{ me { id } }',
variables: '{}',
extensions: '',
});
expect(command).not.toContain('"variables"');
expect(command).not.toContain('"extensions"');
});
it('falls back to the raw query when it cannot be parsed', () => {
const command = buildCurlCommand({ endpoint: ENDPOINT, query: '{ me { id }' });
expect(command).toContain('{ me { id }');
});
});