-
Notifications
You must be signed in to change notification settings - Fork 29
/
mountebankSerialiser.js
95 lines (76 loc) · 2.36 KB
/
mountebankSerialiser.js
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
import * as fs from "fs";
const pacticipant = "pactflow-example-bi-directional-consumer-mountebank";
const provider = "pactflow-example-bi-directional-provider-dredd";
const filePath = `./pacts/${pacticipant}-${
process.env.PACT_PROVIDER || provider
}.json`;
const pjson = require("../package.json")
const defaultPact = {
consumer: { name: pacticipant },
provider: { name: process.env.PACT_PROVIDER || provider },
interactions: [],
metadata: {
pactSpecification: {
version: "2.0.0",
},
client: {
name: "pact-mountebank-adapter",
version: pjson.version,
},
},
};
// Read in the MB stubs, and convert to a Pact file
export const mbMatchesToPact = (imposters) => {
const pact = readPactFileOrDefault();
const matches = imposters.stubs.map((imposter) => {
return (imposter.matches || []).map((match) => ({
description: `mb_${match.request.method}_${match.request.path}_${match.response.statusCode}`,
request: {
method: match.request.method,
path: match.request.path,
body: match.request.body ? JSON.parse(match.request.body) : undefined,
query: match.request.query ? new URLSearchParams(match.request.query).toString() : undefined,
headers: match.request.headers,
},
response: {
status: match.response.statusCode,
headers: match.response.headers,
body: match.response.body ? JSON.parse(match.response.body) : undefined,
},
timestamp: match.timestamp, // use this to detect duplicates
}));
});
pact.interactions = [...pact.interactions, ...matches.flat()];
writePact(pact);
};
const removeDuplicates = (pact) => {
let timestamps = {};
pact.interactions = pact.interactions.reduce((acc, interaction) => {
if (!timestamps[interaction.timestamp]) acc.push(interaction)
timestamps[interaction.timestamp] = true
return acc
}, []);
return pact
};
const writePact = (pact) => {
createPactDir();
const cleanPact = removeDuplicates(pact);
fs.writeFileSync(filePath, JSON.stringify(cleanPact));
};
const createPactDir = () => {
try {
fs.mkdirSync("./pacts");
} catch {
// likely dir already exists
}
};
const readPactFileOrDefault = () => {
let pact = {};
try {
const res = fs.readFileSync(filePath);
pact = JSON.parse(res.toString("utf8"));
} catch {
pact = defaultPact;
}
return pact;
};