Closed twhitbeck closed 2 years ago
@asos-craigmorten thanks for taking a look!
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
.
thanks @cmorten it's all good now! boy that bit me hard!
I think they include the fix in release v9.0.1, I'm using it all fine.
Fixed upstream in latest version https://deno.land/x/oak@v9.0.1
Issue
Trying to test that an endpoint returns an error status code (like 400) fails.
Details
https://github.com/twhitbeck/superoak-testcase
curl localhost:8080/create -H "Content-Type: application/json" -d "{\"url\":\"foo\"}" -v
I'm trying to create a test case that this endpoint will respond with 400.
deno test --allow-net