@microsoft.azure/autorest.testserver
Version:
Autorest test server.
124 lines • 5.78 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateQueryParam = exports.validateHeader = exports.validateXMLBodyEquals = exports.validateBodyNotEmpty = exports.validateBodyEmpty = exports.validateCoercedDateBodyEquals = exports.validateBodyEquals = exports.validateRawBodyEquals = exports.BODY_NOT_EMPTY_ERROR_MESSAGE = exports.BODY_EMPTY_ERROR_MESSAGE = exports.BODY_NOT_EQUAL_ERROR_MESSAGE = void 0;
const deep_equal_1 = __importDefault(require("deep-equal"));
const xml2js_1 = require("xml2js");
const validation_error_1 = require("./validation-error");
exports.BODY_NOT_EQUAL_ERROR_MESSAGE = "Body provided doesn't match expected body";
exports.BODY_EMPTY_ERROR_MESSAGE = "Body should exists";
exports.BODY_NOT_EMPTY_ERROR_MESSAGE = "Body should be empty";
const validateRawBodyEquals = (request, expectedRawBody) => {
const actualRawBody = request.rawBody;
if (expectedRawBody == null) {
if (!isBodyEmpty(actualRawBody)) {
throw new validation_error_1.ValidationError(exports.BODY_NOT_EQUAL_ERROR_MESSAGE, expectedRawBody, actualRawBody);
}
return;
}
if (actualRawBody !== expectedRawBody) {
throw new validation_error_1.ValidationError(exports.BODY_NOT_EQUAL_ERROR_MESSAGE, expectedRawBody, actualRawBody);
}
};
exports.validateRawBodyEquals = validateRawBodyEquals;
const validateBodyEquals = (request, expectedBody) => {
if (expectedBody == null) {
if (!isBodyEmpty(request.rawBody)) {
throw new validation_error_1.ValidationError(exports.BODY_NOT_EQUAL_ERROR_MESSAGE, expectedBody, request.rawBody);
}
return;
}
if (!(0, deep_equal_1.default)(request.body, expectedBody, { strict: true })) {
throw new validation_error_1.ValidationError(exports.BODY_NOT_EQUAL_ERROR_MESSAGE, expectedBody, request.body);
}
};
exports.validateBodyEquals = validateBodyEquals;
const validateCoercedDateBodyEquals = (request, expectedBody) => {
if (expectedBody == null) {
if (!isBodyEmpty(request.rawBody)) {
throw new validation_error_1.ValidationError(exports.BODY_NOT_EQUAL_ERROR_MESSAGE, expectedBody, request.rawBody);
}
return;
}
if (!(0, deep_equal_1.default)(coerceDate(request.body), expectedBody, { strict: true })) {
throw new validation_error_1.ValidationError(exports.BODY_NOT_EQUAL_ERROR_MESSAGE, expectedBody, request.body);
}
};
exports.validateCoercedDateBodyEquals = validateCoercedDateBodyEquals;
const validateBodyEmpty = (request) => {
if (isBodyEmpty(request.rawBody)) {
if (request.body instanceof Buffer) {
if (request.body.length > 0) {
throw new validation_error_1.ValidationError(exports.BODY_NOT_EMPTY_ERROR_MESSAGE, undefined, request.rawBody);
}
}
}
else {
throw new validation_error_1.ValidationError(exports.BODY_EMPTY_ERROR_MESSAGE, undefined, request.rawBody);
}
};
exports.validateBodyEmpty = validateBodyEmpty;
const validateBodyNotEmpty = (request) => {
if (isBodyEmpty(request.rawBody)) {
if (request.body instanceof Buffer) {
if (request.body.length === 0) {
throw new validation_error_1.ValidationError(exports.BODY_EMPTY_ERROR_MESSAGE, undefined, request.rawBody);
}
}
else {
throw new validation_error_1.ValidationError(exports.BODY_EMPTY_ERROR_MESSAGE, undefined, request.rawBody);
}
}
};
exports.validateBodyNotEmpty = validateBodyNotEmpty;
/**
* Check if the provided body is empty.
* @param body express.js request body.
*/
const isBodyEmpty = (body) => {
return body == null || body === "";
};
const coerceDateXml = (xml) => {
return xml.replace(/(\d\d\d\d-\d\d-\d\d[Tt]\d\d:\d\d:\d\d\.\d\d\d)\d{0,4}([Zz]|[+-]00:00)/g, "$1Z");
};
const coerceDate = (targetObject) => {
let stringRep = JSON.stringify(targetObject);
stringRep = stringRep.replace(/(\d\d\d\d-\d\d-\d\d[Tt]\d\d:\d\d:\d\d)\.\d{3,7}([Zz]|[+-]00:00)/g, "$1Z");
return JSON.parse(stringRep);
};
/**
* Check whether the XML request body is matching the given xml.
*/
const validateXMLBodyEquals = async (request, expectedBody) => {
const rawBody = request.body;
const actualBody = coerceDateXml(rawBody);
const actualParsedBody = await (0, xml2js_1.parseStringPromise)(actualBody);
const expectedParsedBody = await (0, xml2js_1.parseStringPromise)(expectedBody);
if (!(0, deep_equal_1.default)(actualParsedBody, expectedParsedBody, { strict: true })) {
throw new validation_error_1.ValidationError(exports.BODY_NOT_EQUAL_ERROR_MESSAGE, expectedParsedBody, actualParsedBody);
}
};
exports.validateXMLBodyEquals = validateXMLBodyEquals;
/**
* Check whether the request header contains the given name/value pair
*/
const validateHeader = (request, headerName, expected) => {
const actual = request.headers[headerName];
if (actual !== expected) {
throw new validation_error_1.ValidationError(`Expected ${expected} but got ${actual}`, expected, actual);
}
};
exports.validateHeader = validateHeader;
/**
* Check whether the query string contains the given parameter name and value.
*/
const validateQueryParam = (request, paramName, expected) => {
const actual = request.query[paramName];
if (actual !== expected) {
throw new validation_error_1.ValidationError(`Expected query param ${paramName}=${expected} but got ${actual}`, expected, actual);
}
};
exports.validateQueryParam = validateQueryParam;
//# sourceMappingURL=request-validations.js.map