Repository: node-nock/nock (js)

Issue:
Multiple Set-Cookies headers in a recorded test are not respected
[RFC 6265](https://tools.ietf.org/html/rfc6265) indicates that an origin server can include multiple Set-Cookies header fields in a single response.

[This](http://www.accuweather.com/) [This](https://twitter.com/) and [This](https://www.google.com/) are live-in-the-wild examples of sites that use multiple Set-Cookie response headers.

Unfortunately, in recorded fixtures, multiple Set-Cookie headers are not being respected and only the last one wins.

This seems to be a regression introduced after 8.0.0

Unit Test:

Fixture - /tests/fixtures/testRawHeaders.json
``` JSON
[
    {
        "scope": "http://www.example2.com:80",
        "method": "GET",
        "path": "/",
        "body": "",
        "status": 200,
        "response": "Hello, World!",
        "rawHeaders": [
            "Cache-Control",
            "max-age=604800",
            "Content-Type",
            "text/html",
            "Set-Cookie",
            "foo=bar; path=/; secure; HttpOnly",
            "Set-Cookie",
            "baz=qix; path=/; secure; HttpOnly"
        ]
    }
]
```

Test
``` JS
test('multiple Set-Cookie headers in recorded tests are respected', function (t) {
    let nockDefs = nock.loadDefs(path.join("tests", "fixtures", "testRawHeaders.json"));
    nock.define(nockDefs);
    request({
        url: 'http://www.example2.com'
    }, function (err, response) {
        t.ok(response);
        
        t.ok(response.headers);
        t.true(Object.getOwnPropertyNames(response.headers).length > 0);
        t.equal(Object.prototype.toString.call(response.headers["set-cookie"]), "[object Array]");

        t.true(response.rawHeaders.length > 0);
        var setCookieHeaderCount = 0;
        for(var i = 0; i < response.rawHeaders.length; i++) {
            if (response.rawHeaders[i] === "Set-Cookie")
                setCookieHeaderCount++;
        }
        t.equal(setCookieHeaderCount, 2);
        t.end();
    });
```



Pull request:
Bug 818 multiple set cookie
Fixes #818 

Would like to add a test asserting full intercept cycle works but don't have time ATM. Have tested manually and it works.

Patch:
diff --git a/lib/common.js b/lib/common.js
index 71a4e0f..2ef0fd8 100644
--- a/lib/common.js
+++ b/lib/common.js
@@ -252,7 +252,15 @@ var headersArrayToObject = function (rawHeaders) {
   var headers = {};
 
   for (var i=0, len=rawHeaders.length; i<len; i=i+2) {
-    headers[rawHeaders[i]] = rawHeaders[i+1];
+    var key = rawHeaders[i];
+    var value = rawHeaders[i+1];
+
+    if (headers[key]) {
+      headers[key] = _.isArray(headers[key]) ? headers[key] : [headers[key]];
+      headers[key].push(value);
+    } else {
+      headers[key] = value;
+    }
   }
 
   return headers;


Test patch:
diff --git a/tests/test_common.js b/tests/test_common.js
index 45950f9..822b0d1 100644
--- a/tests/test_common.js
+++ b/tests/test_common.js
@@ -203,3 +203,40 @@ tap.test('stringifyRequest', function (t) {
 
   t.end();
 });
+
+
+tap.test('headersArrayToObject', function (t) {
+  var headers = [
+    "Content-Type",
+    "application/json; charset=utf-8",
+    "Last-Modified",
+    "foobar",
+    "Expires",
+    "fizbuzz"
+  ];
+
+  t.deepEqual(common.headersArrayToObject(headers), {
+    "Content-Type": "application/json; charset=utf-8",
+    "Last-Modified": "foobar",
+    "Expires": "fizbuzz"
+  });
+
+  var headersMultipleSetCookies = headers.concat([
+    "Set-Cookie",
+    "foo=bar; Domain=.github.com; Path=/",
+    "Set-Cookie",
+    "fiz=baz; Domain=.github.com; Path=/"
+  ]);
+
+  t.deepEqual(common.headersArrayToObject(headersMultipleSetCookies), {
+    "Content-Type": "application/json; charset=utf-8",
+    "Last-Modified": "foobar",
+    "Expires": "fizbuzz",
+    "Set-Cookie": [
+      "foo=bar; Domain=.github.com; Path=/",
+      "fiz=baz; Domain=.github.com; Path=/"
+    ]
+  });
+
+  t.end();
+});