Skip to content

Commit

Permalink
Add router macro for HEAD requests (#962)
Browse files Browse the repository at this point in the history
  • Loading branch information
LostKobrakai authored Jul 12, 2020
1 parent e54586a commit 6dd82ea
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/plug/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,14 @@ defmodule Plug.Router do
compile(:get, path, options, contents)
end

@doc """
Dispatches to the path only if the request is a HEAD request.
See `match/3` for more examples.
"""
defmacro head(path, options, contents \\ []) do
compile(:head, path, options, contents)
end

@doc """
Dispatches to the path only if the request is a POST request.
See `match/3` for more examples.
Expand Down
9 changes: 9 additions & 0 deletions test/plug/router_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ defmodule Plug.RouterTest do
resp(conn, 200, inspect(conn.assigns))
end

head "/head" do
resp(conn, 200, "")
end

post "/post" do
resp(conn, 200, "")
end
Expand Down Expand Up @@ -451,6 +455,11 @@ defmodule Plug.RouterTest do
assert conn.resp_body =~ ~s(an_option: :a_value)
end

test "declare and call HEAD requests" do
conn = call(Sample, conn(:head, "/head"))
assert conn.status == 200
end

test "declare and call POST requests" do
conn = call(Sample, conn(:post, "/post"))
assert conn.status == 200
Expand Down

0 comments on commit 6dd82ea

Please sign in to comment.