Skip to content

Commit

Permalink
feat: add router.put() and router.delete() (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
keroxp authored Apr 5, 2020
1 parent d15b51e commit 86c700e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
19 changes: 17 additions & 2 deletions router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,19 @@ export interface Router extends Route {
route(prefix: string, ...handlers: (RouteHandler | Router)[]): void;

/**
* Register GET route. This is shortcut for handle();
* Handlers will be called on GET and HEAD method.
* Register GET/HEAD route. This is shortcut for handle();
* */
get(pattern: string | RegExp, ...handlers: RouteHandler[]): void;

/** Register POST route. This is shortcut for handle() */
post(pattern: string | RegExp, ...handlers: RouteHandler[]): void;

/** Register PUT route */
put(pattern: string | RegExp, ...handlers: RouteHandler[]): void;

/** Register DELETE route */
delete(pattern: string | RegExp, ...handlers: RouteHandler[]): void;

/** Accept ws upgrade */
ws(pattern: string | RegExp, handler: WebSocketHandler): void;
ws(
Expand Down Expand Up @@ -137,6 +142,14 @@ export function createRouter(): Router {
routes.push({ pattern, methods: ["POST"], handlers });
}

function put(pattern: string | RegExp, ...handlers: RouteHandler[]) {
routes.push({ pattern, methods: ["PUT"], handlers });
}

function _delete(pattern: string | RegExp, ...handlers: RouteHandler[]) {
routes.push({ pattern, methods: ["DELETE"], handlers });
}

function use(...handlers: ServeHandler[]) {
middlewareList.push(...handlers);
}
Expand Down Expand Up @@ -253,6 +266,8 @@ export function createRouter(): Router {
route,
get,
post,
put,
delete: _delete,
ws,
catch: _catch,
finally: _finally,
Expand Down
53 changes: 51 additions & 2 deletions router_test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
// Copyright 2019 Yusuke Sakurai. All rights reserved. MIT license.
import { assertEquals } from "./vendor/https/deno.land/std/testing/asserts.ts";
import {
assertEquals,
assertThrowsAsync
} from "./vendor/https/deno.land/std/testing/asserts.ts";
import { it, makeGet, assertRoutingError } from "./test_util.ts";
import { Loglevel, setLevel } from "./logger.ts";
import { writeResponse } from "./serveio.ts";
import {
createRouter
createRouter,
Router
} from "./router.ts";
import { ServerRequest } from "./server.ts";
import { createRecorder } from "./testing.ts";
import { RoutingError } from "./error.ts";

setLevel(Loglevel.NONE);

Expand Down Expand Up @@ -87,6 +93,49 @@ it("router", (t) => {
);
});

it("method routes", (t) => {
const handler = (req: ServerRequest) => {
return req.respond({ status: 200, body: req.method });
};
const methods = ["GET", "POST", "PUT", "DELETE"];
const assertMethods = async (router: Router, method: string) => {
for (const _method of methods) {
const rec = createRecorder({ url: "/", method: _method });
if (_method === method) {
await router.handleRoute("", rec);
const resp = await rec.response();
assertEquals(resp.status, 200);
assertEquals(await resp.body.text(), method);
} else {
await assertThrowsAsync(async () => {
await router.handleRoute("", rec);
}, RoutingError);
}
}
};
let router: Router;
t.beforeAfterEach(() => {
router = createRouter();
return () => {};
});
t.run("GET", async () => {
router.get("/", handler);
assertMethods(router, "GET");
});
t.run("POST", async () => {
router.post("/", handler);
assertMethods(router, "POST");
});
t.run("PUT", async () => {
router.put("/", handler);
assertMethods(router, "PUT");
});
t.run("DELETE", async () => {
router.delete("/", handler);
assertMethods(router, "DELETE");
});
});

it("same path routes", (t) => {
t.run("/", async () => {
const router = createRouter();
Expand Down

0 comments on commit 86c700e

Please sign in to comment.