cmorten / superoak

HTTP assertions for Oak made easy via SuperDeno. 🐿 🦕
https://cmorten.github.io/superoak/
MIT License
121 stars 8 forks source link

Testing error case fails #25

Closed twhitbeck closed 2 years ago

twhitbeck commented 3 years ago

Issue

Trying to test that an endpoint returns an error status code (like 400) fails.

Details

https://github.com/twhitbeck/superoak-testcase

I'm trying to create a test case that this endpoint will respond with 400.

[uncaught oak error]: BadRequestError - Invalid url

request: { url: "http://127.0.0.1:19542/create", method: "POST", hasBody: true }
response: { status: 404, type: undefined, hasBody: false, writable: true }

    at createHttpError (https://deno.land/x/oak@v9.0.0/httpError.ts:128:10)
    at Context.throw (https://deno.land/x/oak@v9.0.0/context.ts:177:17)
    at file:///home/twhitbeck/src/local/superoak-testcase/server.ts:18:18
    at async dispatch (https://deno.land/x/oak@v9.0.0/middleware.ts:41:7)
    at async dispatch (https://deno.land/x/oak@v9.0.0/middleware.ts:41:7)
    at async dispatch (https://deno.land/x/oak@v9.0.0/middleware.ts:41:7)
    at async Application.#handleRequest (https://deno.land/x/oak@v9.0.0/application.ts:378:9)
test it should respond with 400 when the url is marlformed ... FAILED (18ms)

failures:

it should respond with 400 when the url is marlformed
AssertionError: Test case is leaking async ops.
Before:
  - dispatched: 2
  - completed: 2
After:
  - dispatched: 13
  - completed: 12

Make sure to await all promises returned from Deno APIs before
finishing test case.
    at assert (deno:runtime/js/06_util.js:42:13)
    at asyncOpSanitizer (deno:runtime/js/40_testing.js:48:7)
    at async resourceSanitizer (deno:runtime/js/40_testing.js:72:7)
    at async exitSanitizer (deno:runtime/js/40_testing.js:99:9)
    at async runTest (deno:runtime/js/40_testing.js:215:7)
    at async Object.runTests (deno:runtime/js/40_testing.js:283:22)
    at async file:///home/twhitbeck/src/local/superoak-testcase/c7629de1-c472-4780-a46d-d82b87eba195$deno$test.js:4:1

failures:

        it should respond with 400 when the url is marlformed

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out (90208ms)

error: Test failed
twhitbeck commented 3 years ago

@asos-craigmorten thanks for taking a look!

cmorten commented 3 years ago

Hey @twhitbeck :wave:

So the issue here is https://github.com/asos-craigmorten/superoak/issues/22 😅

Oak, since adopting the native Deno server, hasn't correctly been closing the server (REF: https://github.com/oakserver/oak/issues/314) (hence the hanging resource at the end of tests - and because superoak kinda wraps everything, you can't really work around it 😞). I put in a fix https://github.com/oakserver/oak/pull/389 but a new release hasn't been created yet. 🤞 it will be released soon! Perhaps we can hint at Kitson on Discord 😉

For now you can work around the issue by using the std lib server implementation for Oak with the serverConstructor parameter. E.g.

import {
  Application,
  Router,
  Status,
  HttpServerStd,
} from "https://deno.land/x/oak@v9.0.0/mod.ts";

// Define the routes
const router = new Router();

router.post("/create", async (context) => {
  const body = context.request.body({ type: "json" });

  const { url } = (await body.value) as { url: string };

  try {
    new URL(url);
  } catch {
    context.throw(Status.BadRequest, "Invalid url");
  }

  context.response.body = url;
});

const app = new Application({ serverConstructor: HttpServerStd }); // here

app.use(router.routes());
app.use(router.allowedMethods());

if (import.meta.main) {
  await app.listen({ port: 8080 });
}

export { app };

Not ideal but all we can do for now until the fix in Oak is released!

Alternatively you could use a commit / branch version of Oak to resolve for now, e.g. https://raw.githubusercontent.com/oakserver/oak/main/mod.ts.

twhitbeck commented 3 years ago

thanks @cmorten it's all good now! boy that bit me hard!

c0per commented 2 years ago

I think they include the fix in release v9.0.1, I'm using it all fine.

asos-craigmorten commented 2 years ago

Fixed upstream in latest version https://deno.land/x/oak@v9.0.1