Skip to content

Instantly share code, notes, and snippets.

@knewter
Created July 18, 2014 13:57
Show Gist options
  • Save knewter/49b12aedae3226c52cf3 to your computer and use it in GitHub Desktop.
Save knewter/49b12aedae3226c52cf3 to your computer and use it in GitHub Desktop.
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