cmorten / superdeno

Super-agent driven library for testing Deno HTTP servers.
https://cmorten.github.io/superdeno/
MIT License
124 stars 6 forks source link

4xx and 5xx response missing body and text #11

Closed ngot closed 4 years ago

ngot commented 4 years ago

Issue

Setup:

When server response 4xx or 5xx, superdeno's response doesn't have body or text, so there is no way to test the HTTP body part.

import {
  Application,
} from "https://deno.land/x/ako/mod.ts";
import { superdeno } from "https://deno.land/x/superdeno@1.5.1/mod.ts";
import { describe, it } from "https://deno.land/x/opine@0.17.0/test/utils.ts";

describe("ctx.onerror(err)", () => {
  it("should respond", async () => {
    const app = new Application();

    app.use((ctx, next) => {
      ctx.body = "something else";

      ctx.throw(418, "boom");
    });

    const res = await superdeno(app.listen())
      .head("/")
      .expect(418)
      .expect("Content-Length", "4")
      .expect("Content-Type", "text/plain; charset=utf-8");
    console.log(res);

    // res.body is null
    // res.text is null
  });
});

curl:

curl -i  127.0.0.1:5000
HTTP/1.1 418 I'm a teapot
content-length: 4
content-type: text/plain; charset=utf-8

boom% 
ngot commented 4 years ago

Read https://github.com/asos-craigmorten/superdeno/issues/1 again, realized that patch only fixed the error throw, not the empty body.

asos-craigmorten commented 4 years ago

Hi @ngot I've added a test for this for both Oak and Ako in 1.6.0.

I believe the issue is that in the sample test you are making a HEAD request which will discard the body, whereas in your curl request you are implicitly using a GET request, so the body is returned.

Test added here: https://github.com/asos-craigmorten/superdeno/blob/main/test/supertest.ako.test.ts#L7 and passing in CICD.

ngot commented 4 years ago

@asos-craigmorten Thanks for the investigation. That's right, I didn't realise I was using the HEAD request. I was definitely too blind to find that.

asos-craigmorten commented 4 years ago

No worries - thanks for raising anyway! Lead to some extra tests and a small refactor so all good 🎉