-
Notifications
You must be signed in to change notification settings - Fork 242
/
nockMocks.ts
153 lines (135 loc) 路 3.85 KB
/
nockMocks.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
import nock from 'nock';
import { HEALTH_CHECK_QUERY, SERVICE_DEFINITION_QUERY } from '../..';
import { SUPERGRAPH_SDL_QUERY } from '../../loadSupergraphSdlFromStorage';
import { getTestingSupergraphSdl } from '../../__tests__/execution-utils';
import { print } from 'graphql';
import { Fixture, fixtures as testingFixtures } from 'apollo-federation-integration-testsuite';
export const graphRef = 'federated-service@current';
export const apiKey = 'service:federated-service:DD71EBbGmsuh-6suUVDwnA';
const apiKeyHash = 'dd55a79d467976346d229a7b12b673ce';
export const mockApolloConfig = {
apollo: {
key: apiKey,
keyHash: apiKeyHash,
graphRef,
},
};
// Service mocks
function mockSdlQuery({ url }: Fixture) {
return nock(url).post('/', {
query: SERVICE_DEFINITION_QUERY,
});
}
export function mockSdlQuerySuccess(service: Fixture) {
return mockSdlQuery(service).reply(200, {
data: { _service: { sdl: print(service.typeDefs) } },
});
}
export function mockAllServicesSdlQuerySuccess(
fixtures: Fixture[] = testingFixtures,
) {
return fixtures.map((fixture) =>
mockSdlQuery(fixture).reply(200, {
data: { _service: { sdl: print(fixture.typeDefs) } },
}),
);
}
export function mockServiceHealthCheck({ url }: Fixture) {
return nock(url).post('/', {
query: HEALTH_CHECK_QUERY,
});
}
export function mockServiceHealthCheckSuccess(service: Fixture) {
return mockServiceHealthCheck(service).reply(200, {
data: { __typename: 'Query' },
});
}
export function mockAllServicesHealthCheckSuccess(
fixtures: Fixture[] = testingFixtures,
) {
return fixtures.map((fixture) =>
mockServiceHealthCheck(fixture).reply(200, {
data: { __typename: 'Query' },
}),
);
}
// Supergraph SDL fetching mocks
function gatewayNock(url: Parameters<typeof nock>[0]): nock.Scope {
const { name, version } = require('../../../package.json');
return nock(url, {
reqheaders: {
'apollographql-client-name': name,
'apollographql-client-version': version,
'user-agent': `${name}/${version}`,
'content-type': 'application/json',
},
});
}
export const mockCloudConfigUrl =
'https://example.cloud-config-url.com/cloudconfig/';
export const mockOutOfBandReporterUrl =
'https://example.outofbandreporter.com/monitoring/';
export function mockSupergraphSdlRequestIfAfter(ifAfter: string | null) {
return gatewayNock(mockCloudConfigUrl).post('/', {
query: SUPERGRAPH_SDL_QUERY,
variables: {
ref: graphRef,
apiKey: apiKey,
ifAfterId: ifAfter,
},
});
}
export function mockSupergraphSdlRequest(ifAfter: string | null = null) {
return mockSupergraphSdlRequestIfAfter(ifAfter);
}
export function mockSupergraphSdlRequestSuccessIfAfter(
ifAfter: string | null = null,
id: string = 'originalId-1234',
supergraphSdl: string = getTestingSupergraphSdl(),
) {
if (supergraphSdl == null) {
supergraphSdl = getTestingSupergraphSdl();
}
return mockSupergraphSdlRequestIfAfter(ifAfter).reply(
200,
JSON.stringify({
data: {
routerConfig: {
__typename: 'RouterConfigResult',
id,
supergraphSdl,
},
},
}),
);
}
export function mockSupergraphSdlRequestIfAfterUnchanged(
ifAfter: string | null = null,
) {
return mockSupergraphSdlRequestIfAfter(ifAfter).reply(
200,
JSON.stringify({
data: {
routerConfig: {
__typename: 'Unchanged',
},
},
}),
);
}
export function mockSupergraphSdlRequestSuccess() {
return mockSupergraphSdlRequestSuccessIfAfter(null);
}
export function mockOutOfBandReportRequest() {
return gatewayNock(mockOutOfBandReporterUrl).post('/', () => true);
}
export function mockOutOfBandReportRequestSuccess() {
return mockOutOfBandReportRequest().reply(
200,
JSON.stringify({
data: {
reportError: true
},
}),
);
}