1 2 /**3 * Note: how to use to fix the lambda, normalize headers map just at4 * the begining of the handler function:5 * 6 * normalizeHeadersMap(event.headers);7 */8 9 10 // Code start11 12 /**13 * Retun normalized header, for example:14 * x-Forwarded-FOR -> X-Forwarded-For15 * content-type -> Content-Type16 * API-KEY -> Api-Key17 */18 function normalizeHeader(header) {19 var h = header.toLowerCase();20 var parts = h.split('-');21 22 parts = parts.filter(function(p) {23 return p.length > 0;24 });25 26 parts = parts.map(capitalize);27 28 return parts.join('-');29 }30 31 function normalizeHeadersMap(m) {32 var r = {};33 for (var k in m) {34 var v = m[k];35 var n = normalizeHeader(k); 36 m[n] = v;37 r[n] = v;38 } 39 return r;40 }41 42 /**43 * First letter uppercase and the rest lowercase44 */45 function capitalize(s){46 return s && s[0].toUpperCase() + s.slice(1).toLowerCase();47 }48 49 // Code end50 51 52 53 54 55 56 57 // Tests start58 59 var tests = [];60 61 tests.push(function test_normalize_header() {62 63 var cases = {};64 cases['api-key'] = 'Api-Key';65 cases['X-My--Custom-Field'] = 'X-My-Custom-Field';66 cases['x-Forwarded-FOR'] = 'X-Forwarded-For';67 cases['Content-Type'] = 'Content-Type';68 69 for (var input in cases) {70 var output = normalizeHeader(input);71 var expected = cases[input];72 73 if (output != expected) {74 console.log('Normalized input for "' + input + '" should be "' + expected + '"');75 return false;76 };77 }78 79 return true;80 });81 82 tests.push(function test_capitalize() {83 84 var cases = {};85 cases[''] = '';86 cases['h'] = 'H';87 cases['ZETA'] = 'Zeta';88 cases['blabla'] = 'Blabla';89 cases['sEvErAl_CaSES'] = 'Several_cases';90 91 for (var input in cases) {92 var output = capitalize(input);93 var expected = cases[input];94 95 if (output != expected) {96 console.log('Capitalized input for "' + input + '" should be "' + expected + '"');97 return false;98 };99 }100 101 return true;102 });103 104 tests.push(function test_headers_map() {105 106 var headers = {};107 headers['content-type'] = 'application/json; utf-8';108 headers['API-KEY'] = '54849df48ds';109 headers['ZETA'] = '8uh8uh8uh';110 headers['x-forwarded-for'] = '192.168.1.1,55.125.32.47';111 112 var output = normalizeHeadersMap(headers);113 var expected = {};114 expected['Content-Type'] = 'application/json; utf-8';115 expected['Api-Key'] = '54849df48ds';116 expected['Zeta'] = '8uh8uh8uh';117 expected['X-Forwarded-For'] = '192.168.1.1,55.125.32.47';118 119 if (JSON.stringify(output) != JSON.stringify(expected)) {120 console.log('Expected', expected , 'obtained', output);121 return false;122 };123 124 return true;125 });126 127 tests.push(function test_headers_map_reference() {128 129 var headers = {};130 headers['API-KEY'] = '54849df48ds';131 132 normalizeHeadersMap(headers);133 134 var expected = {};135 expected['API-KEY'] = '54849df48ds';136 expected['Api-Key'] = '54849df48ds';137 138 if (JSON.stringify(expected) != JSON.stringify(headers)) {139 console.log('Expected', expected , 'obtained', headers);140 return false;141 };142 143 return true;144 });145 146 // Tests end147 148 149 150 151 152 153 154 155 // Test runner156 157 runTests(tests);158 159 function runTests(l) {160 l.map(function(test) {161 if (false === test()) {162 console.log('FAIL:' + test.name);163 return;164 } else {165 console.log('OK: ' + test.name);166 }167 });168 console.log('PASS');169 }170
Enlace
El enlace para compartir es: