fluentrest-ts
Version:
A lightweight, fluent TypeScript API testing library inspired by Java's RestAssured. Built on top of Axios, JSONPath, and Joi for powerful request handling and response validation.
105 lines (104 loc) • 4.72 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.expectStatus = expectStatus;
exports.expectBody = expectBody;
exports.expectHeader = expectHeader;
exports.expectBodyContains = expectBodyContains;
exports.validateBody = validateBody;
const chalk_1 = __importDefault(require("chalk"));
const jsonpath_plus_1 = require("jsonpath-plus");
const logger_1 = require("../core/logger");
/**
* Asserts that the response status code matches the expected status.
*/
function expectStatus(response, status, logLevel, logToFile) {
try {
if (response.status !== status) {
throw new Error(`Expected status ${status}, got ${response.status}`);
}
if (logLevel !== "none")
console.log(chalk_1.default.green(`✔ Status ${status} as expected.`));
}
catch (error) {
const formatted = (0, logger_1.formatError)("Status assertion failed", error, response.data, "ERR_ASSERTION_STATUS");
(0, logger_1.logError)(error, "Status Assertion Error", logLevel, logToFile, response.data, "ERR_ASSERTION_STATUS");
throw new Error(formatted);
}
}
/**
* Asserts that a value at a JSONPath in the body matches the expected value.
*/
function expectBody(response, path, expected, logLevel, logToFile) {
try {
const results = (0, jsonpath_plus_1.JSONPath)({ path, json: response.data });
const actual = results[0];
if (!results.length || actual !== expected) {
throw new Error(`Expected value at '${path}' to be '${expected}', got '${actual}'`);
}
if (logLevel !== "none")
console.log(chalk_1.default.green(`✔ Body value at '${path}' is '${expected}' as expected.`));
}
catch (error) {
const formatted = (0, logger_1.formatError)("Body assertion failed", error, response.data, "ERR_ASSERTION_BODY");
(0, logger_1.logError)(error, "Body Assertion Error", logLevel, logToFile, response.data, "ERR_ASSERTION_BODY");
throw new Error(formatted);
}
}
/**
* Asserts that a response header matches the expected value.
*/
function expectHeader(response, headerKey, expectedValue, logLevel, logToFile) {
try {
const actual = response.headers?.[headerKey.toLowerCase()];
if (actual !== expectedValue) {
throw new Error(`Expected header '${headerKey}' to be '${expectedValue}', got '${actual}'`);
}
if (logLevel !== "none")
console.log(chalk_1.default.green(`✔ Header '${headerKey}' is '${expectedValue}' as expected.`));
}
catch (error) {
const formatted = (0, logger_1.formatError)("Header assertion failed", error, response.headers, "ERR_ASSERTION_HEADER");
(0, logger_1.logError)(error, "Header Assertion Error", logLevel, logToFile, response.headers, "ERR_ASSERTION_HEADER");
throw new Error(formatted);
}
}
/**
* Asserts that the response body contains the specified key-value fragment.
*/
function expectBodyContains(response, fragment, logLevel, logToFile) {
try {
const responseBody = JSON.stringify(response.data);
const matches = Object.entries(fragment).every(([key, value]) => responseBody.includes(`"${key}":${JSON.stringify(value)}`));
if (!matches) {
throw new Error(`Expected body to contain fragment: ${JSON.stringify(fragment)}`);
}
if (logLevel !== "none")
console.log(chalk_1.default.green(`✔ Body contains expected fragment.`));
}
catch (error) {
const formatted = (0, logger_1.formatError)("Body fragment check failed", error, response.data, "ERR_ASSERTION_FRAGMENT");
(0, logger_1.logError)(error, "Body Fragment Assertion Error", logLevel, logToFile, response.data, "ERR_ASSERTION_FRAGMENT");
throw new Error(formatted);
}
}
/**
* Validates the full response body against a Joi schema.
*/
function validateBody(response, schema, logLevel, logToFile) {
try {
const { error } = schema.validate(response.data);
if (error) {
throw new Error(`Schema validation failed: ${error.message}`);
}
if (logLevel !== "none")
console.log(chalk_1.default.green(`✔ Schema validation passed.`));
}
catch (error) {
const formatted = (0, logger_1.formatError)("Schema validation failed", error, response.data, "ERR_VALIDATION_SCHEMA");
(0, logger_1.logError)(error, "Schema Validation Error", logLevel, logToFile, response.data, "ERR_VALIDATION_SCHEMA");
throw new Error(formatted);
}
}