cmorten / opine

Minimalist web framework for Deno ported from ExpressJS.
https://github.com/cmorten/opine/blob/main/.github/API/api.md
MIT License
854 stars 43 forks source link

Cookie headers are being overwritten by later setCookie headers #117

Closed cmorten closed 3 years ago

cmorten commented 3 years ago

Issue

Setup:

Details

Attempting to upgrade superdeno to support Deno ^1.9.0 noticing that the cookie tests are failing because Opine is not appending cookies set in headers, instead incorrectly overwriting them...

const app = opine();

    app.get("/", (req, res) => {
      res.cookie({ name: "foo", value: "bar" });
      res.cookie({ name: "user", value: "deno" });
      res.set("Set-Cookie", "fizz=buzz");

      console.log(res.headers); // Showing only `"set-cookie": "fizz=buzz",`. Missing the other 2 cookies.

      res.set("X-Tested-With", "SuperDeno");
      res.type("application/json");
      res.end();
    });

    superdeno(app)
      .get("/")
      .expect(function (res) {
        expect(res.headers).toEqual({
          "content-length": "0",
          "x-powered-by": "Opine",
          "set-cookie": "foo=bar; Path=/, user=deno; Path=/, fizz=buzz", // Seeing `fizz=buzz` instead and fails ❌
          "x-tested-with": "SuperDeno",
          "content-type": "application/json; charset=utf-8",
        });
      })
      .expect(200, done);
cmorten commented 3 years ago

This is now the expected behaviour of set following the adjustment of the underlying Header class in Deno to match the web specification. To append cookies instead use .append() not .set().