Created
July 18, 2014 13:57
-
-
Save knewter/49b12aedae3226c52cf3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
defmodule ReversingPlug do | |
use Plug.Builder | |
import Plug.Conn | |
plug :hello | |
plug :reverse | |
plug :sender | |
def hello(conn, _opts) do | |
%Plug.Conn{conn | resp_body: "Hello world"} | |
end | |
def reverse(conn, _opts) do | |
%Plug.Conn{conn | resp_body: String.reverse(conn.resp_body)} | |
end | |
def sender(conn, _opts) do | |
conn | |
|> put_resp_content_type("text/plain") | |
|> send_resp(200, conn.resp_body) | |
end | |
end | |
defmodule ReversingPlugTest do | |
use ExUnit.Case, async: true | |
use Plug.Test | |
@opts ReversingPlug.init([]) | |
test "returns hello world" do | |
conn = conn(:get, "/") | |
conn = ReversingPlug.call(conn, @opts) | |
assert conn.state == :sent | |
assert conn.status == 200 | |
assert conn.resp_body == "dlrow olleH" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment