mirror of
https://github.com/edgurgel/httparrot
synced 2025-04-05 08:12:31 -04:00
Add '/relative-redirect/:n'
This commit is contained in:
parent
059e60c3ca
commit
42a093cc32
3 changed files with 74 additions and 0 deletions
|
@ -15,6 +15,7 @@ defmodule HTTParrot do
|
|||
{'/status/:code', HTTParrot.StatusCodeHandler, []},
|
||||
{'/redirect/:n', HTTParrot.RedirectHandler, []},
|
||||
{'/redirect-to', HTTParrot.RedirectToHandler, []},
|
||||
{'/relative-redirect/:n', HTTParrot.RelativeRedirectHandler, []},
|
||||
{'/cookies', HTTParrot.CookiesHandler, []},
|
||||
{'/cookies/set', HTTParrot.SetCookiesHandler, []},
|
||||
{'/cookies/delete', HTTParrot.DeleteCookiesHandler, []},
|
||||
|
|
27
lib/httparrot/relative_redirect_handler.ex
Normal file
27
lib/httparrot/relative_redirect_handler.ex
Normal file
|
@ -0,0 +1,27 @@
|
|||
defmodule HTTParrot.RelativeRedirectHandler do
|
||||
@moduledoc """
|
||||
Redirects to the relative foo URL.
|
||||
"""
|
||||
|
||||
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
|
||||
HTTParrot.RedirectHandler.malformed_request(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, n) do
|
||||
url = if n > 1, do: "/redirect/#{n-1}", else: "/get"
|
||||
{{true, url}, req, nil}
|
||||
end
|
||||
|
||||
def terminate(_, _, _), do: :ok
|
||||
end
|
46
test/relative_redirect_handler_test.exs
Normal file
46
test/relative_redirect_handler_test.exs
Normal file
|
@ -0,0 +1,46 @@
|
|||
defmodule HTTParrot.RelativeRedirectHandlerTest do
|
||||
use ExUnit.Case
|
||||
import :meck
|
||||
import HTTParrot.RelativeRedirectHandler
|
||||
|
||||
setup do
|
||||
new :cowboy_req
|
||||
end
|
||||
|
||||
teardown do
|
||||
unload :cowboy_req
|
||||
end
|
||||
|
||||
test "malformed_request returns false if it's not an integer" do
|
||||
expect(:cowboy_req, :binding, [{[:n, :req1], {"a2B=", :req2}}])
|
||||
|
||||
assert malformed_request(:req1, :state) == {true, :req2, :state}
|
||||
|
||||
assert validate :cowboy_req
|
||||
end
|
||||
|
||||
test "malformed_request returns false if it's an integer" do
|
||||
expect(:cowboy_req, :binding, [{[:n, :req1], {"2", :req2}}])
|
||||
|
||||
assert malformed_request(:req1, :state) == {false, :req2, 2}
|
||||
|
||||
assert validate :cowboy_req
|
||||
end
|
||||
|
||||
test "malformed_request returns 1 if 'n' is less than 1" do
|
||||
expect(:cowboy_req, :binding, [{[:n, :req1], {"0", :req2}}])
|
||||
|
||||
assert malformed_request(:req1, :state) == {false, :req2, 1}
|
||||
|
||||
assert validate :cowboy_req
|
||||
end
|
||||
|
||||
test "moved_permanently returns 'redirect/n-1' if n > 1" do
|
||||
assert moved_permanently(:req1, 4) == {{true, "/redirect/3"}, :req1, nil}
|
||||
end
|
||||
|
||||
test "moved_permanently returns '/get' if n = 1" do
|
||||
assert moved_permanently(:req1, 1) == {{true, "/get"}, :req1, nil}
|
||||
end
|
||||
|
||||
end
|
Loading…
Add table
Reference in a new issue