Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MockAgent unexpected multi header behaviour with fetch #2616

Closed
tobiasdcl opened this issue Jan 16, 2024 · 2 comments · Fixed by #2619
Closed

MockAgent unexpected multi header behaviour with fetch #2616

tobiasdcl opened this issue Jan 16, 2024 · 2 comments · Fixed by #2619
Labels
bug Something isn't working

Comments

@tobiasdcl
Copy link

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

@tobiasdcl tobiasdcl added the bug Something isn't working label Jan 16, 2024
@metcoder95
Copy link
Member

cc @KhafraDev

By doing this we can reproduce the issue:

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']
    }
  })

fetch('http://localhost:3000/foo', {
  method: 'GET'
}).then(({ headers }) => {
  console.log(headers.getSetCookie())
})

Haven't dig into it yet.

@tobiasdcl
Copy link
Author

Awesome, thank you! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants