Add tests for deprecation messages

This commit is contained in:
Jason Tedor 2017-02-27 10:42:56 -05:00
parent f14055cb8d
commit 8a9f4323b9
3 changed files with 59 additions and 31 deletions

View file

@ -178,18 +178,10 @@ export function initializeInput($el, $actionsEl, $copyAsCurlEl, output) {
var warnings = xhr.getResponseHeader("warning");
if (warnings) {
// pattern for valid warning header
var re = /\d{3} [0-9a-zA-Z!#$%&'*+-.^_`|~]+ \"((?:\t| |!|[\x23-\x5b]|[\x5d-\x7e]|[\x80-\xff]|\\\\|\\")*)\"(?: \"[^"]*\")/
// split on any comma that is followed by an even number of quotes
warnings = _.map(utils.splitOnUnquotedCommaSpace(warnings), function (warning) {
var match = re.exec(warning)
// extract the actual warning if there was a match
return "#! Deprecation: " + (match !== null ? match[1] : warning)
});
value = warnings.join("\n") + "\n" + value;
var deprecationMessages = utils.extractDeprecationMessages(warnings);
value = deprecationMessages.join("\n") + "\n" + value;
}
if (isMultiRequest) {
value = "# " + req.method + " " + req.url + "\n" + value;
}

View file

@ -56,29 +56,44 @@ utils.expandLiteralStrings = function (data) {
});
}
utils.extractDeprecationMessages = function (warnings) {
// pattern for valid warning header
var re = /\d{3} [0-9a-zA-Z!#$%&'*+-.^_`|~]+ \"((?:\t| |!|[\x23-\x5b]|[\x5d-\x7e]|[\x80-\xff]|\\\\|\\")*)\"(?: \"[^"]*\")/
// split on any comma that is followed by an even number of quotes
return _.map(utils.splitOnUnquotedCommaSpace(warnings), function (warning) {
var match = re.exec(warning)
// extract the actual warning if there was a match
return "#! Deprecation: " + (match !== null ? utils.unescape(match[1]) : warning)
});
}
utils.unescape = function (s) {
return s.replace(/\\\\/g, "\\").replace(/\\"/g, "\"")
}
utils.splitOnUnquotedCommaSpace = function (s) {
var quoted = false;
var arr = [];
var buffer = '';
var i = 0
while (i < s.length) {
var token = s.charAt(i++)
if (token == '\\' && i < s.length) {
token += s.charAt(i++)
} else if (token == ',' && i < s.length && s.charAt(i) == ' ') {
token += s.charAt(i++);
}
if (token == '"') {
quoted = !quoted
} else if (!quoted && token == ', ') {
arr.push(buffer);
buffer = '';
continue
}
buffer += token;
var quoted = false;
var arr = [];
var buffer = '';
var i = 0
while (i < s.length) {
var token = s.charAt(i++)
if (token == '\\' && i < s.length) {
token += s.charAt(i++)
} else if (token == ',' && i < s.length && s.charAt(i) == ' ') {
token += s.charAt(i++);
}
arr.push(buffer)
return arr;
if (token == '"') {
quoted = !quoted
} else if (!quoted && token == ', ') {
arr.push(buffer);
buffer = '';
continue
}
buffer += token;
}
arr.push(buffer)
return arr;
}
module.exports = utils;

View file

@ -34,6 +34,27 @@ _.each(expandingTests.split(/^=+$/m), function (fixture) {
deepEqual(utils.expandLiteralStrings(collapsed), expanded);
});
test("extract deprecation messages", function () {
deepEqual(utils.extractDeprecationMessages(
'299 Elasticsearch-6.0.0-alpha1-SNAPSHOT-abcdef1 "this is a warning" "Mon, 27 Feb 2017 14:52:14 GMT"'),
['#! Deprecation: this is a warning']);
deepEqual(utils.extractDeprecationMessages(
'299 Elasticsearch-6.0.0-alpha1-SNAPSHOT-abcdef1 "this is a warning" "Mon, 27 Feb 2017 14:52:14 GMT", 299 Elasticsearch-6.0.0-alpha1-SNAPSHOT-abcdef1 "this is a second warning" "Mon, 27 Feb 2017 14:52:14 GMT"'),
['#! Deprecation: this is a warning', '#! Deprecation: this is a second warning']);
deepEqual(utils.extractDeprecationMessages(
'299 Elasticsearch-6.0.0-alpha1-SNAPSHOT-abcdef1 "this is a warning, and it includes a comma" "Mon, 27 Feb 2017 14:52:14 GMT"'),
['#! Deprecation: this is a warning, and it includes a comma']);
deepEqual(utils.extractDeprecationMessages(
'299 Elasticsearch-6.0.0-alpha1-SNAPSHOT-abcdef1 "this is a warning, and it includes an escaped backslash \\\\ and a pair of \\\"escaped quotes\\\"" "Mon, 27 Feb 2017 14:52:14 GMT"'),
['#! Deprecation: this is a warning, and it includes an escaped backslash \\ and a pair of "escaped quotes"']);
});
test("unescape", function () {
deepEqual(utils.unescape('escaped backslash \\\\'), 'escaped backslash \\');
deepEqual(utils.unescape('a pair of \\\"escaped quotes\\\"'), 'a pair of "escaped quotes"');
deepEqual(utils.unescape('escaped quotes do not have to come in pairs: \\\"'), 'escaped quotes do not have to come in pairs: "');
});
test("split on unquoted comma followed by space", function () {
deepEqual(utils.splitOnUnquotedCommaSpace('a, b'), ['a', 'b']);
deepEqual(utils.splitOnUnquotedCommaSpace('a,b, c'), ['a,b', 'c']);