Skip to content

Commit

Permalink
REST helper update, fix asynchronicity to get response from APIs (#504)
Browse files Browse the repository at this point in the history
* REST helper update, fix asynchronicity to get response from APIs

* merge updates and update REST to work as sendPost sendGet
  • Loading branch information
Alan Trevino authored and DavertMik committed Jun 20, 2017
1 parent aa475ff commit fb32e70
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/command/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ let defaultConfig = {
mocha: {}
};

let helpers = ['WebDriverIO', 'Protractor', 'SeleniumWebdriver', 'Nightmare', 'FileSystem'];
let helpers = ['WebDriverIO', 'Protractor', 'SeleniumWebdriver', 'Nightmare', 'FileSystem', 'REST'];
let translations = Object.keys(require('../../translations'));
let noTranslation = 'English (no localization)';
translations.unshift(noTranslation);
Expand Down
85 changes: 85 additions & 0 deletions lib/helper/REST.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
'user strict';
const Helper = require('../helper');
const requireg = require('requireg');
const co = require('co');
let unirest = requireg('unirest');
let headers = '';
let payload = '';
let request;

class REST extends Helper {

constructor(config) {
super(config);
unirest = requireg('unirest');

this.options = {
timeout: 10000,
reset_headers: false,
endpoint: ''
};
Object.assign(this.options, config);
}

static _checkRequirements() {
try {
requireg("unirest");
} catch (e) {
return ["unirest"];
}
}

setRestTimeout(newTimeout) {
this.options.timeout = newTimeout;
}

haveRequestHeaders(customHeaders) {
if (!customHeaders) {
throw new Error('Cannot send empty headers.');
}
headers = customHeaders;
}

_cleanRequestHeaders() {
if (this.options.reset_headers) {
this.headers = {};
}
}
_url(url) {
return this.options.endpoint + url;
}

sendGet(url) {
request = unirest.get(this._url(url));
return this._executeRequest(request);
}

sendPost(url, payload = {}) {
request = unirest.post(this._url(url), payload);
return this._executeRequest(request);
}

_executeRequest(request) {
if (headers && 'auth' in headers) {
request.auth(headers.auth);
}
request.timeout(this.options.timeout);

return co(executeRequest(request)).then((response) => {
this._cleanRequestHeaders();
return response;
});
}
}
module.exports = REST;

function* executeRequest(request) {
return new Promise(function (resolve, reject) {
request
.headers(headers)
.send(payload)
.end(function (response) {
return resolve(response) || reject(response);
});
});
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@
"protractor": ">4.0.9 <6.0",
"selenium-webdriver": "^2.53.1",
"sinon": "^1.17.2",
"webdriverio": ">3.4.0 <4.7.1"
"unirest": "^0.5.1",
"webdriverio": ">3.4.0 <5.0.0"

},
"scripts": {
"prepublish": "gulp prepublish",
Expand Down

0 comments on commit fb32e70

Please sign in to comment.