UNPKG

@opra/testing

Version:
168 lines (167 loc) 6.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ApiExpect = void 0; const tslib_1 = require("tslib"); require("../expect-extend/index.js"); const common_1 = require("@opra/common"); const ansi_colors_1 = tslib_1.__importDefault(require("ansi-colors")); const expect_1 = require("expect"); const api_expect_base_js_1 = require("./api-expect-base.js"); const api_expect_collection_js_1 = require("./api-expect-collection.js"); const api_expect_error_js_1 = require("./api-expect-error.js"); const api_expect_object_js_1 = require("./api-expect-object.js"); const api_expect_operation_result_js_1 = require("./api-expect-operation-result.js"); class ApiExpect extends api_expect_base_js_1.ApiExpectBase { /** * Tests if request succeeded * @param status Status code number between 200-299 */ toSuccess(status) { let msg = ''; try { msg += `Status code do not match. `; if (status) { (0, expect_1.expect)(this.response.status).toEqual(status); } else { (0, expect_1.expect)(this.response.status).toBeGreaterThanOrEqual(200); (0, expect_1.expect)(this.response.status).toBeLessThan(400); } } catch (e) { e.message = "Request didn't succeeded as expected. " + msg + '\n\n' + e.message; const issues = this.response.body?.errors; if (issues) { e.message += '\n\n'; issues.forEach((issue, i) => { const stack = Array.isArray(issue.stack) ? issue.stack.join('\n') : issue.stack; e.message += ansi_colors_1.default.yellow(issues.length > 1 ? `Error [${i}]: ` : 'Error: ') + issue.message + '\n' + (stack ? ' ' + stack.substring(stack.indexOf('at ')) + '\n' : ''); }); } Error.captureStackTrace(e, this.toSuccess); throw e; } return this; } /** * Tests if request failed * @param status Status code number between 400-599 */ toFail(status) { let msg = ''; try { msg += `Status code do not match. `; if (status) { (0, expect_1.expect)(this.response.status).toEqual(status); } else { (0, expect_1.expect)(this.response.status).toBeGreaterThanOrEqual(400); (0, expect_1.expect)(this.response.status).toBeLessThanOrEqual(599); } } catch (e) { e.message = "Request didn't failed as expected. " + msg + '\n\n' + e.message; const issues = this.response.body?.errors; if (issues) { e.message += '\n\n'; issues.forEach((issue, i) => { const stack = Array.isArray(issue.stack) ? issue.stack.join('\n') : issue.stack; e.message += ansi_colors_1.default.yellow(issues.length > 1 ? `Error [${i}]: ` : 'Error: ') + issue.message + '\n' + (stack ? ' ' + stack.substring(stack.indexOf('at ')) + '\n' : ''); }); } Error.captureStackTrace(e, this.toSuccess); throw e; } return new api_expect_error_js_1.ApiExpectError(this.response); } /** * Tests if API returns a Collection */ toReturnCollection() { let msg = ''; try { msg = 'Content-Type header value is not valid. '; (0, expect_1.expect)(this.response.contentType).toEqual('application/opra.response+json'); msg = 'Type of response "body" is not valid. '; (0, expect_1.expect)(typeof this.response.body).toEqual('object'); msg = 'Type of "payload" is not an Array. '; const payload = this.response.body.payload; (0, expect_1.expect)(Array.isArray(payload) ? 'array' : typeof payload).toEqual('array'); } catch (e) { e.message = "Api didn't returned a Collection. " + msg + '\n\n' + e.message; if (msg) e.message = msg + '\n\n' + e.message; Error.captureStackTrace(e, this.toReturnCollection); throw e; } return new api_expect_collection_js_1.ApiExpectCollection(this.response); } /** * Tests if API returns an Object */ toReturnObject(contentType) { let msg = ''; try { msg = 'Content-Type header value is not valid. '; (0, expect_1.expect)(this.response.contentType).toEqual(contentType || common_1.MimeTypes.opra_response_json); msg = 'Type of response "body" is not valid. '; (0, expect_1.expect)(typeof this.response.body).toEqual('object'); msg = 'Type of "payload" is not an Object. '; const payload = this.response.body.payload; (0, expect_1.expect)(typeof payload).toEqual('object'); } catch (e) { e.message = "Api didn't returned an Object. " + msg + '\n\n' + e.message; if (msg) e.message = msg + '\n\n' + e.message; Error.captureStackTrace(e, this.toReturnCollection); throw e; } return new api_expect_object_js_1.ApiExpectObject(this.response); } /** * Tests if API returns an OperationResult */ toReturnOperationResult() { let msg = ''; try { msg = 'Content-Type header value is not valid. '; (0, expect_1.expect)(this.response.contentType).toEqual(common_1.MimeTypes.opra_response_json); msg = 'Type of response "body" is not valid. '; (0, expect_1.expect)(typeof this.response.body).toEqual('object'); msg = 'The response has payload. '; const payload = this.response.body.payload; (0, expect_1.expect)(typeof payload).toEqual('undefined'); } catch (e) { e.message = "Api didn't returned a OperationResult. " + msg + '\n\n' + e.message; if (msg) e.message = msg + '\n\n' + e.message; Error.captureStackTrace(e, this.toReturnCollection); throw e; } return new api_expect_operation_result_js_1.ApiExpectOperationResult(this.response); } } exports.ApiExpect = ApiExpect;