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

Add '/stream/:n'

This commit is contained in:
Eduardo Gurgel 2014-01-05 16:36:01 -03:00
parent 704b38006d
commit c25bd1c886
2 changed files with 68 additions and 0 deletions

View file

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

View file

@ -0,0 +1,67 @@
defmodule HTTParrot.StreamHandlerTest do
use ExUnit.Case
import :meck
import HTTParrot.StreamHandler
setup do
new :cowboy_req
new HTTParrot.GeneralRequestInfo
new JSEX
end
teardown do
unload :cowboy_req
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 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 "malformed_request returns 100 if 'n' is greater than 100" do
expect(:cowboy_req, :binding, [{[:n, :req1], {"200", :req2}}])
assert malformed_request(:req1, :state) == {false, :req2, 100}
assert validate :cowboy_req
end
test "response must stream chunks" do
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {[:info], :req2})
expect(JSEX, :encode!, [{[[{:id, 0}, :info]], :json1},
{[[{:id, 1}, :info]], :json2}])
assert {{:chunked, func}, :req2, nil} = get_json(:req1, 2)
assert is_function(func)
send_func = fn(body) -> self <- {:chunk, body} end
func.(send_func)
assert_receive {:chunk, :json1}
assert_receive {:chunk, :json2}
assert validate HTTParrot.GeneralRequestInfo
assert validate JSEX
end
end