2023-12-06 05:56:15 -05:00
|
|
|
import request from '@cypress/request';
|
2017-11-19 10:08:04 -05:00
|
|
|
import assert from 'assert';
|
2021-05-13 16:13:57 -05:00
|
|
|
import _ from 'lodash';
|
2017-11-19 10:08:04 -05:00
|
|
|
|
2022-01-09 14:51:50 -05:00
|
|
|
import { IRequestPromise } from '../types';
|
|
|
|
|
2017-06-30 16:11:12 -05:00
|
|
|
const requestData = Symbol('smart_request_data');
|
|
|
|
|
2021-03-14 02:42:46 -05:00
|
|
|
export class PromiseAssert extends Promise<any> implements IRequestPromise {
|
2019-07-16 01:40:01 -05:00
|
|
|
public constructor(options: any) {
|
2017-06-30 16:11:12 -05:00
|
|
|
super(options);
|
|
|
|
}
|
|
|
|
|
2019-07-16 01:40:01 -05:00
|
|
|
public status(expected: number) {
|
2017-06-30 16:11:12 -05:00
|
|
|
const selfData = this[requestData];
|
|
|
|
|
2021-03-14 02:42:46 -05:00
|
|
|
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-06-30 16:11:12 -05:00
|
|
|
}
|
|
|
|
|
2019-07-16 01:40:01 -05:00
|
|
|
public body_ok(expected: any) {
|
2017-11-19 10:08:04 -05:00
|
|
|
const selfData = this[requestData];
|
2017-06-30 16:11:12 -05:00
|
|
|
|
2021-03-14 02:42:46 -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
|
|
|
}
|
2019-07-16 01:40:01 -05:00
|
|
|
|
2021-03-14 02:42:46 -05:00
|
|
|
return body;
|
|
|
|
})
|
|
|
|
);
|
2017-06-30 16:11:12 -05:00
|
|
|
}
|
|
|
|
|
2019-07-16 01:40:01 -05:00
|
|
|
public body_error(expected: any) {
|
2017-11-19 10:08:04 -05:00
|
|
|
const selfData = this[requestData];
|
2017-06-30 16:11:12 -05:00
|
|
|
|
2021-03-14 02:42:46 -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
|
|
|
}
|
2021-03-14 02:42:46 -05:00
|
|
|
return body;
|
|
|
|
})
|
|
|
|
);
|
2017-06-30 16:11:12 -05:00
|
|
|
}
|
|
|
|
|
2019-07-16 01:40:01 -05:00
|
|
|
public request(callback: any) {
|
2017-06-30 16:11:12 -05:00
|
|
|
callback(this[requestData].request);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-07-16 01:40:01 -05:00
|
|
|
public response(cb: any) {
|
2017-06-30 16:11:12 -05:00
|
|
|
const selfData = this[requestData];
|
|
|
|
|
2021-03-14 02:42:46 -05:00
|
|
|
return injectResponse(
|
|
|
|
this,
|
|
|
|
this.then(function (body) {
|
|
|
|
cb(selfData.response);
|
|
|
|
return body;
|
|
|
|
})
|
|
|
|
);
|
2017-06-30 16:11:12 -05:00
|
|
|
}
|
|
|
|
|
2019-07-16 01:40:01 -05:00
|
|
|
public send(data: any) {
|
2017-06-30 16:11:12 -05:00
|
|
|
this[requestData].request.end(data);
|
|
|
|
return this;
|
|
|
|
}
|
2015-04-11 12:11:04 -05:00
|
|
|
}
|
|
|
|
|
2017-11-19 10:08:04 -05:00
|
|
|
function injectResponse(smartObject: any, promise: Promise<any>): Promise<any> {
|
2017-06-30 16:11:12 -05:00
|
|
|
promise[requestData] = smartObject[requestData];
|
2017-04-19 14:15:28 -05:00
|
|
|
return promise;
|
2015-04-11 12:11:04 -05:00
|
|
|
}
|
|
|
|
|
2017-11-19 10:08:04 -05:00
|
|
|
function smartRequest(options: any): Promise<any> {
|
|
|
|
const smartObject: any = {};
|
2017-06-30 16:11:12 -05:00
|
|
|
|
|
|
|
smartObject[requestData] = {};
|
|
|
|
smartObject[requestData].error = Error();
|
2017-11-19 10:08:04 -05:00
|
|
|
Error.captureStackTrace(smartObject[requestData].error, smartRequest);
|
|
|
|
|
2021-03-14 02:42:46 -05:00
|
|
|
const promiseResult: Promise<any> = new PromiseAssert(function (resolve, reject) {
|
2017-11-19 10:08:04 -05:00
|
|
|
// store request reference on symbol
|
2021-03-14 02:42:46 -05:00
|
|
|
smartObject[requestData].request = request(options, function (err, res, body) {
|
2017-06-30 16:11:12 -05:00
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
2015-04-11 12:11:04 -05:00
|
|
|
}
|
2017-06-30 16:11:12 -05:00
|
|
|
|
|
|
|
// store the response on symbol
|
|
|
|
smartObject[requestData].response = res;
|
|
|
|
resolve(body);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-11-19 10:08:04 -05:00
|
|
|
return injectResponse(smartObject, promiseResult);
|
2017-06-30 16:11:12 -05:00
|
|
|
}
|
|
|
|
|
2017-10-22 05:28:38 -05:00
|
|
|
export default smartRequest;
|