1 2 /**3 * Retun normalized header, for example:4 * x-Forwarded-FOR -> X-Forwarded-For5 * content-type -> Content-Type6 * API-KEY -> Api-Key7 */8 function normalizeHeader(header) {9 var h = header.toLowerCase();10 var parts = h.split('-');11 12 parts = parts.filter(function(p) {13 return p.length > 0;14 });15 16 parts = parts.map(capitalize);17 18 return parts.join('-');19 }20 21 function capitalize(s){22 return s && s[0].toUpperCase() + s.slice(1);23 }24 25 26 var tests = [];27 tests.push(function test_normalize_headers() {28 29 var cases = {};30 cases['api-key'] = 'Api-Key';31 cases['X-My--Custom-Field'] = 'X-My-Custom-Field';32 cases['x-Forwarded-FOR'] = 'X-Forwarded-For';33 cases['Content-Type'] = 'Content-Type';34 35 for (var input in cases) {36 var output = normalizeHeader(input);37 var expected = cases[input];38 39 if (output != expected) {40 console.log('Normalized input for "' + input + '" should be "' + expected + '"');41 return false;42 };43 }44 45 return true;46 });47 48 runTests(tests);49 50 function runTests(l) {51 l.map(function(test) {52 if (false === test()) {53 console.log('FAIL: ' + test.name);54 return;55 };56 });57 console.log('PASS');58 }59 60 61 62 63 64
Enlace
El enlace para compartir es: