pmpact 0.4.13 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/app/app.js +8 -2
- package/package.json +2 -4
- package/tests/unit/app/app.js +48 -9
package/app/app.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
const fs = require('fs');
|
|
2
2
|
const path = require('path');
|
|
3
|
-
const axios = require('axios');
|
|
4
3
|
const debug = require('debug')('pmpact:app');
|
|
5
4
|
|
|
6
5
|
const isUrl = (value) => /^(?:\w+:)?\/\/(\S+)$/.test(value);
|
|
@@ -15,7 +14,14 @@ const getContent = async (source, headers) => {
|
|
|
15
14
|
if (headers) {
|
|
16
15
|
options.headers = JSON.parse(headers);
|
|
17
16
|
}
|
|
18
|
-
|
|
17
|
+
const response = await fetch(source, options);
|
|
18
|
+
if (!response.ok) {
|
|
19
|
+
const responseBody = await response.text();
|
|
20
|
+
const bodyMessage = responseBody ? ` - ${responseBody}` : '';
|
|
21
|
+
throw new Error(`Request failed with status ${response.status} ${response.statusText}${bodyMessage}`);
|
|
22
|
+
|
|
23
|
+
}
|
|
24
|
+
return await response.json();
|
|
19
25
|
}
|
|
20
26
|
else {
|
|
21
27
|
debug(`Require file: ${source}`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pmpact",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "A command line tool to convert Pact files to Postman collections.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -30,11 +30,9 @@
|
|
|
30
30
|
},
|
|
31
31
|
"homepage": "https://github.com/ITV/pmpact#readme",
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"axios": "1.15.0",
|
|
34
33
|
"commander": "4.1.1",
|
|
35
34
|
"debug": "4.3.4",
|
|
36
|
-
"http-status": "1.6.2"
|
|
37
|
-
"lodash": "4.18.1"
|
|
35
|
+
"http-status": "1.6.2"
|
|
38
36
|
},
|
|
39
37
|
"devDependencies": {
|
|
40
38
|
"execa": "5.1.1",
|
package/tests/unit/app/app.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { it, describe } from 'node:test';
|
|
1
|
+
import { it, describe, afterEach } from 'node:test';
|
|
2
2
|
import assert from 'node:assert/strict';
|
|
3
3
|
import simplePactV2Json from '../../fixtures/v2/simple-pact.json' with { type: 'json' };
|
|
4
4
|
import simplePactV3Json from '../../fixtures/v3/simple-pact.json' with { type: 'json' };
|
|
@@ -9,9 +9,12 @@ const proxyquire = require('proxyquire');
|
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
describe('pmpact > app', () => {
|
|
12
|
-
|
|
13
|
-
let axiosStub;
|
|
14
12
|
let fsStub;
|
|
13
|
+
let originalFetch;
|
|
14
|
+
|
|
15
|
+
afterEach(() => {
|
|
16
|
+
global.fetch = originalFetch;
|
|
17
|
+
});
|
|
15
18
|
|
|
16
19
|
const SIMPLE_PACT_URL_V2 = 'http://simple-pact-v2';
|
|
17
20
|
const SIMPLE_PACT_URL_V3 = 'http://simple-pact-v3';
|
|
@@ -21,18 +24,18 @@ describe('pmpact > app', () => {
|
|
|
21
24
|
};
|
|
22
25
|
|
|
23
26
|
const getApp = async () => {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
originalFetch = global.fetch;
|
|
28
|
+
global.fetch = (url) => {
|
|
29
|
+
let data;
|
|
30
|
+
if (url === SIMPLE_PACT_URL_V2) data = simplePactV2Json;
|
|
31
|
+
if (url === SIMPLE_PACT_URL_V3) data = simplePactV3Json;
|
|
32
|
+
return Promise.resolve({ ok: true, json: () => Promise.resolve(data) });
|
|
29
33
|
};
|
|
30
34
|
|
|
31
35
|
fsStub = {
|
|
32
36
|
writeFile: (path, data, opts, cb) => cb()
|
|
33
37
|
};
|
|
34
38
|
const Application = proxyquire('../../../app/app.js', {
|
|
35
|
-
'axios': axiosStub,
|
|
36
39
|
'fs': fsStub
|
|
37
40
|
});
|
|
38
41
|
return new Application();
|
|
@@ -129,4 +132,40 @@ describe('pmpact > app', () => {
|
|
|
129
132
|
assert.ok(err.message.indexOf('Invalid pact-parser version supplied') !== -1);
|
|
130
133
|
}
|
|
131
134
|
});
|
|
135
|
+
|
|
136
|
+
it('should throw an error with body message when url request fails with a response body', async () => {
|
|
137
|
+
originalFetch = global.fetch;
|
|
138
|
+
global.fetch = () => Promise.resolve({
|
|
139
|
+
ok: false,
|
|
140
|
+
status: 404,
|
|
141
|
+
statusText: 'Not Found',
|
|
142
|
+
text: () => Promise.resolve('Pact broker not found')
|
|
143
|
+
});
|
|
144
|
+
const Application = proxyquire('../../../app/app.js', { 'fs': fsStub });
|
|
145
|
+
const app = new Application();
|
|
146
|
+
try {
|
|
147
|
+
await app.parse('http://some-pact-broker/pact');
|
|
148
|
+
assert.ok(0, 'Should not resolve');
|
|
149
|
+
} catch (err) {
|
|
150
|
+
assert.ok(err.message.indexOf('Request failed with status 404 Not Found - Pact broker not found') !== -1);
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('should throw an error without body message when url request fails with an empty response body', async () => {
|
|
155
|
+
originalFetch = global.fetch;
|
|
156
|
+
global.fetch = () => Promise.resolve({
|
|
157
|
+
ok: false,
|
|
158
|
+
status: 500,
|
|
159
|
+
statusText: 'Internal Server Error',
|
|
160
|
+
text: () => Promise.resolve('')
|
|
161
|
+
});
|
|
162
|
+
const Application = proxyquire('../../../app/app.js', { 'fs': fsStub });
|
|
163
|
+
const app = new Application();
|
|
164
|
+
try {
|
|
165
|
+
await app.parse('http://some-pact-broker/pact');
|
|
166
|
+
assert.ok(0, 'Should not resolve');
|
|
167
|
+
} catch (err) {
|
|
168
|
+
assert.ok(err.message.indexOf('Request failed with status 500 Internal Server Error') !== -1);
|
|
169
|
+
}
|
|
170
|
+
});
|
|
132
171
|
});
|