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

Rewrite tests to be less integrated

Now mocking JSEX
This commit is contained in:
Eduardo Gurgel 2013-12-30 23:37:18 -03:00
parent 2f19e015b6
commit a049031f67
6 changed files with 31 additions and 18 deletions

View file

@ -17,8 +17,7 @@ defmodule HTTParrot.HeadersHandler do
end
defp response(headers) do
[headers: headers]
|> JSEX.encode!
[headers: headers] |> JSEX.encode!
end
def terminate(_, _, _), do: :ok

View file

@ -18,9 +18,7 @@ defmodule HTTParrot.IPHandler do
defp response(ip) do
ip = :inet_parse.ntoa(ip) |> to_string
[origin: ip]
|> JSEX.encode!
[origin: ip] |> JSEX.encode!
end
def terminate(_, _, _), do: :ok

View file

@ -5,21 +5,21 @@ defmodule HTTParrot.GetHandlerTest do
setup do
new HTTParrot.GeneralRequestInfo
new JSEX
end
teardown do
unload HTTParrot.GeneralRequestInfo
unload JSEX
end
test "returns prettified json with query values, headers, url and origin" do
qs_vals = [{"a", "b"}]
headers = [header1: "value 1", header2: "value 2"]
url = "http://localhost/get?a=b"
ip = "127.1.2.3"
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {[args: qs_vals, headers: headers, url: url, origin: ip], :req2})
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {:info, :req2})
expect(JSEX, :encode!, [{[:info], :json}])
assert get_json(:req1, :state) == {"{\"args\":{\"a\":\"b\"},\"headers\":{\"header1\":\"value 1\",\"header2\":\"value 2\"},\"url\":\"http://localhost/get?a=b\",\"origin\":\"127.1.2.3\"}", :req2, :state}
assert get_json(:req1, :state) == {:json, :req2, :state}
assert validate HTTParrot.GeneralRequestInfo
assert validate JSEX
end
end

View file

@ -5,16 +5,21 @@ defmodule HTTParrot.HeadersHandlerTest do
setup do
new :cowboy_req
new JSEX
end
teardown do
unload :cowboy_req
unload JSEX
end
test "returns prettified json with headers list" do
headers = [header1: "value 1", header2: "value 2"]
expect(:cowboy_req, :headers, 1, {headers, :req2})
assert get_json(:req1, :state) == {"{\"headers\":{\"header1\":\"value 1\",\"header2\":\"value 2\"}}", :req2, :state}
expect(:cowboy_req, :headers, 1, {:headers, :req2})
expect(JSEX, :encode!, [{[[headers: :headers]], :json}])
assert get_json(:req1, :state) == {:json, :req2, :state}
assert validate :cowboy_req
assert validate JSEX
end
end

View file

@ -5,16 +5,22 @@ defmodule HTTParrot.IPHandlerTest do
setup do
new :cowboy_req
new JSEX
end
teardown do
unload :cowboy_req
unload JSEX
end
test "returns prettified json with origin" do
ip = {127, 1, 2, 3}
expect(:cowboy_req, :peer, 1, {{ip, :host}, :req2})
assert get_json(:req1, :state) == {"{\"origin\":\"127.1.2.3\"}", :req2, :state}
expect(JSEX, :encode!, [{[[origin: "127.1.2.3"]], :json}])
assert get_json(:req1, :state) == {:json, :req2, :state}
assert validate :cowboy_req
assert validate JSEX
end
end

View file

@ -5,16 +5,21 @@ defmodule HTTParrot.UserAgentHandlerTest do
setup do
new :cowboy_req
new JSEX
end
teardown do
unload :cowboy_req
unload JSEX
end
test "returns prettified json with user agent" do
user_agent = "Mozilla Chrome"
expect(:cowboy_req, :header, [{["user-agent", :req1, "null"], {user_agent, :req2}}])
assert get_json(:req1, :state) == {"{\"user-agent\":\"Mozilla Chrome\"}", :req2, :state}
expect(:cowboy_req, :header, [{["user-agent", :req1, "null"], {:user_agent, :req2}}])
expect(JSEX, :encode!, [{[[{"user-agent", :user_agent}]], :json}])
assert get_json(:req1, :state) == {:json, :req2, :state}
assert validate :cowboy_req
assert validate JSEX
end
end