Skip to content

Commit 061a7f8

Browse files
authored
Add Postman example from pact contract response (#8)
1 parent 115604a commit 061a7f8

File tree

4 files changed

+99
-3
lines changed

4 files changed

+99
-3
lines changed

app/parsers/2.0.0/pact-parser.js

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const httpStatus = require('http-status')
2+
13
class PactParser {
24
createBaseCollection(consumerName, providerName) {
35
return {
@@ -24,14 +26,17 @@ class PactParser {
2426
query: []
2527
}
2628
},
27-
response: []
29+
response: [],
30+
event: []
2831
};
2932
}
3033
interaction(interaction) {
3134
const item = this.createBaseItem(interaction);
3235
this.headers(interaction, item);
3336
this.query(interaction, item);
3437
this.body(interaction, item);
38+
this.response(interaction, item);
39+
this.event(interaction, item);
3540
return item;
3641
}
3742
headers(interaction, item) {
@@ -48,7 +53,7 @@ class PactParser {
4853
return {
4954
key: val[0],
5055
value: val[1]
51-
}
56+
};
5257
});
5358
}
5459
}
@@ -60,6 +65,47 @@ class PactParser {
6065
}
6166
}
6267
}
68+
response(interaction, item) {
69+
const response = {
70+
name: interaction.description,
71+
originalRequest: item.request,
72+
_postman_previewlanguage: 'json',
73+
header: null,
74+
cookie: [],
75+
};
76+
response.code = interaction.response.status;
77+
response.status = httpStatus[interaction.response.status];
78+
response.header = this.responseHeaders(interaction);
79+
if (interaction.response.body) {
80+
response.body = JSON.stringify(interaction.response.body);
81+
}
82+
item.response.push(response);
83+
}
84+
responseHeaders(interaction) {
85+
const headers = [];
86+
for (const key in interaction.response.headers) {
87+
headers.push({
88+
key,
89+
value: interaction.response.headers[key],
90+
type: 'text',
91+
});
92+
}
93+
return headers;
94+
}
95+
event(interaction, item) {
96+
const event = {
97+
listen: 'test',
98+
type: 'text/javascript',
99+
script: {
100+
exec: [
101+
`pm.test(\"Status code is ${interaction.response.status}\", function () {`,
102+
` pm.response.to.have.status(${interaction.response.status});`,
103+
`});`,
104+
]
105+
}
106+
}
107+
item.event.push(event)
108+
}
63109
parse(source) {
64110
this.output = this.createBaseCollection(source.consumer.name, source.provider.name);
65111
source.interactions.map(item => this.output.item.push(this.interaction(item)));

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"dependencies": {
3333
"axios": "0.19.0",
3434
"commander": "2.17.1",
35-
"debug": "3.1.0"
35+
"debug": "3.1.0",
36+
"http-status": "1.4.2"
3637
},
3738
"devDependencies": {
3839
"chai": "4.2.0",

tests/unit/app/parsers/2.0.0/pact-parser.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,48 @@ describe('pmpact > app > parsers > 2.0.0 > pact-parser', () => {
107107
]);
108108
});
109109

110+
it('should create a response', () => {
111+
const result = parser.parse(simplePactV2Json);
112+
assert.deepEqual(result.item[0].response, [{
113+
name: 'Interaction description',
114+
originalRequest: {
115+
method: 'GET',
116+
header: [
117+
{
118+
key: 'Accept',
119+
value: 'application/json'
120+
}
121+
],
122+
body: {},
123+
url: {
124+
raw: "{{url}}/path1/path2",
125+
host: ["{{url}}"],
126+
path: ['path1','path2'],
127+
query: [{key:'p1',value:'p1'},{key:'p2',value:'p2'}],
128+
}
129+
},
130+
_postman_previewlanguage: "json",
131+
header: [
132+
{
133+
key: "Access-Control-Allow-Methods",
134+
value: "*",
135+
type: "text"
136+
},
137+
{
138+
key: "Access-Control-Allow-Origin",
139+
value: "*",
140+
type: "text"
141+
},
142+
{
143+
key: "Content-Type",
144+
value: "application/json; charset=UTF-8",
145+
type: "text"
146+
}
147+
],
148+
cookie: [],
149+
code: 200,
150+
status: 'OK',
151+
body: '{"data":1}'
152+
}])
153+
})
110154
});

0 commit comments

Comments
 (0)