Skip to content

MockAgent unexpected multi header behaviour with fetch #2616

@tobiasdcl

Description

@tobiasdcl

First of all thank you for maintaining this library ❤️

Bug Description

I encountered an unexpected behaviour when using fetch with multi value headers via MockAgent.
I would expect the behaviour from fetch to be the same when using request (reference test) with MockAgent.
Is this behaviour expected and I am just missing something?

Reproducible By

save the following content as test/repro.js in the undici repository and then run npx tap test/repro.js

'use strict'

const { test } = require('tap')
const { setGlobalDispatcher, MockAgent } = require('..')
const { fetch } = require('..')

test("MockAgent - headers should be array of strings (fetch)", async (t) => {
  const mockAgent = new MockAgent();
  mockAgent.disableNetConnect();
  setGlobalDispatcher(mockAgent);

  const mockPool = mockAgent.get("http://localhost:3000");

  mockPool
    .intercept({
      path: "/foo",
      method: "GET",
    })
    .reply(200, "foo", {
      headers: {
        "set-cookie": ["foo=bar", "bar=baz", "baz=qux"],
      },
    });

  const { headers } = await fetch("http://localhost:3000/foo", {
    method: "GET",
  });

  t.equal(headers.getSetCookie(), ["foo=bar", "bar=baz", "baz=qux"]);
});

Expected Behavior

headers.getSetCookie() should return an array (["foo=bar", "bar=baz", "baz=qux"]) but instead it just returns ["foo=bar,bar=baz,baz=qux"] (array with one entry and all values concatenated)

Logs & Screenshots

 FAIL ​ test/repro.js
 ✖ should be equal

  test/repro.js                                                               
  27 |   });                                                                  
  28 |                                                                        
> 29 |   t.equal(headers.getSetCookie(), ["foo=bar", "bar=baz", "baz=qux"]);  
     | ----^                                                                  
  30 | });                                                                    

  --- expected                  
  +++ actual                    
  @@ -1,5 +1,3 @@               
   Array [                      
  -  "foo=bar",                 
  -  "bar=baz",                 
  -  "baz=qux",                 
  +  "foo=bar,bar=baz,baz=qux", 
   ]                            

  test: test/repro.js MockAgent - headers should be array of strings (fetch)

Environment

  • Node v20.11.0
  • ubuntu-22.04

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions