[Pkg-javascript-commits] [node-stream-http] 183/208: Handle arrays in request headers and improve header handling
Bastien Roucariès
rouca at moszumanska.debian.org
Sun Aug 13 13:39:43 UTC 2017
This is an automated email from the git hooks/post-receive script.
rouca pushed a commit to branch master
in repository node-stream-http.
commit e54de15542a7f9ca6fd987e6e9e27fd08686d2e2
Author: John Hiesey <john at hiesey.com>
Date: Mon Apr 3 22:25:57 2017 -0700
Handle arrays in request headers and improve header handling
Passing an array as a header value now works as in node.
Header handling with multiple values for the same header is also
tested, and mime type test updated for safari 10.1
Fixes #73
---
lib/request.js | 24 +++++++++++++++++-------
test/browser/headers.js | 39 +++++++++++++++++++++++++++++++++++++--
test/server/index.js | 7 +++++--
3 files changed, 59 insertions(+), 11 deletions(-)
diff --git a/lib/request.js b/lib/request.js
index e9eb967..80149ae 100644
--- a/lib/request.js
+++ b/lib/request.js
@@ -112,14 +112,24 @@ ClientRequest.prototype._onFinish = function () {
}
}
- if (self._mode === 'fetch') {
- var headers = Object.keys(headersObj).map(function (name) {
- return [headersObj[name].name, headersObj[name].value]
- })
+ // create flattened list of headers
+ var headersList = []
+ Object.keys(headersObj).forEach(function (keyName) {
+ var name = headersObj[keyName].name
+ var value = headersObj[keyName].value
+ if (Array.isArray(value)) {
+ value.forEach(function (v) {
+ headersList.push([name, v])
+ })
+ } else {
+ headersList.push([name, value])
+ }
+ })
+ if (self._mode === 'fetch') {
global.fetch(self._opts.url, {
method: self._opts.method,
- headers: headers,
+ headers: headersList,
body: body || undefined,
mode: 'cors',
credentials: opts.withCredentials ? 'include' : 'same-origin'
@@ -157,8 +167,8 @@ ClientRequest.prototype._onFinish = function () {
}
}
- Object.keys(headersObj).forEach(function (name) {
- xhr.setRequestHeader(headersObj[name].name, headersObj[name].value)
+ headersList.forEach(function (header) {
+ xhr.setRequestHeader(header[0], header[1])
})
self._response = null
diff --git a/test/browser/headers.js b/test/browser/headers.js
index 99f8bf8..1054eda 100644
--- a/test/browser/headers.js
+++ b/test/browser/headers.js
@@ -46,6 +46,40 @@ test('headers', function (t) {
})
})
+test('arrays of headers', function (t) {
+ http.get({
+ path: '/testHeaders?Response-Header=bar&Response-Header=BAR2',
+ headers: {
+ 'Test-Request-Header': ['foo', 'FOO2']
+ }
+ }, function (res) {
+ var rawHeaders = []
+ for (var i = 0; i < res.rawHeaders.length; i += 2) {
+ var lowerKey = res.rawHeaders[i].toLowerCase()
+ if (lowerKey.indexOf('test-') === 0)
+ rawHeaders.push(lowerKey, res.rawHeaders[i + 1])
+ }
+ t.equal(rawHeaders[0], 'test-response-header', 'raw response header present')
+ t.equal(rawHeaders[1], 'bar, BAR2', 'raw response header value')
+ t.equal(rawHeaders.length, 2, 'correct number of raw headers')
+
+ t.equal(res.headers['test-response-header'], 'bar, BAR2', 'response header')
+
+ var buffers = []
+
+ res.on('end', function () {
+ var body = JSON.parse(Buffer.concat(buffers).toString())
+ t.equal(body['test-request-header'], 'foo,FOO2', 'request headers')
+ t.equal(Object.keys(body).length, 1, 'correct number of request headers')
+ t.end()
+ })
+
+ res.on('data', function (data) {
+ buffers.push(data)
+ })
+ })
+})
+
test('content-type response header', function (t) {
http.get('/testHeaders', function (res) {
t.equal(res.headers['content-type'], 'application/json', 'content-type preserved')
@@ -56,10 +90,11 @@ test('content-type response header', function (t) {
var browser = (new UAParser()).setUA(navigator.userAgent).getBrowser()
var browserName = browser.name
var browserVersion = browser.major
+var browserMinorVersion = browser.minor
// The content-type header is broken when 'prefer-streaming' or 'allow-wrong-content-type'
-// is passed in browsers that rely on xhr.overrideMimeType(), namely older chrome and newer safari
+// is passed in browsers that rely on xhr.overrideMimeType(), namely older chrome and safari 6-10.0
var wrongMimeType = ((browserName === 'Chrome' && browserVersion <= 42) ||
- ((browserName === 'Safari' || browserName === 'Mobile Safari') && browserVersion >= 6))
+ ((browserName === 'Safari' || browserName === 'Mobile Safari') && browserVersion >= 6 && (browserVersion < 10 || (browserVersion === 10 && browserMinorVersion === 0))))
test('content-type response header with forced streaming', function (t) {
http.get({
diff --git a/test/server/index.js b/test/server/index.js
index 5a042b8..8f474b2 100644
--- a/test/server/index.js
+++ b/test/server/index.js
@@ -37,8 +37,11 @@ app.get('/testHeaders', function (req, res) {
var reqHeaders = {}
Object.keys(req.headers).forEach(function (key) {
key = key.toLowerCase()
- if (key.indexOf('test-') === 0)
- reqHeaders[key] = req.headers[key]
+ if (key.indexOf('test-') === 0) {
+ // different browsers format request headers with multiple values
+ // slightly differently, so normalize
+ reqHeaders[key] = req.headers[key].replace(', ', ',')
+ }
})
var body = JSON.stringify(reqHeaders)
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-stream-http.git
More information about the Pkg-javascript-commits
mailing list