-
-
Notifications
You must be signed in to change notification settings - Fork 349
/
Copy pathpact.integration.spec.ts
164 lines (146 loc) · 4.52 KB
/
pact.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
/* tslint:disable:no-unused-expression no-empty */
import * as chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import sinonChai from 'sinon-chai';
import axios from 'axios';
import net = require('net');
process.env.ENABLE_FEATURE_V4 = 'true';
// eslint-disable-next-line import/first
import { PactV4 } from './v4';
const { expect } = chai;
chai.use(sinonChai);
chai.use(chaiAsPromised);
describe('V4 Pact', () => {
let pact: PactV4;
beforeEach(() => {
pact = new PactV4({
consumer: 'v4consumer',
provider: 'v4provider',
});
});
describe('HTTP req/res contract', () => {
it('generates a pact', () =>
pact
.addInteraction()
.given('some state')
.given('a second state')
.uponReceiving('a standard HTTP req/res')
.withRequest('POST', '/', (builder) => {
builder
.jsonBody({
foo: 'bar',
})
.headers({
'x-foo': 'x-bar',
});
})
.willRespondWith(200, (builder) => {
builder
.jsonBody({
foo: 'bar',
})
.headers({
'x-foo': 'x-bar',
});
})
.executeTest(async (server) =>
axios.post(
server.url,
{
foo: 'bar',
},
{
headers: {
'x-foo': 'x-bar',
},
}
)
));
});
describe('Plugin test', () => {
describe('Using the MATT plugin', () => {
const parseMattMessage = (raw: string): string =>
raw.replace(/(MATT)+/g, '').trim();
const generateMattMessage = (raw: string): string => `MATT${raw}MATT`;
describe('HTTP interface', () => {
it('generates a pact', async () => {
const mattRequest = `{"request": {"body": "hello"}}`;
const mattResponse = `{"response":{"body":"world"}}`;
await pact
.addInteraction()
.given('the Matt protocol exists')
.uponReceiving('an HTTP request to /matt')
.usingPlugin({
plugin: 'matt',
version: '0.1.1',
})
.withRequest('POST', '/matt', (builder) => {
builder.pluginContents('application/matt', mattRequest);
})
.willRespondWith(200, (builder) => {
builder.pluginContents('application/matt', mattResponse);
})
.executeTest((mockserver) =>
axios
.request({
baseURL: mockserver.url,
headers: {
'content-type': 'application/matt',
Accept: 'application/matt',
},
data: generateMattMessage('hello'),
method: 'POST',
url: '/matt',
})
.then((res) => {
expect(parseMattMessage(res.data)).to.eq('world');
})
);
});
});
describe('Synchronous Message (TCP) ', () => {
describe('with MATT protocol', () => {
const HOST = '127.0.0.1';
const sendMattMessageTCP = (
message: string,
host: string,
port: number
): Promise<string> => {
const socket = net.connect({
port,
host,
});
const res = socket.write(`${generateMattMessage(message)}\n`);
if (!res) {
throw Error('unable to connect to host');
}
return new Promise((resolve) => {
socket.on('data', (data) => {
resolve(parseMattMessage(data.toString()));
});
});
};
it('generates a pact', () => {
const mattMessage = `{"request": {"body": "hellotcp"}, "response":{"body":"tcpworld"}}`;
return pact
.addSynchronousInteraction('a MATT message')
.usingPlugin({
plugin: 'matt',
version: '0.1.1',
})
.withPluginContents(mattMessage, 'application/matt')
.startTransport('matt', HOST)
.executeTest(async (tc) => {
const message = await sendMattMessageTCP(
'hellotcp',
HOST,
tc.port
);
expect(message).to.eq('tcpworld');
});
});
});
});
});
});
});