-
-
Notifications
You must be signed in to change notification settings - Fork 349
/
messageProviderPact.ts
203 lines (174 loc) · 5.93 KB
/
messageProviderPact.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
/**
* @module Message
*/
import { omit, isEmpty } from 'lodash';
import serviceFactory, { VerifierOptions } from '@pact-foundation/pact-core';
import express from 'express';
import * as http from 'http';
import bodyParser from 'body-parser';
import { encode as encodeBase64 } from 'js-base64';
import { AddressInfo } from 'net';
import {
MessageDescriptor,
MessageFromProviderWithMetadata,
MessageProvider,
} from './dsl/message';
import logger, { setLogLevel } from './common/logger';
import { PactMessageProviderOptions } from './dsl/options';
// Listens for the server start event
// Converts event Emitter to a Promise
export const waitForServerReady = (server: http.Server): Promise<http.Server> =>
new Promise((resolve, reject) => {
server.on('listening', () => resolve(server));
server.on('error', () => reject());
});
// Get the Proxy we'll pass to the CLI for verification
export const setupProxyServer = (
app: (request: http.IncomingMessage, response: http.ServerResponse) => void
): http.Server => http.createServer(app).listen();
const hasMetadata = (
o: unknown | MessageFromProviderWithMetadata
): o is MessageFromProviderWithMetadata =>
Boolean((o as MessageFromProviderWithMetadata).__pactMessageMetadata);
export const providerWithMetadata =
(
provider: MessageProvider,
metadata: Record<string, string>
): MessageProvider =>
(descriptor: MessageDescriptor) =>
Promise.resolve(provider(descriptor)).then((message) =>
hasMetadata(message)
? {
__pactMessageMetadata: {
...message.__pactMessageMetadata,
...metadata,
},
message,
}
: { __pactMessageMetadata: metadata, message }
);
/**
* A Message Provider is analagous to Consumer in the HTTP Interaction model.
*
* It is the initiator of an interaction, and expects something on the other end
* of the interaction to respond - just in this case, not immediately.
*/
export class MessageProviderPact {
constructor(private config: PactMessageProviderOptions) {
if (config.logLevel && !isEmpty(config.logLevel)) {
serviceFactory.logLevel(config.logLevel);
setLogLevel(config.logLevel);
} else {
setLogLevel();
}
}
/**
* Verify a Message Provider.
*/
public verify(): Promise<unknown> {
logger.info('Verifying message');
// Start the verification CLI proxy server
const app = this.setupProxyApplication();
const server = setupProxyServer(app);
// Run the verification once the proxy server is available
return waitForServerReady(server)
.then(this.runProviderVerification())
.then(
(result) => {
server.close();
return result;
},
(err) => {
server.close();
throw err;
}
);
}
// Run the Verification CLI process
private runProviderVerification() {
return (server: http.Server) => {
const opts = {
...omit(this.config, 'handlers'),
...{
providerBaseUrl: `http://localhost:${
(server.address() as AddressInfo).port
}`,
},
} as VerifierOptions;
return serviceFactory.verifyPacts(opts);
};
}
// Get the API handler for the verification CLI process to invoke on POST /*
private setupVerificationHandler(): (
req: express.Request,
res: express.Response
) => void {
return (req, res) => {
const message: MessageDescriptor = req.body;
// Invoke the handler, and return the JSON response body
// wrapped in a Message
this.setupStates(message)
.then(() => this.findHandler(message))
.then((handler) => handler(message))
.then((messageFromHandler) => {
if (hasMetadata(messageFromHandler)) {
const metadata = encodeBase64(
JSON.stringify(messageFromHandler.__pactMessageMetadata)
);
res.header('Pact-Message-Metadata', metadata);
res.header('PACT_MESSAGE_METADATA', metadata);
return res.json(messageFromHandler.message);
}
return res.json(messageFromHandler);
})
.catch((e) => res.status(500).send(e));
};
}
// Get the Express app that will run on the HTTP Proxy
private setupProxyApplication(): express.Express {
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use((req, res, next) => {
// TODO: this seems to override the metadata for content-type
res.header('Content-Type', 'application/json; charset=utf-8');
next();
});
// Proxy server will respond to Verifier process
app.all('/*', this.setupVerificationHandler());
return app;
}
// Lookup the handler based on the description, or get the default handler
private setupStates(message: MessageDescriptor): Promise<unknown> {
const promises: Array<Promise<unknown>> = [];
if (message.providerStates) {
message.providerStates.forEach((state) => {
const handler = this.config.stateHandlers
? this.config.stateHandlers[state.name]
: null;
if (handler) {
promises.push(handler(state.name, state.params));
} else {
logger.warn(`no state handler found for "${state.name}", ignoring`);
}
});
}
return Promise.all(promises);
}
// Lookup the handler based on the description, or get the default handler
private findHandler(message: MessageDescriptor): Promise<MessageProvider> {
const handler = this.config.messageProviders
? this.config.messageProviders[message.description || '']
: undefined;
if (!handler) {
logger.error(`no handler found for message ${message.description}`);
return Promise.reject(
new Error(
`No handler found for message "${message.description}".
Check your "handlers" configuration`
)
);
}
return Promise.resolve(handler);
}
}