Skip to content

Commit

Permalink
fix(response content-type checking): more robust JSON response hand…
Browse files Browse the repository at this point in the history
…ling (#89)

Use `json: true` with request: request has more robust `content-type` checking.
  • Loading branch information
silasbw committed Mar 3, 2017
1 parent f836101 commit ace816e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
6 changes: 2 additions & 4 deletions lib/api-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ class ApiGroup {
const requestOptions = Object.assign({
method: method || 'GET',
uri: this._url(options.path),
body: options.body && JSON.stringify(options.body),
body: options.body,
json: true,
qs: options.qs,
headers: options.headers
}, this.requestOptions);
Expand All @@ -97,9 +98,6 @@ class ApiGroup {

return request(requestOptions, (err, res, body) => {
if (err) return cb(err);
if (res.headers['content-type'] === 'application/json') {
body = JSON.parse(body);
}
cb(null, { statusCode: res.statusCode, body: body });
});
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"dependencies": {
"async": "^2.0.0-rc.6",
"js-yaml": "^3.7.0",
"request": "^2.72.0"
"request": "^2.79.0"
},
"devDependencies": {
"assume": "^1.4.1",
Expand Down
40 changes: 40 additions & 0 deletions test/pods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ const testPod = {
}
};

const testPatch = {
metadata: {
name: 'test-pod'
},
spec: {
containers: [
{
image: 'still-does-not-matter:latest',
name: 'test'
}
]
}
};

describe('lib.pods', () => {

describe('.post', () => {
Expand All @@ -45,6 +59,32 @@ describe('lib.pods', () => {
});
});

describe('.patch', () => {
beforeTesting('int', done => {
common.changeName(err => {
assume(err).is.falsy();
common.api.ns.pods.post({ body: testPod }, (postErr, pod) => {
assume(postErr).is.falsy();
done();
});
});
});
beforeTestingEach('unit', () => {
nock(common.api.url)
.patch(`/api/v1/namespaces/${ common.currentName }/pods/test-pod`)
.reply(200, Object.assign({ kind: 'Pod' }, testPatch));
});

it('succeeds at updating a pod', done => {
common.api.ns.pods('test-pod').patch({ body: testPatch }, (err, pod) => {
assume(err).is.falsy();
assume(pod.metadata.name).is.equal('test-pod');
assume(pod.spec.containers[0].image).is.equal('still-does-not-matter:latest');
done();
});
});
});

describe('.get', () => {
beforeTesting('int', done => {
common.changeName(err => {
Expand Down

0 comments on commit ace816e

Please sign in to comment.