0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-23 22:27:34 -05:00
verdaccio/test/lib/request.js

128 lines
3 KiB
JavaScript
Raw Normal View History

// @flow
2018-06-22 18:03:25 -05:00
import _ from 'lodash';
import assert from 'assert';
import request from 'request';
import type {IRequestPromise} from '../types';
2017-06-30 16:11:12 -05:00
const requestData = Symbol('smart_request_data');
export class PromiseAssert extends Promise<any> implements IRequestPromise{
2017-06-30 16:11:12 -05:00
constructor(options: any) {
2017-06-30 16:11:12 -05:00
super(options);
}
status(expected: number) {
// $FlowFixMe
2017-06-30 16:11:12 -05: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;
}));
}
body_ok(expected: any) {
// $FlowFixMe
const selfData = this[requestData];
2017-06-30 16:11:12 -05: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) {
selfData.error.message = err.message;
throw selfData.error;
2017-06-30 16:11:12 -05:00
}
return body;
}));
}
body_error(expected: any) {
// $FlowFixMe
const selfData = this[requestData];
2017-06-30 16:11:12 -05:00
return injectResponse(this, this.then(function(body) {
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) {
selfData.error.message = err.message;
throw selfData.error;
2017-06-30 16:11:12 -05:00
}
return body;
}));
}
request(callback: any) {
// $FlowFixMe
2017-06-30 16:11:12 -05:00
callback(this[requestData].request);
return this;
}
response(cb: any) {
// $FlowFixMe
2017-06-30 16:11:12 -05:00
const selfData = this[requestData];
return injectResponse(this, this.then(function(body) {
cb(selfData.response);
return body;
}));
}
send(data: any) {
// $FlowFixMe
2017-06-30 16:11:12 -05:00
this[requestData].request.end(data);
return this;
}
2017-04-19 14:15:28 -05:00
}
function injectResponse(smartObject: any, promise: Promise<any>): Promise<any> {
// $FlowFixMe
2017-06-30 16:11:12 -05:00
promise[requestData] = smartObject[requestData];
2017-04-19 14:15:28 -05:00
return promise;
}
function smartRequest(options: any): Promise<any> {
const smartObject: any = {};
2017-06-30 16:11:12 -05:00
smartObject[requestData] = {};
smartObject[requestData].error = Error();
Error.captureStackTrace(smartObject[requestData].error, smartRequest);
const promiseResult: Promise<any> = new PromiseAssert(function(resolve, reject) {
// store request reference on symbol
2017-06-30 16:11:12 -05:00
smartObject[requestData].request = request(options, function(err, res, body) {
if (err) {
return reject(err);
}
2017-06-30 16:11:12 -05:00
// store the response on symbol
smartObject[requestData].response = res;
resolve(body);
});
});
return injectResponse(smartObject, promiseResult);
2017-06-30 16:11:12 -05:00
}
export default smartRequest;