-
Notifications
You must be signed in to change notification settings - Fork 79
/
consumer.integration.spec.ts
414 lines (381 loc) · 13 KB
/
consumer.integration.spec.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import * as chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import axios from 'axios';
import path = require('path');
import zlib = require('zlib');
import FormData = require('form-data');
import fs = require('fs');
import { load } from 'protobufjs';
import { setLogLevel } from '../src/logger';
import {
ConsumerPact,
makeConsumerPact,
MatchingResultRequestMismatch,
} from '../src';
import { FfiSpecificationVersion } from '../src/ffi/types';
chai.use(chaiAsPromised);
const { expect } = chai;
const HOST = '127.0.0.1';
const isWin = process.platform === 'win32';
const isLinux = process.platform === 'linux';
const isDarwinArm64 = process.platform === 'darwin' && process.arch === 'arm64';
const isDarwinX64 = process.platform === 'darwin' && process.arch === 'x64';
const isLinuxArm64 = process.platform === 'linux' && process.arch === 'arm64';
const isCirrusCi = process.env['CIRRUS_CI'] === 'true';
const usesOctetStream =
isLinuxArm64 ||
isWin ||
isDarwinArm64 ||
(isCirrusCi && isLinux) ||
(isCirrusCi && isDarwinX64);
describe('FFI integration test for the HTTP Consumer API', () => {
setLogLevel('trace');
let port: number;
let pact: ConsumerPact;
const bytes: Buffer = zlib.gzipSync('this is an encoded string');
const like = (value: unknown) => ({
'pact:matcher:type': 'type',
value,
});
describe.skip('with JSON data', () => {
beforeEach(() => {
pact = makeConsumerPact(
'foo-consumer',
'bar-provider',
FfiSpecificationVersion['SPECIFICATION_VERSION_V3']
);
const interaction = pact.newInteraction('some description');
interaction.uponReceiving('a request to get a create with JSON data');
interaction.given('fido exists');
interaction.withRequest('POST', '/dogs/1234');
interaction.withRequestHeader('x-special-header', 0, 'header');
interaction.withQuery('someParam', 0, 'someValue');
interaction.withResponseBody(
JSON.stringify({
id: like(1234),
}),
'application/json'
);
interaction.withResponseBody(
JSON.stringify({
name: like('fido'),
age: like(23),
alive: like(true),
}),
'application/json'
);
interaction.withResponseHeader('x-special-response-header', 0, 'header');
interaction.withStatus(200);
port = pact.createMockServer(HOST);
});
it('generates a pact with success', () =>
axios
.request({
baseURL: `http://${HOST}:${port}`,
headers: {
'content-type': usesOctetStream
? 'application/octet-stream'
: 'application/gzip',
Accept: 'application/json',
'x-special-header': 'header',
},
params: {
someParam: 'someValue',
},
data: {
id: 1234,
},
method: 'POST',
url: '/dogs/1234',
})
.then((res) => {
expect(res.data).to.deep.equal({
name: 'fido',
age: 23,
alive: true,
});
})
.then(() => {
expect(pact.mockServerMatchedSuccessfully(port)).to.be.true;
})
.then(() => {
// You don't have to call this, it's just here to check it works
const mismatches = pact.mockServerMismatches(port);
expect(mismatches).to.have.length(0);
})
.then(() => {
pact.writePactFile(path.join(__dirname, '__testoutput__'));
})
.then(() => {
pact.cleanupMockServer(port);
}));
it('generates a pact with failure', () =>
axios
.request({
baseURL: `http://${HOST}:${port}`,
headers: {
Accept: 'application/json',
'x-special-header': 'WrongHeader',
},
params: {
someParam: 'wrongValue',
},
method: 'POST',
url: '/dogs/1234',
})
.then(
() => {
throw new Error(
'This call is not supposed to succeed during testing'
);
},
(err) => {
expect(err.message).to.equal('Request failed with status code 500');
}
)
.then(() => {
const mismatches = pact.mockServerMismatches(port);
const requestMismatches =
mismatches[0] as MatchingResultRequestMismatch;
expect(requestMismatches.type).to.equal('request-mismatch');
expect(requestMismatches.method).to.equal('POST');
expect(requestMismatches.path).to.equal('/dogs/1234');
expect(requestMismatches.mismatches).to.deep.include({
actual: 'wrongValue',
expected: 'someValue',
mismatch: "Expected 'someValue' to be equal to 'wrongValue'",
parameter: 'someParam',
type: 'QueryMismatch',
});
expect(requestMismatches.mismatches).to.deep.include({
actual: 'WrongHeader',
expected: 'header',
key: 'x-special-header',
mismatch:
"Mismatch with header 'x-special-header': Expected 'header' to be equal to 'WrongHeader'",
type: 'HeaderMismatch',
});
})
.then(() => {
// Yes, this writes the pact file.
// Yes, even though the tests have failed
pact.writePactFile(path.join(__dirname, '__testoutput__'));
})
.then(() => {
pact.cleanupMockServer(port);
}));
});
// See https://github.com/pact-foundation/pact-reference/issues/171 for why we have an OS switch here
// Windows: does not have magic mime matcher, uses content-type
// OSX arm64: does not magic mime matcher, uses content-type
// OSX x86_64: has magic mime matcher, sniffs content
// OSX x86_64 - CI GitHub Actions: has magic mime matcher, sniffs content
// Linux: has magic mime matcher, sniffs content
// Linux - CI Cirrus: does not have magic mime matcher, uses content-type
describe('with binary data', () => {
beforeEach(() => {
pact = makeConsumerPact(
'foo-consumer',
'bar-provider',
FfiSpecificationVersion['SPECIFICATION_VERSION_V3']
);
const interaction = pact.newInteraction('some description');
interaction.uponReceiving('a request to create a dog with binary data');
interaction.given('fido exists');
interaction.withRequest('POST', '/dogs/1234');
interaction.withRequestHeader('x-special-header', 0, 'header');
interaction.withQuery('someParam', 0, 'someValue');
interaction.withRequestBinaryBody(
bytes,
usesOctetStream ? 'application/octet-stream' : 'application/gzip'
);
interaction.withResponseBody(
JSON.stringify({
name: like('fido'),
age: like(23),
alive: like(true),
}),
'application/json'
);
interaction.withResponseHeader('x-special-response-header', 0, 'header');
interaction.withStatus(200);
port = pact.createMockServer(HOST);
});
// TODO: find out what's going on here. Suspect binary matching has changed in the core?
// See https://github.com/pact-foundation/pact-reference/issues/171
it('generates a pact with success', () =>
axios
.request({
baseURL: `http://${HOST}:${port}`,
headers: {
'content-type': usesOctetStream
? 'application/octet-stream'
: 'application/gzip',
Accept: 'application/json',
'x-special-header': 'header',
},
params: {
someParam: 'someValue',
},
data: bytes,
method: 'POST',
url: '/dogs/1234',
})
.then((res) => {
expect(res.data).to.deep.equal({
name: 'fido',
age: 23,
alive: true,
});
})
.then(() => {
expect(pact.mockServerMatchedSuccessfully(port)).to.be.true;
})
.then(() => {
// You don't have to call this, it's just here to check it works
const mismatches = pact.mockServerMismatches(port);
expect(mismatches).to.have.length(0);
})
.then(() => {
pact.writePactFile(path.join(__dirname, '__testoutput__'));
})
.then(() => {
pact.cleanupMockServer(port);
}));
});
// Should only run this if the plugin is installed
describe.skip('using a plugin (protobufs)', () => {
const protoFile = `${__dirname}/integration/plugin.proto`;
beforeEach(() => {
pact = makeConsumerPact(
'foo-consumer',
'bar-provider',
FfiSpecificationVersion['SPECIFICATION_VERSION_V3']
);
pact.addPlugin('protobuf', '0.1.14');
const interaction = pact.newInteraction('some description');
const protobufContents = {
'pact:proto': protoFile,
'pact:message-type': 'InitPluginRequest',
'pact:content-type': 'application/protobuf',
implementation: "notEmpty('pact-js-driver')",
version: "matching(semver, '0.0.0')",
};
interaction.uponReceiving('a request to get a protobuf');
interaction.given('protobuf state');
interaction.withRequest('GET', '/protobuf');
interaction.withPluginResponseInteractionContents(
'application/protobuf',
JSON.stringify(protobufContents)
);
interaction.withStatus(200);
port = pact.createMockServer(HOST);
});
afterEach(() => {
pact.cleanupPlugins();
});
it('generates a pact with success', async () => {
const root = await load(protoFile);
// Obtain a message type
const InitPluginRequest = root.lookupType(
'io.pact.plugin.InitPluginRequest'
);
return axios
.request({
baseURL: `http://${HOST}:${port}`,
method: 'GET',
url: '/protobuf',
responseType: 'arraybuffer',
})
.then((res) => {
const message: any = InitPluginRequest.decode(res.data);
expect(message.implementation).to.equal('pact-js-driver');
expect(message.version).to.equal('0.0.0');
})
.then(() => {
expect(pact.mockServerMatchedSuccessfully(port)).to.be.true;
})
.then(() => {
// You don't have to call this, it's just here to check it works
const mismatches = pact.mockServerMismatches(port);
expect(mismatches).to.have.length(0);
})
.then(() => {
pact.writePactFile(path.join(__dirname, '__testoutput__'));
})
.then(() => {
pact.cleanupMockServer(port);
});
});
});
describe('with multipart data', () => {
const form = new FormData();
const f: string = path.resolve(__dirname, './monkeypatch.rb');
form.append('my_file', fs.createReadStream(f));
const formHeaders = form.getHeaders();
beforeEach(() => {
const consumerPact = makeConsumerPact(
'foo-consumer',
'bar-provider',
FfiSpecificationVersion['SPECIFICATION_VERSION_V3']
);
const interaction = consumerPact.newInteraction('some description');
interaction.uponReceiving('a request to get a dog with multipart data');
interaction.given('fido exists');
interaction.withRequest('POST', '/dogs/1234');
interaction.withRequestHeader('x-special-header', 0, 'header');
interaction.withQuery('someParam', 0, 'someValue');
interaction.withRequestMultipartBody('text/plain', f, 'my_file');
interaction.withResponseBody(
JSON.stringify({
name: like('fido'),
age: like(23),
alive: like(true),
}),
'application/json'
);
interaction.withResponseHeader('x-special-header', 0, 'header');
interaction.withStatus(200);
port = consumerPact.createMockServer(HOST);
});
it('generates a pact with success', () =>
axios
.request({
baseURL: `http://${HOST}:${port}`,
headers: {
'Content-Type': 'multipart/form-data',
Accept: 'application/json',
'x-special-header': 'header',
...formHeaders,
},
params: {
someParam: 'someValue',
},
data: form,
method: 'POST',
url: '/dogs/1234',
})
.then((res) => {
expect(res.data).to.deep.equal({
name: 'fido',
age: 23,
alive: true,
});
})
.then(() => {
// You don't have to call this, it's just here to check it works
const mismatches = pact.mockServerMismatches(port);
console.dir(mismatches, { depth: 10 });
expect(mismatches).to.have.length(0);
})
.then(() => {
expect(pact.mockServerMatchedSuccessfully(port)).to.be.true;
})
.then(() => {
pact.writePactFile(path.join(__dirname, '__testoutput__'));
})
.then(() => {
pact.cleanupMockServer(port);
}));
});
});