-
Notifications
You must be signed in to change notification settings - Fork 79
/
message.integration.spec.ts
208 lines (175 loc) · 6.67 KB
/
message.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
import chai = require('chai');
import path = require('path');
import chaiAsPromised = require('chai-as-promised');
import * as rimraf from 'rimraf';
import zlib = require('zlib');
import { load } from '@grpc/proto-loader';
import * as grpc from '@grpc/grpc-js';
import { ConsumerMessagePact, makeConsumerMessagePact } from '../src';
import { FfiSpecificationVersion } from '../src/ffi/types';
import { setLogLevel } from '../src/logger';
chai.use(chaiAsPromised);
const { expect } = chai;
const isWin = process.platform === 'win32';
const isLinux = process.platform === 'linux';
const isDarwinArm64 = process.platform === 'darwin' && process.arch === 'arm64';
const isLinuxArm64 = process.platform === 'linux' && process.arch === 'arm64';
const isCirrusCi = process.env['CIRRUS_CI'] === 'true';
const usesOctetStream =
isLinuxArm64 || isWin || isDarwinArm64 || (isCirrusCi && isLinux);
const getFeature = async (address: string, protoFile: string) => {
const def = await load(protoFile);
const { routeguide } = grpc.loadPackageDefinition(def);
const client = new routeguide['RouteGuide'](
address,
grpc.credentials.createInsecure()
);
return new Promise<any>((resolve, reject) => {
client.GetFeature(
{
latitude: 180,
longitude: 200,
},
(e: Error, feature: any) => {
if (e) {
reject(e);
} else {
resolve(feature);
}
}
);
});
};
describe('FFI integration test for the Message Consumer API', () => {
setLogLevel('error');
let pact: ConsumerMessagePact;
const secret = 'this is an encoded string';
const bytes: Buffer = zlib.gzipSync(secret);
before(() => {
rimraf.sync(path.join(__dirname, '__testoutput__', 'message-consumer*'));
});
beforeEach(() => {
pact = makeConsumerMessagePact(
'message-consumer',
'message-provider',
FfiSpecificationVersion['SPECIFICATION_VERSION_V4']
);
});
describe('Asynchronous Messages', () => {
describe('with JSON data', () => {
it('generates a pact with success', () => {
pact.addMetadata('pact-node', 'meta-key', 'meta-val');
const message = pact.newAsynchronousMessage('');
message.expectsToReceive('a product event');
message.given('some state');
message.givenWithParam('some state 2', 'state2 key', 'state2 val');
message.withContents(
JSON.stringify({ foo: 'bar' }),
'application/json'
);
message.withMetadata('meta-key', 'meta-val');
const reified = message.reifyMessage();
expect(JSON.parse(reified).contents.content).to.have.property(
'foo',
'bar'
);
pact.writePactFile(path.join(__dirname, '__testoutput__'));
});
});
// 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 on CI: does not magic mime matcher, uses content-type
// OSX: has magic mime matcher, sniffs content
// Linux: has magic mime matcher, sniffs content
describe('with binary data', () => {
it('generates a pact with success', () => {
const message = pact.newAsynchronousMessage('');
message.expectsToReceive('a binary event');
message.given('some state');
message.givenWithParam('some state 2', 'state2 key', 'state2 val');
message.withBinaryContents(
bytes,
usesOctetStream ? 'application/octet-stream' : 'application/gzip'
);
message.withMetadata('meta-key', 'meta-val');
const reified = message.reifyMessage();
const { contents } = JSON.parse(reified);
// Check the base64 encoded contents can be decoded, unzipped and equals the secret
const buf = Buffer.from(contents.content, 'base64');
const deflated = zlib.gunzipSync(buf).toString('utf8');
expect(deflated).to.equal(secret);
pact.writePactFile(path.join(__dirname, '__testoutput__'));
});
});
});
describe('Synchronous Messages', () => {
describe('with JSON data', () => {
it('generates a pact with success', () => {
pact.addMetadata('pact-node', 'meta-key', 'meta-val');
const message = pact.newSynchronousMessage('A synchronous message');
message.given('some state');
message.givenWithParam('some state 2', 'state2 key', 'state2 val');
message.withRequestContents(
JSON.stringify({ foo: 'bar' }),
'application/json'
);
message.withResponseContents(
JSON.stringify({ foo: 'bar' }),
'application/json'
);
message.withMetadata('meta-key', 'meta-val');
// const reified = message.reifyMessage();
// expect(JSON.parse(reified).contents).to.have.property('foo', 'bar');
pact.writePactFile(path.join(__dirname, '__testoutput__'));
});
});
describe.skip('with plugin contents (gRPC)', () => {
const protoFile = `${__dirname}/integration/grpc/route_guide.proto`;
let port: number;
afterEach(() => {
pact.cleanupPlugins();
});
beforeEach(() => {
const grpcInteraction = `{
"pact:proto": "${protoFile}",
"pact:proto-service": "RouteGuide/GetFeature",
"pact:content-type": "application/protobuf",
"request": {
"latitude": "matching(number, 180)",
"longitude": "matching(number, 200)"
},
"response": {
"name": "matching(type, 'Big Tree')",
"location": {
"latitude": "matching(number, 180)",
"longitude": "matching(number, 200)"
}
}
}`;
pact.addMetadata('pact-node', 'meta-key', 'meta-val');
pact.addPlugin('protobuf', '0.1.14');
const message = pact.newSynchronousMessage('a grpc test 1');
message.given('some state 1');
message.withPluginRequestResponseInteractionContents(
'application/protobuf',
grpcInteraction
);
message.withMetadata('meta-key 1', 'meta-val 2');
port = pact.pactffiCreateMockServerForTransport(
'127.0.0.1',
'grpc',
''
);
});
it('generates a pact with success', async () => {
const feature: any = await getFeature(`127.0.0.1:${port}`, protoFile);
expect(feature.name).to.eq('Big Tree');
const res = pact.mockServerMatchedSuccessfully(port);
expect(res).to.eq(true);
const mismatches = pact.mockServerMismatches(port);
expect(mismatches.length).to.eq(0);
pact.writePactFile(path.join(__dirname, '__testoutput__'));
});
});
});
});