Skip to content

Commit f33e5aa

Browse files
alex-thewsey-ibmnfriedly
authored andcommitted
[discovery] createEnv with size 0 for Issue watson-developer-cloud#461 (watson-developer-cloud#463)
Discovery createEnv with size 0 Fixes watson-developer-cloud#461 [semver patch]
1 parent c67debc commit f33e5aa

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

discovery/v1.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ DiscoveryV1.prototype.getEnvironments = function(params, callback) {
8080
DiscoveryV1.prototype.createEnvironment = function(params, callback) {
8181
params = params || {};
8282

83-
// size is an int of 1,2,3, default 1
84-
if (!params.size) {
83+
// size is an int of 0,1,2,3, default 1
84+
if (typeof params.size === 'undefined' || params.size === null) {
8585
params.size = 1;
8686
}
8787

test/unit/test.discovery.v1.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,33 @@ describe('discovery-v1', function() {
111111
assert.equal(req.method, 'POST');
112112
});
113113

114+
it('should create an environment with size 1 by default', function() {
115+
const req = discovery.createEnvironment(
116+
{
117+
name: 'new environment',
118+
description: 'my description'
119+
},
120+
noop
121+
);
122+
const jsonBodyParts = readMultipartReqJsons(req);
123+
assert.equal(jsonBodyParts.length, 1);
124+
assert.equal(jsonBodyParts[0].size, 1);
125+
});
126+
127+
it('should create an environment with size 0', function() {
128+
const req = discovery.createEnvironment(
129+
{
130+
name: 'new environment',
131+
description: 'my description',
132+
size: 0
133+
},
134+
noop
135+
);
136+
const jsonBodyParts = readMultipartReqJsons(req);
137+
assert.equal(jsonBodyParts.length, 1);
138+
assert.equal(jsonBodyParts[0].size, 0);
139+
});
140+
114141
it('should update an environment', function() {
115142
const req = discovery.updateEnvironment(
116143
{
@@ -265,6 +292,26 @@ describe('discovery-v1', function() {
265292
);
266293
assert.equal(req.method, 'GET');
267294
});
295+
296+
/**
297+
* Return an array of parsed objects representing all valid JSON parts of a multipart request.
298+
* @param {*} req
299+
* @return {Array}
300+
*/
301+
function readMultipartReqJsons(req) {
302+
const result = [];
303+
if (req && req.body && req.body.length) {
304+
req.body.forEach(part => {
305+
try {
306+
result.push(JSON.parse(Buffer.from(part).toString('ascii')));
307+
} catch (err) {
308+
// JSON parse error -> this part is not JSON: skip.
309+
}
310+
});
311+
}
312+
313+
return result;
314+
}
268315
});
269316
});
270317
});

0 commit comments

Comments
 (0)