Skip to content

Commit c22a465

Browse files
normalize response headers for batch response
1 parent e810b60 commit c22a465

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

lib/requests/helpers/parseResponse.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,16 @@ function parseBatchHeaders(text) {
130130
line = readLine(text, ctx);
131131
parts = responseHeaderRegex.exec(line);
132132
if (parts !== null) {
133-
headers[parts[1]] = parts[2];
133+
headers[parts[1].toLowerCase()] = parts[2];
134134
}
135135
else {
136136
// Whatever was found is not a header, so reset the context position.
137137
ctx.position = pos;
138138
}
139139
} while (line && parts);
140140

141+
normalizeHeaders(headers);
142+
141143
return headers;
142144
}
143145

@@ -163,6 +165,46 @@ function readTo(text, ctx, str) {
163165
return text.substring(start, end);
164166
}
165167

168+
function normalizeHeaders(headers) {
169+
var keys = [];
170+
for (var i = 0; i < headers.length; ++i) {
171+
var key = normalizeHeader(headers[i]);
172+
if (key.length > 0) {
173+
keys.push(key);
174+
}
175+
}
176+
return keys;
177+
}
178+
179+
function normalizeHeader(header) {
180+
var key = "";
181+
var upperCase = false;
182+
for (var i = 0; i < header.length; ++i) {
183+
var letter = header[i];
184+
if (letter === " " && key.length > 0) {
185+
upperCase = true;
186+
continue;
187+
}
188+
if (!isAlnum(letter)) {
189+
continue;
190+
}
191+
if (key.length === 0 && isDigit(letter)) {
192+
continue;
193+
}
194+
if (upperCase) {
195+
upperCase = false;
196+
key += letter.toUpperCase();
197+
} else {
198+
key += letter.toLowerCase();
199+
}
200+
}
201+
return key;
202+
}
203+
204+
function isDigit(char) {
205+
return char >= "0" && char <= "9";
206+
}
207+
166208
//partially taken from https://github.com/emiltholin/google-api-batch-utils
167209
/**
168210
*

tests/main-tests.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4946,12 +4946,8 @@ describe("promises -", function () {
49464946
}).catch(function (object) {
49474947
expect(object.length).to.be.eq(1);
49484948

4949-
console.log(object[0].headers);
4950-
console.log("--\r\n");
4951-
console.log({ "OData-Version": "4.0", "REQ_ID": "5fe339e5-c75e-4dad-9597-b257ebce666b", "Content-Type": "application/json; odata.metadata=minimal" });
4952-
49534949
expect(object[0].headers).to.deep.equal({
4954-
"OData-Version": "4.0", "REQ_ID": "5fe339e5-c75e-4dad-9597-b257ebce666b", "Content-Type": "application/json; odata.metadata=minimal"
4950+
"odata-version": "4.0", "req_id": "5fe339e5-c75e-4dad-9597-b257ebce666b", "content-type": "application/json; odata.metadata=minimal"
49554951
});
49564952

49574953
expect(object[0].error).to.deep.equal({

types/dynamics-web-api-callbacks.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Type definitions for dynamics-web-api-callbacks v1.6.7
1+
// Type definitions for dynamics-web-api-callbacks v1.6.8
22
// Project: https://github.com/AleksandrRogov/DynamicsWebApi
33
// Definitions by: Aleksandr Rogov https://github.com/AleksandrRogov/
44

types/dynamics-web-api.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Type definitions for dynamics-web-api v1.6.7
1+
// Type definitions for dynamics-web-api v1.6.8
22
// Project: https://github.com/AleksandrRogov/DynamicsWebApi/
33
// Definitions by: Aleksandr Rogov https://github.com/AleksandrRogov/
44

0 commit comments

Comments
 (0)