Revert "Zotero.HTTP.request(): Process headers case insensitively"

This reverts commit f6dd47dd1f.

I shouldn't have cherry-picked this from fx102.
This commit is contained in:
Dan Stillman 2022-12-24 03:54:18 -05:00
parent f6dd47dd1f
commit 0862e1e1bc
2 changed files with 22 additions and 41 deletions

View file

@ -117,7 +117,7 @@ Zotero.HTTP = new function() {
* @param {nsIURI|String} url - URL to request
* @param {Object} [options] Options for HTTP request:
* @param {String} [options.body] - The body of a POST request
* @param {Object | Headers} [options.headers] - HTTP headers to send with the request
* @param {Object} [options.headers] - Object of HTTP headers to send with the request
* @param {Boolean} [options.followRedirects = true] - Object of HTTP headers to send with the
* request
* @param {Zotero.CookieSandbox} [options.cookieSandbox] - The sandbox from which cookies should
@ -343,19 +343,22 @@ Zotero.HTTP = new function() {
}
// Send headers
var headers = new Headers(options?.headers || {});
var headers = {};
if (options && options.headers) {
Object.assign(headers, options.headers);
}
var compressedBody = false;
if (options.body) {
if (!headers.get("Content-Type")) {
headers.set("Content-Type", "application/x-www-form-urlencoded");
if (!headers["Content-Type"]) {
headers["Content-Type"] = "application/x-www-form-urlencoded";
}
else if (headers.get("Content-Type") == 'multipart/form-data') {
else if (headers["Content-Type"] == 'multipart/form-data') {
// Allow XHR to set Content-Type with boundary for multipart/form-data
headers.delete("Content-Type");
delete headers["Content-Type"];
}
if (options.compressBody && this.isWriteMethod(method)) {
headers.set('Content-Encoding', 'gzip');
headers['Content-Encoding'] = 'gzip';
compressedBody = await Zotero.Utilities.Internal.gzip(options.body);
let oldLen = options.body.length;
@ -365,17 +368,23 @@ Zotero.HTTP = new function() {
}
}
if (options.debug) {
if (headers.has("Zotero-API-Key")) {
let dispHeaders = new Headers(headers);
dispHeaders.set("Zotero-API-Key", "[Not shown]");
Zotero.debug({ ...dispHeaders.entries() });
if (headers["Zotero-API-Key"]) {
let dispHeaders = {};
Object.assign(dispHeaders, headers);
if (dispHeaders["Zotero-API-Key"]) {
dispHeaders["Zotero-API-Key"] = "[Not shown]";
}
Zotero.debug(dispHeaders);
}
else {
Zotero.debug({ ...headers.entries() });
Zotero.debug(headers);
}
}
for (var [header, value] of headers) {
for (var header in headers) {
// Convert numbers to string to make Sinon happy
let value = typeof headers[header] == 'number'
? headers[header].toString()
: headers[header]
xmlhttp.setRequestHeader(header, value);
}

View file

@ -45,20 +45,6 @@ describe("Zotero.HTTP", function () {
}
}
);
httpd.registerPathHandler(
'/requireJSON',
{
handle(request, response) {
if (request.getHeader('Content-Type') == 'application/json') {
response.setStatusLine(null, 200, "OK");
}
else {
response.setStatusLine(null, 400, "Bad Request");
}
response.write('JSON required');
}
}
);
});
beforeEach(function () {
@ -138,20 +124,6 @@ describe("Zotero.HTTP", function () {
server.respond();
});
it("should process headers case insensitively", async function () {
Zotero.HTTP.mock = null;
var req = await Zotero.HTTP.request(
'GET',
baseURL + 'requireJSON',
{
headers: {
'content-type': 'application/json'
}
}
);
assert.equal(req.status, 200);
});
describe("Retries", function () {
var spy;
var delayStub;