-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from DevL/additional_options
Enable extraction multiple headers and configure callback for missing headers.
- Loading branch information
Showing
5 changed files
with
160 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,50 @@ | ||
ExUnit.start() | ||
|
||
defmodule AppMaker do | ||
defmacro __using__(options) do | ||
quote do | ||
use Plug.Router | ||
alias Plug.Conn.Status | ||
|
||
plug PlugRequireHeader, unquote(options) | ||
plug :match | ||
plug :dispatch | ||
end | ||
end | ||
end | ||
|
||
defmodule TestApp do | ||
use Plug.Router | ||
alias Plug.Conn.Status | ||
use AppMaker, headers: [api_key: "x-api-key"] | ||
|
||
plug PlugRequireHeader, api_key: "x-api-key" | ||
plug :match | ||
plug :dispatch | ||
get "/" do | ||
send_resp(conn, Status.code(:ok), "API key: #{conn.assigns[:api_key]}") | ||
end | ||
end | ||
|
||
defmodule TestAppWithCallback do | ||
use AppMaker, headers: [api_key: "x-api-key"], on_missing: {__MODULE__, :callback} | ||
|
||
get "/" do | ||
send_resp(conn, Status.code(:ok), "#{conn.assigns[:api_key]}") | ||
end | ||
|
||
def callback(conn, missing_header_key) do | ||
conn | ||
|> send_resp(Status.code(:precondition_failed), "Missing header: #{missing_header_key}") | ||
|> halt | ||
end | ||
end | ||
|
||
defmodule TestAppWithCallbackAndMultipleRequiredHeaders do | ||
use AppMaker, headers: [api_key: "x-api-key", secret: "x-secret"], on_missing: {__MODULE__, :callback} | ||
|
||
get "/" do | ||
send_resp(conn, Status.code(:ok), "API key: #{conn.assigns[:api_key]} and the secret #{conn.assigns[:secret]}") | ||
end | ||
|
||
def callback(conn, missing_header_key) do | ||
conn | ||
|> send_resp(Status.code(:bad_request), "Missing header: #{missing_header_key}") | ||
|> halt | ||
end | ||
end |