Skip to content

Commit

Permalink
Pass both connection key and header key to the callback function.
Browse files Browse the repository at this point in the history
  • Loading branch information
DevL committed Apr 6, 2015
1 parent 139daae commit 7ef8702
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ defmodule MyPhoenixApp.MyOtherController do
|> text "The API key used is: #{conn.assigns[:api_key]}"
end

def handle_missing_header(conn, missing_header_key) do
def handle_missing_header(conn, {_connection_assignment_key, missing_header_key}) do
conn
|> send_resp(Status.code(:bad_request), "Missing header: #{missing_header_key}")
|> halt
Expand All @@ -81,7 +81,7 @@ avoiding this is to have your callback function pattern match on the state of th
plug PlugRequireHeader, headers: [api_key: "x-api-key", secret: "x-secret"], on_missing: {__MODULE__, :handle_missing_header}

def handle_missing_header(%Plug.Conn{state: :sent} = conn, _), do: conn
def handle_missing_header(conn, missing_header_key) do
def handle_missing_header(conn, {_connection_assignment_key, missing_header_key}) do
conn
|> send_resp(Status.code(:bad_request), "Missing header: #{missing_header_key}")
|> halt
Expand Down
4 changes: 2 additions & 2 deletions lib/plug_require_header.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule PlugRequireHeader do
import Plug.Conn
alias Plug.Conn.Status

@vsn "0.3.2"
@vsn "0.4.0"
@doc false
def version, do: @vsn

Expand Down Expand Up @@ -63,7 +63,7 @@ defmodule PlugRequireHeader do
defp extract_header_key(conn, {connection_key, header_key}, callback) do
case List.keyfind(conn.req_headers, header_key, 0) do
{^header_key, value} -> assign_connection_key(conn, connection_key, value)
_ -> callback.(conn, header_key)
_ -> callback.(conn, {connection_key, header_key})
end
end

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule PlugRequireHeader.Mixfile do
def project do
[
app: :plug_require_header,
version: "0.3.2",
version: "0.4.0",
name: "PlugRequireHeader",
source_url: "https://github.com/DevL/plug_require_header",
elixir: "~> 1.0",
Expand Down
10 changes: 3 additions & 7 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ defmodule TestAppWithCallback do
send_resp(conn, Status.code(:ok), "#{conn.assigns[:api_key]}")
end

def callback(conn, missing_header_key) do
def callback(conn, {_, missing_header_key}) do
conn
|> send_resp(Status.code(:precondition_failed), "Missing header: #{missing_header_key}")
|> halt
Expand All @@ -50,11 +50,7 @@ defmodule TestAppWithCallbackAndMultipleRequiredHeaders do
send_resp(conn, Status.code(:ok), "API key: #{conn.assigns[:api_key]} and the secret #{conn.assigns[:secret]}")
end

def callback(conn, "x-api-key") do
conn |> assign :api_key, "not available"
end

def callback(conn, "x-secret") do
conn |> assign :secret, "is missing"
def callback(conn, {connection_key, _}) do
conn |> assign connection_key, "is missing"
end
end

0 comments on commit 7ef8702

Please sign in to comment.