mirror of
https://github.com/edgurgel/httparrot
synced 2025-04-05 08:12:31 -04:00
Extract general stuff from /get /post /delete /put response
GeneralRequestInfo will take care of it
This commit is contained in:
parent
b07b3769de
commit
859133ba58
4 changed files with 67 additions and 34 deletions
13
lib/httparrot/general_request_info.ex
Normal file
13
lib/httparrot/general_request_info.ex
Normal file
|
@ -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
|
|
@ -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
|
||||
|
|
43
test/general_request_info_test.exs
Normal file
43
test/general_request_info_test.exs
Normal file
|
@ -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
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue