1
0
Fork 0
mirror of https://github.com/edgurgel/httparrot synced 2025-04-05 08:12:31 -04:00

Add '/redirect-to'

This commit is contained in:
Eduardo Gurgel 2013-12-28 16:03:29 -03:00
parent e3a1d06470
commit c3b6c4479f
3 changed files with 50 additions and 1 deletions

View file

@ -7,7 +7,8 @@ defmodule HTTParrot do
{'/user-agent', HTTParrot.UserAgentHandler, []},
{'/headers', HTTParrot.HeadersHandler, []},
{'/get', HTTParrot.GetHandler, []},
{'/status/:code', HTTParrot.StatusCodeHandler, []} ] }
{'/status/:code', HTTParrot.StatusCodeHandler, []},
{'/redirect-to', HTTParrot.RedirectToHandler, []} ] }
])
{:ok, port} = :application.get_env(:httparrot, :port)
{:ok, _} = :cowboy.start_http(:http, 100, [port: port], [env: [dispatch: dispatch], onresponse: &prettify_json/4])

View file

@ -0,0 +1,23 @@
defmodule HTTParrot.RedirectToHandler do
def init(_transport, _req, _opts) do
{:upgrade, :protocol, :cowboy_rest}
end
def allowed_methods(req, state) do
{["GET", "HEAD", "OPTIONS"], req, state}
end
def malformed_request(req, state) do
{url, req} = :cowboy_req.qs_val("url", req, nil)
if url, do: {false, req, url}, else: {true, req, state}
end
def resource_exists(req, state), do: {false, req, state}
def previously_existed(req, state), do: {true, req, state}
def moved_permanently(req, url) do
{{true, url}, req, url}
end
def terminate(_, _, _), do: :ok
end

View file

@ -0,0 +1,25 @@
defmodule HTTParrot.RedirectToHandlerTest do
use ExUnit.Case
import :meck
import HTTParrot.RedirectToHandler
setup do
new :cowboy_req
end
teardown do
unload :cowboy_req
end
test "malformed_request returns true if no 'url' is defined" do
expect(:cowboy_req, :qs_val, [{["url", :req1, nil], {:url, :req2}}])
assert malformed_request(:req1, :state) == {false, :req2, :url}
assert validate :cowboy_req
end
test "malformed_request returns false if no 'url' is defined" do
expect(:cowboy_req, :qs_val, [{["url", :req1, nil], {nil, :req2}}])
assert malformed_request(:req1, :state) == {true, :req2, :state}
assert validate :cowboy_req
end
end