diff --git a/spec/amber/router/route_spec.cr b/spec/amber/router/route_spec.cr index 0f08adc2a..af87ba7e7 100644 --- a/spec/amber/router/route_spec.cr +++ b/spec/amber/router/route_spec.cr @@ -14,7 +14,7 @@ module Amber route.class.should eq Route end - describe "#substitute_keys)in_path" do + describe "#substitute_keys_in_path" do it "parses route resource params" do params = {"id" => "123", "name" => "John"} empty_hash = {} of String => String diff --git a/spec/amber/router/router_spec.cr b/spec/amber/router/router_spec.cr index 131e37253..5aee42417 100644 --- a/spec/amber/router/router_spec.cr +++ b/spec/amber/router/router_spec.cr @@ -12,9 +12,18 @@ module Amber end router.match("GET", "/hello").path.should eq "get/hello" + router.match("HEAD", "/hello").path.should eq "head/hello" + router.match("OPTIONS", "/hello").path.should eq "options/hello" router.match("GET", "/hello/2").path.should eq "get/hello/:id" + router.match("HEAD", "/hello/2").path.should eq "head/hello/:id" + router.match("OPTIONS", "/hello/2").path.should eq "options/hello/:id" router.match("GET", "/hello/new").path.should eq "get/hello/new" + router.match("HEAD", "/hello/new").path.should eq "head/hello/new" + router.match("OPTIONS", "/hello/new").path.should eq "options/hello/new" router.match("GET", "/hello/2/edit").path.should eq "get/hello/:id/edit" + router.match("HEAD", "/hello/2/edit").path.should eq "head/hello/:id/edit" + router.match("OPTIONS", "/hello/2/edit").path.should eq "options/hello/:id/edit" + router.match("OPTIONS", "/hello/1").path.should eq "options/hello/:id" router.match("PUT", "/hello/1").path.should eq "put/hello/:id" router.match("PATCH", "/hello/1").path.should eq "patch/hello/:id" router.match("DELETE", "/hello/1").path.should eq "delete/hello/:id" diff --git a/src/amber/dsl/router.cr b/src/amber/dsl/router.cr index b2274c88d..1128286fd 100644 --- a/src/amber/dsl/router.cr +++ b/src/amber/dsl/router.cr @@ -28,6 +28,13 @@ module Amber::DSL {% for verb in RESOURCES %} macro {{verb.id}}(*args) route {{verb}}, \{{*args}} + {% if verb == :get %} + route :head, \{{*args}} + {% end %} + route {{verb}}, \{{*args}} + {% if ![:trace, :connect, :options, :head].includes? verb %} + route :options, \{{*args}} + {% end %} end {% end %} diff --git a/src/amber/router/route.cr b/src/amber/router/route.cr index ef00ce67d..62adf3aaa 100644 --- a/src/amber/router/route.cr +++ b/src/amber/router/route.cr @@ -28,10 +28,6 @@ module Amber "#{verb.to_s.downcase}#{scope}#{resource}" end - def trail_head - "head#{scope}#{resource}" - end - def call(context) handler.call(context) end diff --git a/src/amber/router/router.cr b/src/amber/router/router.cr index 4373abb2c..6886804ce 100644 --- a/src/amber/router/router.cr +++ b/src/amber/router/router.cr @@ -2,8 +2,7 @@ require "amber_router" module Amber module Router - # This is the main application handler all routers should finally hit this - # handler. + # This is the main application handler all routers should finally hit this handler. class Router property :routes, :routes_hash, :socket_routes PATH_EXT_REGEX = /\.[^$\/]+$/ @@ -32,7 +31,6 @@ module Amber trail = build_node(route.verb, route.resource) @routes.add(route.trail, route) @routes_hash["#{route.controller.downcase}##{route.action.to_s.downcase}"] = route - add_head(route) if route.verb == "GET" end def add_socket_route(route, handler : WebSockets::Server::Handler) @@ -67,10 +65,6 @@ module Amber private def build_node(http_verb : Symbol | String, resource : String) "#{http_verb.to_s.downcase}#{resource}" end - - private def add_head(route) - @routes.add(route.trail_head, route) - end end end end