ng-apimock-with-presets
Version:
An ng-apimock fork with preset functionality
148 lines • 6.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var http = require("http");
var fs = require("fs-extra");
var url = require("url");
var http_1 = require("./http");
var NgApimockHandler = (function () {
function NgApimockHandler() {
this.MAX_RECORDINGS_PER_MOCK = 2;
}
NgApimockHandler.prototype.handleRequest = function (request, response, next, registry, ngApimockId) {
var _this = this;
var match = this.getMatchingMock(registry.mocks, request.url, request.method);
var payload;
if (match) {
var selection = this.getSelection(registry, match.identifier, ngApimockId);
var variables_1 = this.getVariables(registry, ngApimockId);
var mockResponse_1 = match.responses[selection];
var requestDataChunks_1 = [];
request.on('data', function (rawData) {
requestDataChunks_1.push(rawData);
});
if (mockResponse_1 !== undefined) {
request.on('end', function () {
payload = Buffer.concat(requestDataChunks_1).toString();
if (_this.getEcho(registry, match.identifier, ngApimockId)) {
console.log(match.method + ' request made on \'' + match.expression + '\' with payload: ', payload);
}
var statusCode = mockResponse_1.status || 200;
var jsonCallbackName = _this.getJsonCallbackName(request.url);
var headers;
var chunk;
if (_this.isBinaryResponse(mockResponse_1)) {
headers = mockResponse_1.headers || http_1.httpHeaders.CONTENT_TYPE_BINARY;
chunk = fs.readFileSync(mockResponse_1.file);
}
else {
headers = mockResponse_1.headers || http_1.httpHeaders.CONTENT_TYPE_APPLICATION_JSON;
chunk = _this.updateData(mockResponse_1.data, variables_1, (match.isArray ? [] : {}));
}
if (jsonCallbackName !== false) {
chunk = jsonCallbackName + '(' + chunk + ')';
}
var mockDelay = _this.getDelay(registry, match.identifier, ngApimockId);
var _delay = mockDelay === null || mockDelay === undefined ? mockResponse_1.delay : mockDelay;
var sendResponse = function () {
response.writeHead(statusCode, headers);
response.end(chunk);
if (registry.record) {
_this.storeRecording(payload, chunk, request, statusCode, registry, match.identifier);
}
};
if (_delay) {
setTimeout(sendResponse, _delay);
}
else {
sendResponse();
}
});
}
else {
if (registry.record && !request.headers.record) {
request.on('end', function () {
payload = Buffer.concat(requestDataChunks_1).toString();
var headers = request.headers;
var host = headers.host;
var options = {
host: host.split(':')[0],
port: Number(host.split(':')[1]),
path: request.url,
method: request.method,
headers: headers
};
headers.record = 'off';
var req = http.request(options, function (res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
_this.storeRecording(payload, chunk.toString('utf8'), request, res.statusCode, registry, match.identifier);
response.end(chunk);
});
});
req.on('error', function (e) {
response.end(e);
});
req.end();
});
}
else {
next();
}
}
}
else {
next();
}
};
NgApimockHandler.prototype.getMatchingMock = function (mocks, requestUrl, method) {
return mocks.filter(function (_mock) {
var expressionMatches = new RegExp(_mock.expression).exec(decodeURI(requestUrl)) !== null, methodMatches = _mock.method === method;
return expressionMatches && methodMatches;
})[0];
};
NgApimockHandler.prototype.isBinaryResponse = function (response) {
return response.file !== undefined;
};
NgApimockHandler.prototype.updateData = function (data, variables, defaults) {
var _data;
if (data !== undefined) {
_data = JSON.stringify(data);
Object.keys(variables).forEach(function (key) {
if (variables.hasOwnProperty(key)) {
_data = _data.replace(new RegExp('%%' + key + '%%', 'g'), variables[key]);
}
});
}
else {
_data = JSON.stringify(defaults);
}
return _data;
};
NgApimockHandler.prototype.getJsonCallbackName = function (requestUrl) {
var url_parts = url.parse(requestUrl, true);
if (!url_parts.query || !url_parts.query.callback) {
return false;
}
return url_parts.query.callback;
};
NgApimockHandler.prototype.storeRecording = function (payload, chunk, request, statusCode, registry, identifier) {
var result = {
data: typeof chunk === 'string' ? chunk : chunk.toString('utf8'),
payload: payload,
datetime: new Date().getTime(),
method: request.method,
url: request.url,
statusCode: statusCode
};
if (registry.recordings[identifier] === undefined) {
registry.recordings[identifier] = [];
}
else if (registry.recordings[identifier].length > (this.MAX_RECORDINGS_PER_MOCK - 1)) {
registry.recordings[identifier].shift();
}
registry.recordings[identifier].push(result);
};
return NgApimockHandler;
}());
exports.default = NgApimockHandler;
//# sourceMappingURL=ngApimockHandler.js.map