2017-11-19 16:08:04 +01:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import assert from 'assert';
|
|
|
|
import request from 'request';
|
|
|
|
import _ from 'lodash';
|
|
|
|
import type {IRequestPromise} from './types';
|
|
|
|
|
2017-06-30 23:11:12 +02:00
|
|
|
const requestData = Symbol('smart_request_data');
|
|
|
|
|
2017-11-19 16:08:04 +01:00
|
|
|
export class PromiseAssert extends Promise<any> implements IRequestPromise{
|
2017-06-30 23:11:12 +02:00
|
|
|
|
2017-11-19 16:08:04 +01:00
|
|
|
constructor(options: any) {
|
2017-06-30 23:11:12 +02:00
|
|
|
super(options);
|
|
|
|
}
|
|
|
|
|
2017-11-19 16:08:04 +01:00
|
|
|
status(expected: number) {
|
|
|
|
// $FlowFixMe
|
2017-06-30 23:11:12 +02:00
|
|
|
const selfData = this[requestData];
|
|
|
|
|
|
|
|
return injectResponse(this, this.then(function(body) {
|
|
|
|
try {
|
|
|
|
assert.equal(selfData.response.statusCode, expected);
|
|
|
|
} catch(err) {
|
|
|
|
selfData.error.message = err.message;
|
|
|
|
throw selfData.error;
|
|
|
|
}
|
|
|
|
return body;
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2017-11-19 16:08:04 +01:00
|
|
|
body_ok(expected: any) {
|
|
|
|
// $FlowFixMe
|
|
|
|
const selfData = this[requestData];
|
2017-06-30 23:11:12 +02:00
|
|
|
|
|
|
|
return injectResponse(this, this.then(function(body) {
|
|
|
|
try {
|
|
|
|
if (_.isRegExp(expected)) {
|
|
|
|
assert(body.ok.match(expected), '\'' + body.ok + '\' doesn\'t match ' + expected);
|
|
|
|
} else {
|
|
|
|
assert.equal(body.ok, expected);
|
|
|
|
}
|
|
|
|
assert.equal(body.error, null);
|
|
|
|
} catch(err) {
|
2017-11-19 16:08:04 +01:00
|
|
|
selfData.error.message = err.message;
|
|
|
|
throw selfData.error;
|
2017-06-30 23:11:12 +02:00
|
|
|
}
|
|
|
|
return body;
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-19 16:08:04 +01:00
|
|
|
body_error(expected: any) {
|
|
|
|
// $FlowFixMe
|
|
|
|
const selfData = this[requestData];
|
2017-06-30 23:11:12 +02:00
|
|
|
|
|
|
|
return injectResponse(this, this.then(function(body) {
|
2017-12-02 01:50:09 +01:00
|
|
|
// console.log("======>smartRequest body_error://", body);
|
2017-06-30 23:11:12 +02:00
|
|
|
try {
|
|
|
|
if (_.isRegExp(expected)) {
|
|
|
|
assert(body.error.match(expected), body.error + ' doesn\'t match ' + expected);
|
|
|
|
} else {
|
|
|
|
assert.equal(body.error, expected);
|
|
|
|
}
|
|
|
|
assert.equal(body.ok, null);
|
|
|
|
} catch(err) {
|
2017-11-19 16:08:04 +01:00
|
|
|
selfData.error.message = err.message;
|
|
|
|
throw selfData.error;
|
2017-06-30 23:11:12 +02:00
|
|
|
}
|
|
|
|
return body;
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2017-11-19 16:08:04 +01:00
|
|
|
request(callback: any) {
|
|
|
|
// $FlowFixMe
|
2017-06-30 23:11:12 +02:00
|
|
|
callback(this[requestData].request);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2017-11-19 16:08:04 +01:00
|
|
|
response(cb: any) {
|
|
|
|
// $FlowFixMe
|
2017-06-30 23:11:12 +02:00
|
|
|
const selfData = this[requestData];
|
|
|
|
|
|
|
|
return injectResponse(this, this.then(function(body) {
|
|
|
|
cb(selfData.response);
|
|
|
|
return body;
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2017-11-19 16:08:04 +01:00
|
|
|
send(data: any) {
|
|
|
|
// $FlowFixMe
|
2017-06-30 23:11:12 +02:00
|
|
|
this[requestData].request.end(data);
|
|
|
|
return this;
|
|
|
|
}
|
2017-04-19 21:15:28 +02:00
|
|
|
|
2015-04-11 19:11:04 +02:00
|
|
|
}
|
|
|
|
|
2017-11-19 16:08:04 +01:00
|
|
|
function injectResponse(smartObject: any, promise: Promise<any>): Promise<any> {
|
|
|
|
// $FlowFixMe
|
2017-06-30 23:11:12 +02:00
|
|
|
promise[requestData] = smartObject[requestData];
|
2017-04-19 21:15:28 +02:00
|
|
|
return promise;
|
2015-04-11 19:11:04 +02:00
|
|
|
}
|
|
|
|
|
2017-11-19 16:08:04 +01:00
|
|
|
|
|
|
|
function smartRequest(options: any): Promise<any> {
|
|
|
|
const smartObject: any = {};
|
2017-06-30 23:11:12 +02:00
|
|
|
|
|
|
|
smartObject[requestData] = {};
|
|
|
|
smartObject[requestData].error = Error();
|
2017-11-19 16:08:04 +01:00
|
|
|
Error.captureStackTrace(smartObject[requestData].error, smartRequest);
|
|
|
|
|
|
|
|
const promiseResult: Promise<any> = new PromiseAssert(function(resolve, reject) {
|
|
|
|
// store request reference on symbol
|
2017-06-30 23:11:12 +02:00
|
|
|
smartObject[requestData].request = request(options, function(err, res, body) {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
2015-04-11 19:11:04 +02:00
|
|
|
}
|
2017-06-30 23:11:12 +02:00
|
|
|
|
|
|
|
// store the response on symbol
|
|
|
|
smartObject[requestData].response = res;
|
2017-12-02 01:50:09 +01:00
|
|
|
// console.log("======>smartRequest RESPONSE: ", body);
|
2017-06-30 23:11:12 +02:00
|
|
|
resolve(body);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-11-19 16:08:04 +01:00
|
|
|
return injectResponse(smartObject, promiseResult);
|
2017-06-30 23:11:12 +02:00
|
|
|
}
|
|
|
|
|
2017-10-22 12:28:38 +02:00
|
|
|
export default smartRequest;
|
2015-04-11 19:11:04 +02:00
|
|
|
|