Skip to content

Commit

Permalink
fix(tests): extract getRaw() helper, to allow fetching HTML pages (#468)
Browse files Browse the repository at this point in the history
## Problem

In order to write approval tests on the features covered by `userLibrary.js`, it would be nice if the `api-client` helper provided a method to fetch HTML pages too.

## Proposed solution

- extract `getRaw()`, to allow fetching HTML pages
- make `get()` use `getRaw()`
- fix, while we're at it: in case of JSON parsing error, pass that error to the callback instead of printing it to console and returning the unparsed body
  • Loading branch information
adrienjoly committed Aug 19, 2021
1 parent f8b5a31 commit bd730d9
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions test/api-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,27 @@ exports.signupAs = function signupAs(user, callback) {
};
// HTTP request wrappers

exports.get = function (jar, url, callback) {
exports.getRaw = function (jar, url, callback) {
request.get(
{ jar, url: `${URL_PREFIX}${url}` },
function (error, response, body) {
try {
body = JSON.parse(body);
} catch (e) {
console.error(e);
}
callback(error, { response, body, jar });
callback(error, { response, body });
}
);
};

exports.get = function (jar, url, callback) {
exports.getRaw(jar, url, function (error, { response, body }) {
let parsedBody = undefined;
try {
parsedBody = JSON.parse(body);
} catch (e) {
error = error | e;
}
callback(error, { response, body: parsedBody, jar });
});
};

// USER

/*
Expand Down

0 comments on commit bd730d9

Please sign in to comment.