Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Router] Adds OPTIONS and HEAD routes by default #700

Merged
merged 4 commits into from
Mar 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion spec/amber/router/route_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions spec/amber/router/router_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 7 additions & 0 deletions src/amber/dsl/router.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}

Expand Down
4 changes: 0 additions & 4 deletions src/amber/router/route.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 1 addition & 7 deletions src/amber/router/router.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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 = /\.[^$\/]+$/
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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