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

Add '/delay/:n'

This commit is contained in:
Eduardo Gurgel 2014-01-05 20:32:14 -03:00
parent a64303b0be
commit 5653bc939c
3 changed files with 95 additions and 0 deletions

View file

@ -19,6 +19,7 @@ defmodule HTTParrot do
{'/basic-auth/:user/:passwd', HTTParrot.BasicAuthHandler, []},
{'/hidden-basic-auth/:user/:passwd', HTTParrot.HiddenBasicAuthHandler, []},
{'/stream/:n', HTTParrot.StreamHandler, []},
{'/delay/:n', HTTParrot.DelayedHandler, []},
{'/html', :cowboy_static, {:priv_file, :httparrot, "html.html"}} ] }
])
{:ok, port} = :application.get_env(:httparrot, :port)

View file

@ -0,0 +1,37 @@
defmodule HTTParrot.DelayedHandler do
alias HTTParrot.GeneralRequestInfo
def init(_transport, _req, _opts) do
{:upgrade, :protocol, :cowboy_rest}
end
def allowed_methods(req, state) do
{["GET"], req, state}
end
def malformed_request(req, state) do
{n, req} = :cowboy_req.binding(:n, req)
try do
n = n |> binary_to_integer |> min(10) |> max(0)
{false, req, n}
rescue
ArgumentError -> {true, req, state}
end
end
def content_types_provided(req, state) do
{[{{"application", "json", []}, :get_json}], req, state}
end
def get_json(req, n) do
{info, req} = GeneralRequestInfo.retrieve(req)
:timer.sleep(n*1000)
{response(info), req, n}
end
defp response(info) do
info |> JSEX.encode!
end
def terminate(_, _, _), do: :ok
end

View file

@ -0,0 +1,57 @@
defmodule HTTParrot.DelayedHandlerTest do
use ExUnit.Case
import :meck
import HTTParrot.DelayedHandler
setup do
new HTTParrot.GeneralRequestInfo
new JSEX
end
teardown do
unload HTTParrot.GeneralRequestInfo
unload JSEX
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 0 if 'n' is less than 0" do
expect(:cowboy_req, :binding, [{[:n, :req1], {"-1", :req2}}])
assert malformed_request(:req1, :state) == {false, :req2, 0}
assert validate :cowboy_req
end
test "malformed_request returns 10 if 'n' is greater than 10" do
expect(:cowboy_req, :binding, [{[:n, :req1], {"20", :req2}}])
assert malformed_request(:req1, :state) == {false, :req2, 10}
assert validate :cowboy_req
end
test "returns json with query values, headers, url and origin" do
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {:info, :req2})
expect(JSEX, :encode!, [{[:info], :json}])
assert get_json(:req1, 0) == {:json, :req2, 0}
assert validate HTTParrot.GeneralRequestInfo
assert validate JSEX
end
end