diff --git a/lib/httparrot/general_request_info.ex b/lib/httparrot/general_request_info.ex new file mode 100644 index 0000000..e80189a --- /dev/null +++ b/lib/httparrot/general_request_info.ex @@ -0,0 +1,13 @@ +defmodule HTTParrot.GeneralRequestInfo do + def retrieve(req) do + {args, req} = :cowboy_req.qs_vals(req) + {headers, req} = :cowboy_req.headers(req) + {url, req} = :cowboy_req.url(req) + {{ip, _port}, req} = :cowboy_req.peer(req) + + ip = :inet_parse.ntoa(ip) |> to_string + if args == [], do: args = [{}] + + {[args: args, headers: headers, url: url, origin: ip], req} + end +end diff --git a/lib/httparrot/get_handler.ex b/lib/httparrot/get_handler.ex index 5468a24..2dc56fc 100644 --- a/lib/httparrot/get_handler.ex +++ b/lib/httparrot/get_handler.ex @@ -1,4 +1,6 @@ defmodule HTTParrot.GetHandler do + alias HTTParrot.GeneralRequestInfo + def init(_transport, _req, _opts) do {:upgrade, :protocol, :cowboy_rest} end @@ -12,19 +14,12 @@ defmodule HTTParrot.GetHandler do end def get_json(req, state) do - {args, req} = :cowboy_req.qs_vals(req) - {headers, req} = :cowboy_req.headers(req) - {url, req} = :cowboy_req.url(req) - {{ip, _port}, req} = :cowboy_req.peer(req) - {response(args, headers, url, ip), req, state} + {info, req} = GeneralRequestInfo.retrieve(req) + {response(info), req, state} end - defp response(args, headers, url, ip) do - ip = :inet_parse.ntoa(ip) |> to_string - if args == [], do: args = [{}] - - [args: args, headers: headers, url: url, origin: ip] - |> JSEX.encode! + defp response(info) do + info |> JSEX.encode! end def terminate(_, _, _), do: :ok diff --git a/test/general_request_info_test.exs b/test/general_request_info_test.exs new file mode 100644 index 0000000..9cbd32b --- /dev/null +++ b/test/general_request_info_test.exs @@ -0,0 +1,43 @@ +defmodule HTTParrot.GeneralRequestInfoTest do + use ExUnit.Case + import :meck + import HTTParrot.GeneralRequestInfo + + setup do + new :cowboy_req + end + + teardown do + unload :cowboy_req + end + + test "returns a list of args, headers, url and original ip" do + qs_vals = [{"a", "b"}] + headers = [header1: "value 1", header2: "value 2"] + ip = {127, 1, 2, 3} + url = "http://localhost/get?a=b" + expect(:cowboy_req, :qs_vals, 1, {qs_vals, :req2}) + expect(:cowboy_req, :headers, 1, {headers, :req3}) + expect(:cowboy_req, :url, 1, {url, :req4}) + expect(:cowboy_req, :peer, 1, {{ip, :host}, :req5}) + + assert retrieve(:req1) == {[args: qs_vals, headers: headers, url: url, origin: "127.1.2.3"], :req5} + + assert validate :cowboy_req + end + + test "returns a list of empty args, headers, url and original ip" do + qs_vals = [] + headers = [header1: "value 1", header2: "value 2"] + ip = {127, 1, 2, 3} + url = "http://localhost/get" + expect(:cowboy_req, :qs_vals, 1, {qs_vals, :req2}) + expect(:cowboy_req, :headers, 1, {headers, :req3}) + expect(:cowboy_req, :url, 1, {url, :req4}) + expect(:cowboy_req, :peer, 1, {{ip, :host}, :req5}) + + assert retrieve(:req1) == {[args: [{}], headers: headers, url: url, origin: "127.1.2.3"], :req5} + + assert validate :cowboy_req + end +end diff --git a/test/get_handler_test.exs b/test/get_handler_test.exs index baa559a..d86a56e 100644 --- a/test/get_handler_test.exs +++ b/test/get_handler_test.exs @@ -4,40 +4,22 @@ defmodule HTTParrot.GetHandlerTest do import HTTParrot.GetHandler setup do - new :cowboy_req + new HTTParrot.GeneralRequestInfo end teardown do - unload :cowboy_req + unload HTTParrot.GeneralRequestInfo end test "returns prettified json with query values, headers, url and origin" do qs_vals = [{"a", "b"}] headers = [header1: "value 1", header2: "value 2"] - ip = {127, 1, 2, 3} url = "http://localhost/get?a=b" - expect(:cowboy_req, :qs_vals, 1, {qs_vals, :req2}) - expect(:cowboy_req, :headers, 1, {headers, :req2}) - expect(:cowboy_req, :url, 1, {url, :req2}) - expect(:cowboy_req, :peer, 1, {{ip, :host}, :req2}) + ip = "127.1.2.3" + expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {[args: qs_vals, headers: headers, url: url, origin: ip], :req2}) 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 validate :cowboy_req - end - - test "returns prettified json with no query value, headers, url and origin" do - qs_vals = [] - headers = [header1: "value 1", header2: "value 2"] - ip = {127, 1, 2, 3} - url = "http://localhost/get" - expect(:cowboy_req, :qs_vals, 1, {qs_vals, :req2}) - expect(:cowboy_req, :headers, 1, {headers, :req2}) - expect(:cowboy_req, :url, 1, {url, :req2}) - expect(:cowboy_req, :peer, 1, {{ip, :host}, :req2}) - - assert get_json(:req1, :state) == {"{\"args\":{},\"headers\":{\"header1\":\"value 1\",\"header2\":\"value 2\"},\"url\":\"http://localhost/get\",\"origin\":\"127.1.2.3\"}", :req2, :state} - - assert validate :cowboy_req + assert validate HTTParrot.GeneralRequestInfo end end