Skip to content

Commit

Permalink
Add parse json body to response object
Browse files Browse the repository at this point in the history
`runAction` exposes a `Response` object in its callback.

The old `request` library had a slightly different interface than the
one for the `Response` object for `node-fetch`.

Specifically, both have `body` defined by `request` had `body` as the
parsed json body. Add the parsed json body to the `Response` object
returned by runAction. `body` has no `setter` and only a `getter` so a
new object needs to be created instead.
  • Loading branch information
PeterPan627 committed Jul 23, 2020
1 parent 06586d4 commit fddc1b5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/run_action.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,15 @@ function runAction(base, method, path, queryParams, bodyData, callback, numAttem
resp.json()
.then(function(body) {
var error = base._checkStatusForError(resp.status, body);
resp.statusCode = resp.status;
callback(error, resp, body);
// Ensure Response interface matches interface from
// `request` Response object
var r = {};
Object.keys(resp).forEach(function(property) {
r[property] = resp[property];
});
r.body = body;
r.statusCode = resp.status;
callback(error, r, body);
})
.catch(function() {
callback(base._checkStatusForError(resp.status));
Expand Down
15 changes: 15 additions & 0 deletions test/base.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,21 @@ describe('Base', function() {
});
});

it('exposes the body of the request directly on the response', function(done) {
testExpressApp.set('handler override', function(req, res) {
res.json({
fizz: 'buzz',
});
});

fakeBase.runAction('get', '/', {}, null, function(err, res, body) {
expect(err).toBeNull();
expect(res.body.fizz).toBe('buzz');
expect(body.fizz).toBe('buzz');
done();
});
});

it('makes requests GET requests with empty bodies', function(done) {
expect(version).toEqual(expect.stringMatching(/^\d+\.\d+\.\d+$/));

Expand Down

0 comments on commit fddc1b5

Please sign in to comment.