Search code examples
rubycookiessetcookiewebrick

How do I set multiple cookies in a single Webrick response?


I use Webrick to test my HTTP client and I need to test how it gets and sets cookies.

Wikipedia provides an example of such response:

HTTP/1.0 200 OK
Content-type: text/html
Set-Cookie: theme=light
Set-Cookie: sessionToken=abc123; Expires=Wed, 09 Jun 2021 10:18:14 GMT
...

but if I do

server.mount_proc ?/ do |req, res|
  res["set-cookie"] = %w{ 1=2 2=3 }

the whole array becomes a single cookie: "[\"1=2\", \"2=3\"]"

And then in WEBrick::HTTPResponse source code I see again the @header = Hash.new that probably means you can't repeat the header key.

Is it impossible?!

UPD:

This leaves me no hope:


Solution

  • Another method should be used instead of res[...]=:

    res.cookies.push WEBrick::Cookie.new("1", "2")
    res.cookies.push WEBrick::Cookie.new("3", "4")
    res.cookies.push WEBrick::Cookie.new("1", "5")