diff --git a/lib/httparrot/headers_handler.ex b/lib/httparrot/headers_handler.ex index 1adc978..ba6f346 100644 --- a/lib/httparrot/headers_handler.ex +++ b/lib/httparrot/headers_handler.ex @@ -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 diff --git a/lib/httparrot/ip_handler.ex b/lib/httparrot/ip_handler.ex index d92a94e..bc3bb9f 100644 --- a/lib/httparrot/ip_handler.ex +++ b/lib/httparrot/ip_handler.ex @@ -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 diff --git a/test/get_handler_test.exs b/test/get_handler_test.exs index d86a56e..4bc4e1d 100644 --- a/test/get_handler_test.exs +++ b/test/get_handler_test.exs @@ -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 diff --git a/test/headers_handler_test.exs b/test/headers_handler_test.exs index 0a625f8..7489524 100644 --- a/test/headers_handler_test.exs +++ b/test/headers_handler_test.exs @@ -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 diff --git a/test/ip_handler_test.exs b/test/ip_handler_test.exs index 54d7c91..c08d1aa 100644 --- a/test/ip_handler_test.exs +++ b/test/ip_handler_test.exs @@ -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 diff --git a/test/user_agent_test.exs b/test/user_agent_test.exs index 3f68305..c3bf046 100644 --- a/test/user_agent_test.exs +++ b/test/user_agent_test.exs @@ -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