mirror of
https://github.com/edgurgel/httparrot
synced 2025-04-06 00:32:34 -04:00
Update JSEX to exjsx
This commit is contained in:
parent
8d693e8073
commit
d0f977d285
29 changed files with 88 additions and 88 deletions
|
@ -57,8 +57,8 @@ defmodule HTTParrot do
|
||||||
def stop(_State), do: :ok
|
def stop(_State), do: :ok
|
||||||
|
|
||||||
def prettify_json(status, headers, body, req) do
|
def prettify_json(status, headers, body, req) do
|
||||||
if JSEX.is_json? body do
|
if JSX.is_json? body do
|
||||||
body = JSEX.prettify!(body)
|
body = JSX.prettify!(body)
|
||||||
headers = List.keystore(headers, "content-length", 0, {"content-length", Integer.to_char_list(String.length(body))})
|
headers = List.keystore(headers, "content-length", 0, {"content-length", Integer.to_char_list(String.length(body))})
|
||||||
{:ok, req} = :cowboy_req.reply(status, headers, body, req)
|
{:ok, req} = :cowboy_req.reply(status, headers, body, req)
|
||||||
end
|
end
|
||||||
|
|
|
@ -23,6 +23,6 @@ defmodule HTTParrot.BasicAuthHandler do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp response(user) do
|
defp response(user) do
|
||||||
[authenticated: true, user: user] |> JSEX.encode!
|
[authenticated: true, user: user] |> JSX.encode!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -19,6 +19,6 @@ defmodule HTTParrot.CacheHandler do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp response(info) do
|
defp response(info) do
|
||||||
info |> JSEX.encode!
|
info |> JSX.encode!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -15,6 +15,6 @@ defmodule HTTParrot.CookiesHandler do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp response(cookies) do
|
defp response(cookies) do
|
||||||
[cookies: cookies] |> JSEX.encode!
|
[cookies: cookies] |> JSX.encode!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -23,6 +23,6 @@ defmodule HTTParrot.DelayedHandler do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp response(info) do
|
defp response(info) do
|
||||||
info |> JSEX.encode!
|
info |> JSX.encode!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -12,6 +12,6 @@ defmodule HTTParrot.DeleteHandler do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp response(info) do
|
defp response(info) do
|
||||||
info |> JSEX.encode!
|
info |> JSX.encode!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -15,6 +15,6 @@ defmodule HTTParrot.GetHandler do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp response(info) do
|
defp response(info) do
|
||||||
info |> JSEX.encode!
|
info |> JSX.encode!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -12,11 +12,11 @@ defmodule HTTParrot.GzipHandler do
|
||||||
def get_json(req, state) do
|
def get_json(req, state) do
|
||||||
{info, req} = GeneralRequestInfo.retrieve(req)
|
{info, req} = GeneralRequestInfo.retrieve(req)
|
||||||
req = :cowboy_req.set_resp_header("content-encoding", "gzip", req)
|
req = :cowboy_req.set_resp_header("content-encoding", "gzip", req)
|
||||||
response = response(info) |> JSEX.prettify! |> :zlib.gzip
|
response = response(info) |> JSX.prettify! |> :zlib.gzip
|
||||||
{response, req, state}
|
{response, req, state}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp response(info) do
|
defp response(info) do
|
||||||
info |> JSEX.encode!
|
info |> JSX.encode!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -11,6 +11,6 @@ defmodule HTTParrot.HeadersHandler do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp response(headers) do
|
defp response(headers) do
|
||||||
[headers: headers] |> JSEX.encode!
|
[headers: headers] |> JSX.encode!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -26,6 +26,6 @@ defmodule HTTParrot.HiddenBasicAuthHandler do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp response(user) do
|
defp response(user) do
|
||||||
[authenticated: true, user: user] |> JSEX.encode!
|
[authenticated: true, user: user] |> JSX.encode!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -15,6 +15,6 @@ defmodule HTTParrot.IPHandler do
|
||||||
|
|
||||||
defp response(ip) do
|
defp response(ip) do
|
||||||
ip = :inet_parse.ntoa(ip) |> to_string
|
ip = :inet_parse.ntoa(ip) |> to_string
|
||||||
[origin: ip] |> JSEX.encode!
|
[origin: ip] |> JSX.encode!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -37,8 +37,8 @@ defmodule HTTParrot.PHandler do
|
||||||
def post_binary(req, _state) do
|
def post_binary(req, _state) do
|
||||||
{:ok, body, req} = handle_binary(req)
|
{:ok, body, req} = handle_binary(req)
|
||||||
if String.valid?(body) do
|
if String.valid?(body) do
|
||||||
if JSEX.is_json?(body) do
|
if JSX.is_json?(body) do
|
||||||
post(req, [form: [{}], data: body, json: JSEX.decode!(body)])
|
post(req, [form: [{}], data: body, json: JSX.decode!(body)])
|
||||||
else
|
else
|
||||||
post(req, [form: [{}], data: body, json: nil])
|
post(req, [form: [{}], data: body, json: nil])
|
||||||
end
|
end
|
||||||
|
@ -65,7 +65,7 @@ defmodule HTTParrot.PHandler do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp response(info, body) do
|
defp response(info, body) do
|
||||||
info ++ body |> JSEX.encode!
|
info ++ body |> JSX.encode!
|
||||||
end
|
end
|
||||||
|
|
||||||
def post_multipart(req, _state) do
|
def post_multipart(req, _state) do
|
||||||
|
|
|
@ -30,7 +30,7 @@ defmodule HTTParrot.StreamHandler do
|
||||||
defp stream_response(n, info) do
|
defp stream_response(n, info) do
|
||||||
fn(send_func) ->
|
fn(send_func) ->
|
||||||
Enum.each 0..n-1, fn (i) ->
|
Enum.each 0..n-1, fn (i) ->
|
||||||
send_func.([id: i] ++ info |> JSEX.encode!)
|
send_func.([id: i] ++ info |> JSX.encode!)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -15,6 +15,6 @@ defmodule HTTParrot.UserAgentHandler do
|
||||||
|
|
||||||
defp response(user_agent) do
|
defp response(user_agent) do
|
||||||
[{"user-agent", user_agent}]
|
[{"user-agent", user_agent}]
|
||||||
|> JSEX.encode!
|
|> JSX.encode!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
4
mix.exs
4
mix.exs
|
@ -19,14 +19,14 @@ defmodule Httparrot.Mixfile do
|
||||||
[ applications: [ :compiler,
|
[ applications: [ :compiler,
|
||||||
:syntax_tools,
|
:syntax_tools,
|
||||||
:cowboy,
|
:cowboy,
|
||||||
:jsex ],
|
:exjsx ],
|
||||||
mod: { HTTParrot, [] },
|
mod: { HTTParrot, [] },
|
||||||
env: [ http_port: 8080, ssl: true, https_port: 8433 ] ]
|
env: [ http_port: 8080, ssl: true, https_port: 8433 ] ]
|
||||||
end
|
end
|
||||||
|
|
||||||
defp deps do
|
defp deps do
|
||||||
[ {:cowboy, "~> 1.0.0"},
|
[ {:cowboy, "~> 1.0.0"},
|
||||||
{:jsex, "~> 2.0"},
|
{:exjsx, "~> 3.0"},
|
||||||
{:meck, "~> 0.8.2", only: :test } ]
|
{:meck, "~> 0.8.2", only: :test } ]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
12
mix.lock
12
mix.lock
|
@ -1,6 +1,6 @@
|
||||||
%{"cowboy": {:package, "1.0.0"},
|
%{"cowboy": {:hex, :cowboy, "1.0.0"},
|
||||||
"cowlib": {:package, "1.0.0"},
|
"cowlib": {:hex, :cowlib, "1.0.0"},
|
||||||
"jsex": {:package, "2.0.0"},
|
"exjsx": {:hex, :exjsx, "3.1.0"},
|
||||||
"jsx": {:package, "2.0.2"},
|
"jsx": {:hex, :jsx, "2.4.0"},
|
||||||
"meck": {:package, "0.8.2"},
|
"meck": {:hex, :meck, "0.8.2"},
|
||||||
"ranch": {:package, "1.0.0"}}
|
"ranch": {:hex, :ranch, "1.0.0"}}
|
||||||
|
|
|
@ -5,7 +5,7 @@ defmodule HTTParrot.BasicAuthHandlerTest do
|
||||||
|
|
||||||
setup do
|
setup do
|
||||||
new :cowboy_req
|
new :cowboy_req
|
||||||
new JSEX
|
new JSX
|
||||||
on_exit fn -> unload end
|
on_exit fn -> unload end
|
||||||
:ok
|
:ok
|
||||||
end
|
end
|
||||||
|
@ -18,7 +18,7 @@ defmodule HTTParrot.BasicAuthHandlerTest do
|
||||||
assert is_authorized(:req1, :state) == {true, :req4, :user}
|
assert is_authorized(:req1, :state) == {true, :req4, :user}
|
||||||
|
|
||||||
assert validate :cowboy_req
|
assert validate :cowboy_req
|
||||||
assert validate JSEX
|
assert validate JSX
|
||||||
end
|
end
|
||||||
|
|
||||||
test "is_authorized returns false if user and passwd doesnt match" do
|
test "is_authorized returns false if user and passwd doesnt match" do
|
||||||
|
@ -29,14 +29,14 @@ defmodule HTTParrot.BasicAuthHandlerTest do
|
||||||
assert is_authorized(:req1, :state) == {{false, "Basic realm=\"Fake Realm\""}, :req4, :state}
|
assert is_authorized(:req1, :state) == {{false, "Basic realm=\"Fake Realm\""}, :req4, :state}
|
||||||
|
|
||||||
assert validate :cowboy_req
|
assert validate :cowboy_req
|
||||||
assert validate JSEX
|
assert validate JSX
|
||||||
end
|
end
|
||||||
|
|
||||||
test "returns user and if it's authenticated" do
|
test "returns user and if it's authenticated" do
|
||||||
expect(JSEX, :encode!, [{[[authenticated: true, user: :user]], :json}])
|
expect(JSX, :encode!, [{[[authenticated: true, user: :user]], :json}])
|
||||||
|
|
||||||
assert get_json(:req1, :user) == {:json, :req1, nil}
|
assert get_json(:req1, :user) == {:json, :req1, nil}
|
||||||
|
|
||||||
assert validate JSEX
|
assert validate JSX
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,18 +5,18 @@ defmodule HTTParrot.CacheHandlerTest do
|
||||||
|
|
||||||
setup do
|
setup do
|
||||||
new HTTParrot.GeneralRequestInfo
|
new HTTParrot.GeneralRequestInfo
|
||||||
new JSEX
|
new JSX
|
||||||
on_exit fn -> unload end
|
on_exit fn -> unload end
|
||||||
:ok
|
:ok
|
||||||
end
|
end
|
||||||
|
|
||||||
test "returns prettified json with query values, headers, url and origin" do
|
test "returns prettified json with query values, headers, url and origin" do
|
||||||
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {:info, :req2})
|
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {:info, :req2})
|
||||||
expect(JSEX, :encode!, [{[:info], :json}])
|
expect(JSX, :encode!, [{[:info], :json}])
|
||||||
|
|
||||||
assert get_json(:req1, :state) == {:json, :req2, :state}
|
assert get_json(:req1, :state) == {:json, :req2, :state}
|
||||||
|
|
||||||
assert validate HTTParrot.GeneralRequestInfo
|
assert validate HTTParrot.GeneralRequestInfo
|
||||||
assert validate JSEX
|
assert validate JSX
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,18 +5,18 @@ defmodule HTTParrot.CookiesHandlerTest do
|
||||||
|
|
||||||
setup do
|
setup do
|
||||||
new :cowboy_req
|
new :cowboy_req
|
||||||
new JSEX
|
new JSX
|
||||||
on_exit fn -> unload end
|
on_exit fn -> unload end
|
||||||
:ok
|
:ok
|
||||||
end
|
end
|
||||||
|
|
||||||
test "returns a JSON with all the cookies o/" do
|
test "returns a JSON with all the cookies o/" do
|
||||||
expect(:cowboy_req, :cookies, 1, {[k1: :v1], :req2})
|
expect(:cowboy_req, :cookies, 1, {[k1: :v1], :req2})
|
||||||
expect(JSEX, :encode!, [{[[cookies: [k1: :v1]]], :json}])
|
expect(JSX, :encode!, [{[[cookies: [k1: :v1]]], :json}])
|
||||||
|
|
||||||
assert get_json(:req1, :state) == {:json, :req2, :state}
|
assert get_json(:req1, :state) == {:json, :req2, :state}
|
||||||
|
|
||||||
assert validate :cowboy_req
|
assert validate :cowboy_req
|
||||||
assert validate JSEX
|
assert validate JSX
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,7 +5,7 @@ defmodule HTTParrot.DelayedHandlerTest do
|
||||||
|
|
||||||
setup do
|
setup do
|
||||||
new HTTParrot.GeneralRequestInfo
|
new HTTParrot.GeneralRequestInfo
|
||||||
new JSEX
|
new JSX
|
||||||
new :cowboy_req
|
new :cowboy_req
|
||||||
on_exit fn -> unload end
|
on_exit fn -> unload end
|
||||||
:ok
|
:ok
|
||||||
|
@ -45,11 +45,11 @@ defmodule HTTParrot.DelayedHandlerTest do
|
||||||
|
|
||||||
test "returns json with query values, headers, url and origin" do
|
test "returns json with query values, headers, url and origin" do
|
||||||
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {:info, :req2})
|
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {:info, :req2})
|
||||||
expect(JSEX, :encode!, [{[:info], :json}])
|
expect(JSX, :encode!, [{[:info], :json}])
|
||||||
|
|
||||||
assert get_json(:req1, 0) == {:json, :req2, 0}
|
assert get_json(:req1, 0) == {:json, :req2, 0}
|
||||||
|
|
||||||
assert validate HTTParrot.GeneralRequestInfo
|
assert validate HTTParrot.GeneralRequestInfo
|
||||||
assert validate JSEX
|
assert validate JSX
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,20 +6,20 @@ defmodule HTTParrot.DeleteHandlerTest do
|
||||||
setup do
|
setup do
|
||||||
new :cowboy_req
|
new :cowboy_req
|
||||||
new HTTParrot.GeneralRequestInfo
|
new HTTParrot.GeneralRequestInfo
|
||||||
new JSEX
|
new JSX
|
||||||
on_exit fn -> unload end
|
on_exit fn -> unload end
|
||||||
:ok
|
:ok
|
||||||
end
|
end
|
||||||
|
|
||||||
test "returns prettified json with query values, headers, url and origin" do
|
test "returns prettified json with query values, headers, url and origin" do
|
||||||
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {:info, :req2})
|
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {:info, :req2})
|
||||||
expect(JSEX, :encode!, [{[:info], :json}])
|
expect(JSX, :encode!, [{[:info], :json}])
|
||||||
expect(:cowboy_req, :set_resp_body, [{[:json, :req2], :req3}])
|
expect(:cowboy_req, :set_resp_body, [{[:json, :req2], :req3}])
|
||||||
|
|
||||||
assert delete_resource(:req1, :state) == {true, :req3, :state}
|
assert delete_resource(:req1, :state) == {true, :req3, :state}
|
||||||
|
|
||||||
assert validate :cowboy_req
|
assert validate :cowboy_req
|
||||||
assert validate HTTParrot.GeneralRequestInfo
|
assert validate HTTParrot.GeneralRequestInfo
|
||||||
assert validate JSEX
|
assert validate JSX
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,18 +5,18 @@ defmodule HTTParrot.GetHandlerTest do
|
||||||
|
|
||||||
setup do
|
setup do
|
||||||
new HTTParrot.GeneralRequestInfo
|
new HTTParrot.GeneralRequestInfo
|
||||||
new JSEX
|
new JSX
|
||||||
on_exit fn -> unload end
|
on_exit fn -> unload end
|
||||||
:ok
|
:ok
|
||||||
end
|
end
|
||||||
|
|
||||||
test "returns prettified json with query values, headers, url and origin" do
|
test "returns prettified json with query values, headers, url and origin" do
|
||||||
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {:info, :req2})
|
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {:info, :req2})
|
||||||
expect(JSEX, :encode!, [{[:info], :json}])
|
expect(JSX, :encode!, [{[:info], :json}])
|
||||||
|
|
||||||
assert get_json(:req1, :state) == {:json, :req2, :state}
|
assert get_json(:req1, :state) == {:json, :req2, :state}
|
||||||
|
|
||||||
assert validate HTTParrot.GeneralRequestInfo
|
assert validate HTTParrot.GeneralRequestInfo
|
||||||
assert validate JSEX
|
assert validate JSX
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,15 +5,15 @@ defmodule HTTParrot.GzipHandlerTest do
|
||||||
|
|
||||||
setup do
|
setup do
|
||||||
new HTTParrot.GeneralRequestInfo
|
new HTTParrot.GeneralRequestInfo
|
||||||
new JSEX
|
new JSX
|
||||||
on_exit fn -> unload end
|
on_exit fn -> unload end
|
||||||
:ok
|
:ok
|
||||||
end
|
end
|
||||||
|
|
||||||
test "returns prettified json with query values, headers, url and origin" do
|
test "returns prettified json with query values, headers, url and origin" do
|
||||||
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {:info, :req2})
|
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {:info, :req2})
|
||||||
expect(JSEX, :encode!, [{[:info], :json}])
|
expect(JSX, :encode!, [{[:info], :json}])
|
||||||
expect(JSEX, :prettify!, [{[:json], "json"}])
|
expect(JSX, :prettify!, [{[:json], "json"}])
|
||||||
expect(:cowboy_req, :set_resp_header, 3, :req3)
|
expect(:cowboy_req, :set_resp_header, 3, :req3)
|
||||||
|
|
||||||
body = :zlib.gzip("json")
|
body = :zlib.gzip("json")
|
||||||
|
@ -21,7 +21,7 @@ defmodule HTTParrot.GzipHandlerTest do
|
||||||
assert get_json(:req1, :state) == {body, :req3, :state}
|
assert get_json(:req1, :state) == {body, :req3, :state}
|
||||||
|
|
||||||
assert validate HTTParrot.GeneralRequestInfo
|
assert validate HTTParrot.GeneralRequestInfo
|
||||||
assert validate JSEX
|
assert validate JSX
|
||||||
assert validate :cowboy_req
|
assert validate :cowboy_req
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,18 +5,18 @@ defmodule HTTParrot.HeadersHandlerTest do
|
||||||
|
|
||||||
setup do
|
setup do
|
||||||
new :cowboy_req
|
new :cowboy_req
|
||||||
new JSEX
|
new JSX
|
||||||
on_exit fn -> unload end
|
on_exit fn -> unload end
|
||||||
:ok
|
:ok
|
||||||
end
|
end
|
||||||
|
|
||||||
test "returns prettified json with headers list" do
|
test "returns prettified json with headers list" do
|
||||||
expect(:cowboy_req, :headers, 1, {:headers, :req2})
|
expect(:cowboy_req, :headers, 1, {:headers, :req2})
|
||||||
expect(JSEX, :encode!, [{[[headers: :headers]], :json}])
|
expect(JSX, :encode!, [{[[headers: :headers]], :json}])
|
||||||
|
|
||||||
assert get_json(:req1, :state) == {:json, :req2, :state}
|
assert get_json(:req1, :state) == {:json, :req2, :state}
|
||||||
|
|
||||||
assert validate :cowboy_req
|
assert validate :cowboy_req
|
||||||
assert validate JSEX
|
assert validate JSX
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,7 +5,7 @@ defmodule HTTParrot.HiddenBasicAuthHandlerTest do
|
||||||
|
|
||||||
setup do
|
setup do
|
||||||
new :cowboy_req
|
new :cowboy_req
|
||||||
new JSEX
|
new JSX
|
||||||
on_exit fn -> unload end
|
on_exit fn -> unload end
|
||||||
:ok
|
:ok
|
||||||
end
|
end
|
||||||
|
@ -18,7 +18,7 @@ defmodule HTTParrot.HiddenBasicAuthHandlerTest do
|
||||||
assert resource_exists(:req1, :state) == {true, :req4, :user}
|
assert resource_exists(:req1, :state) == {true, :req4, :user}
|
||||||
|
|
||||||
assert validate :cowboy_req
|
assert validate :cowboy_req
|
||||||
assert validate JSEX
|
assert validate JSX
|
||||||
end
|
end
|
||||||
|
|
||||||
test "resource_exists returns false if user and passwd doesnt match" do
|
test "resource_exists returns false if user and passwd doesnt match" do
|
||||||
|
@ -29,14 +29,14 @@ defmodule HTTParrot.HiddenBasicAuthHandlerTest do
|
||||||
assert resource_exists(:req1, :state) == {false, :req4, :state}
|
assert resource_exists(:req1, :state) == {false, :req4, :state}
|
||||||
|
|
||||||
assert validate :cowboy_req
|
assert validate :cowboy_req
|
||||||
assert validate JSEX
|
assert validate JSX
|
||||||
end
|
end
|
||||||
|
|
||||||
test "returns user and if it's authenticated" do
|
test "returns user and if it's authenticated" do
|
||||||
expect(JSEX, :encode!, [{[[authenticated: true, user: :user]], :json}])
|
expect(JSX, :encode!, [{[[authenticated: true, user: :user]], :json}])
|
||||||
|
|
||||||
assert get_json(:req1, :user) == {:json, :req1, nil}
|
assert get_json(:req1, :user) == {:json, :req1, nil}
|
||||||
|
|
||||||
assert validate JSEX
|
assert validate JSX
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,7 +5,7 @@ defmodule HTTParrot.IPHandlerTest do
|
||||||
|
|
||||||
setup do
|
setup do
|
||||||
new :cowboy_req
|
new :cowboy_req
|
||||||
new JSEX
|
new JSX
|
||||||
on_exit fn -> unload end
|
on_exit fn -> unload end
|
||||||
:ok
|
:ok
|
||||||
end
|
end
|
||||||
|
@ -13,11 +13,11 @@ defmodule HTTParrot.IPHandlerTest do
|
||||||
test "returns prettified json with origin" do
|
test "returns prettified json with origin" do
|
||||||
ip = {127, 1, 2, 3}
|
ip = {127, 1, 2, 3}
|
||||||
expect(:cowboy_req, :peer, 1, {{ip, :host}, :req2})
|
expect(:cowboy_req, :peer, 1, {{ip, :host}, :req2})
|
||||||
expect(JSEX, :encode!, [{[[origin: "127.1.2.3"]], :json}])
|
expect(JSX, :encode!, [{[[origin: "127.1.2.3"]], :json}])
|
||||||
|
|
||||||
assert get_json(:req1, :state) == {:json, :req2, :state}
|
assert get_json(:req1, :state) == {:json, :req2, :state}
|
||||||
|
|
||||||
assert validate :cowboy_req
|
assert validate :cowboy_req
|
||||||
assert validate JSEX
|
assert validate JSX
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,7 +5,7 @@ defmodule HTTParrot.PHandlerTest do
|
||||||
|
|
||||||
setup do
|
setup do
|
||||||
new HTTParrot.GeneralRequestInfo
|
new HTTParrot.GeneralRequestInfo
|
||||||
new JSEX
|
new JSX
|
||||||
new :cowboy_req
|
new :cowboy_req
|
||||||
on_exit fn -> unload end
|
on_exit fn -> unload end
|
||||||
:ok
|
:ok
|
||||||
|
@ -27,7 +27,7 @@ defmodule HTTParrot.PHandlerTest do
|
||||||
expect(:cowboy_req, :body_qs, 1, {:ok, :body_qs, :req2})
|
expect(:cowboy_req, :body_qs, 1, {:ok, :body_qs, :req2})
|
||||||
expect(:cowboy_req, :set_resp_body, [{[:response, :req3], :req4}])
|
expect(:cowboy_req, :set_resp_body, [{[:response, :req3], :req4}])
|
||||||
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {[:info], :req3})
|
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {[:info], :req3})
|
||||||
expect(JSEX, :encode!, [{[[:info, {:form, :body_qs}, {:data, ""}, {:json, nil}]], :response}])
|
expect(JSX, :encode!, [{[[:info, {:form, :body_qs}, {:data, ""}, {:json, nil}]], :response}])
|
||||||
|
|
||||||
assert post_form(:req1, :state) == {true, :req4, nil}
|
assert post_form(:req1, :state) == {true, :req4, nil}
|
||||||
|
|
||||||
|
@ -38,9 +38,9 @@ defmodule HTTParrot.PHandlerTest do
|
||||||
expect(:cowboy_req, :body, 1, {:ok, "body", :req2})
|
expect(:cowboy_req, :body, 1, {:ok, "body", :req2})
|
||||||
expect(:cowboy_req, :set_resp_body, [{[:response, :req3], :req4}])
|
expect(:cowboy_req, :set_resp_body, [{[:response, :req3], :req4}])
|
||||||
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {[:info], :req3})
|
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {[:info], :req3})
|
||||||
expect(JSEX, :is_json?, 1, true)
|
expect(JSX, :is_json?, 1, true)
|
||||||
expect(JSEX, :decode!, 1, :decoded_json)
|
expect(JSX, :decode!, 1, :decoded_json)
|
||||||
expect(JSEX, :encode!, [{[[:info, {:form, [{}]}, {:data, "body"}, {:json, :decoded_json}]], :response}])
|
expect(JSX, :encode!, [{[[:info, {:form, [{}]}, {:data, "body"}, {:json, :decoded_json}]], :response}])
|
||||||
|
|
||||||
assert post_binary(:req1, :state) == {true, :req4, nil}
|
assert post_binary(:req1, :state) == {true, :req4, nil}
|
||||||
|
|
||||||
|
@ -51,8 +51,8 @@ defmodule HTTParrot.PHandlerTest do
|
||||||
expect(:cowboy_req, :body, 1, {:ok, "body", :req2})
|
expect(:cowboy_req, :body, 1, {:ok, "body", :req2})
|
||||||
expect(:cowboy_req, :set_resp_body, [{[:response, :req3], :req4}])
|
expect(:cowboy_req, :set_resp_body, [{[:response, :req3], :req4}])
|
||||||
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {[:info], :req3})
|
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {[:info], :req3})
|
||||||
expect(JSEX, :is_json?, 1, false)
|
expect(JSX, :is_json?, 1, false)
|
||||||
expect(JSEX, :encode!, [{[[:info, {:form, [{}]}, {:data, "body"}, {:json, nil}]], :response}])
|
expect(JSX, :encode!, [{[[:info, {:form, [{}]}, {:data, "body"}, {:json, nil}]], :response}])
|
||||||
|
|
||||||
assert post_binary(:req1, :state) == {true, :req4, nil}
|
assert post_binary(:req1, :state) == {true, :req4, nil}
|
||||||
|
|
||||||
|
@ -63,8 +63,8 @@ defmodule HTTParrot.PHandlerTest do
|
||||||
expect(:cowboy_req, :body, 1, {:ok, <<0xffff :: 16>>, :req2})
|
expect(:cowboy_req, :body, 1, {:ok, <<0xffff :: 16>>, :req2})
|
||||||
expect(:cowboy_req, :set_resp_body, [{[:response, :req3], :req4}])
|
expect(:cowboy_req, :set_resp_body, [{[:response, :req3], :req4}])
|
||||||
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {[:info], :req3})
|
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {[:info], :req3})
|
||||||
expect(JSEX, :is_json?, 1, false)
|
expect(JSX, :is_json?, 1, false)
|
||||||
expect(JSEX, :encode!, [{[[:info, {:form, [{}]}, {:data, "data:application/octet-stream;base64,//8="}, {:json, nil}]], :response}])
|
expect(JSX, :encode!, [{[[:info, {:form, [{}]}, {:data, "data:application/octet-stream;base64,//8="}, {:json, nil}]], :response}])
|
||||||
|
|
||||||
assert post_binary(:req1, :state) == {true, :req4, nil}
|
assert post_binary(:req1, :state) == {true, :req4, nil}
|
||||||
|
|
||||||
|
@ -85,8 +85,8 @@ defmodule HTTParrot.PHandlerTest do
|
||||||
|
|
||||||
expect(:cowboy_req, :set_resp_body, [{[:response, :req4], :req5}])
|
expect(:cowboy_req, :set_resp_body, [{[:response, :req4], :req5}])
|
||||||
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {[:info], :req4})
|
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {[:info], :req4})
|
||||||
expect(JSEX, :is_json?, 1, false)
|
expect(JSX, :is_json?, 1, false)
|
||||||
expect(JSEX, :encode!, [{[[:info, {:form, [{}]}, {:data, first_chunk <> second_chunk}, {:json, nil}]], :response}])
|
expect(JSX, :encode!, [{[[:info, {:form, [{}]}, {:data, first_chunk <> second_chunk}, {:json, nil}]], :response}])
|
||||||
|
|
||||||
assert post_binary(:req1, :state) == {true, :req5, nil}
|
assert post_binary(:req1, :state) == {true, :req5, nil}
|
||||||
|
|
||||||
|
@ -107,8 +107,8 @@ defmodule HTTParrot.PHandlerTest do
|
||||||
|
|
||||||
expect(:cowboy_req, :set_resp_body, [{[:response, :req4], :req5}])
|
expect(:cowboy_req, :set_resp_body, [{[:response, :req4], :req5}])
|
||||||
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {[:info], :req4})
|
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {[:info], :req4})
|
||||||
expect(JSEX, :is_json?, 1, false)
|
expect(JSX, :is_json?, 1, false)
|
||||||
expect(JSEX, :encode!, [{[[:info, {:form, [{}]}, {:data, "data:application/octet-stream;base64,#{Base.encode64(first_chunk <> second_chunk)}"}, {:json, nil}]], :response}])
|
expect(JSX, :encode!, [{[[:info, {:form, [{}]}, {:data, "data:application/octet-stream;base64,#{Base.encode64(first_chunk <> second_chunk)}"}, {:json, nil}]], :response}])
|
||||||
|
|
||||||
assert post_binary(:req1, :state) == {true, :req5, nil}
|
assert post_binary(:req1, :state) == {true, :req5, nil}
|
||||||
|
|
||||||
|
@ -131,8 +131,8 @@ defmodule HTTParrot.PHandlerTest do
|
||||||
{:ok, {"multipart", "form-data", [{"boundary", "----WebKitFormBoundary8BEQxJvZANFsvRV9"}]}, :req5}}])
|
{:ok, {"multipart", "form-data", [{"boundary", "----WebKitFormBoundary8BEQxJvZANFsvRV9"}]}, :req5}}])
|
||||||
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {[:info], :req6})
|
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {[:info], :req6})
|
||||||
|
|
||||||
expect(JSEX, :is_json?, 1, false)
|
expect(JSX, :is_json?, 1, false)
|
||||||
expect(JSEX, :encode!, [{[[:info, {:form, [{"key1", "value1"}]}, {:files, [{}]}, {:data, ""}, {:json, nil}]], :response}])
|
expect(JSX, :encode!, [{[[:info, {:form, [{"key1", "value1"}]}, {:files, [{}]}, {:data, ""}, {:json, nil}]], :response}])
|
||||||
|
|
||||||
expect(:cowboy_req, :set_resp_body, [{[:response, :req6], :req7}])
|
expect(:cowboy_req, :set_resp_body, [{[:response, :req6], :req7}])
|
||||||
|
|
||||||
|
@ -164,8 +164,8 @@ defmodule HTTParrot.PHandlerTest do
|
||||||
{:ok, {"multipart", "form-data", [{"boundary", "----WebKitFormBoundary8BEQxJvZANFsvRV9"}]}, :req7}}])
|
{:ok, {"multipart", "form-data", [{"boundary", "----WebKitFormBoundary8BEQxJvZANFsvRV9"}]}, :req7}}])
|
||||||
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {[:info], :req8})
|
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {[:info], :req8})
|
||||||
|
|
||||||
expect(JSEX, :is_json?, 1, false)
|
expect(JSX, :is_json?, 1, false)
|
||||||
expect(JSEX, :encode!, [{[[:info, {:form, [{"key1", "value1"}, {"key2", "value2"}]}, {:files, [{}]}, {:data, ""}, {:json, nil}]], :response}])
|
expect(JSX, :encode!, [{[[:info, {:form, [{"key1", "value1"}, {"key2", "value2"}]}, {:files, [{}]}, {:data, ""}, {:json, nil}]], :response}])
|
||||||
|
|
||||||
expect(:cowboy_req, :set_resp_body, [{[:response, :req8], :req9}])
|
expect(:cowboy_req, :set_resp_body, [{[:response, :req8], :req9}])
|
||||||
|
|
||||||
|
@ -189,8 +189,8 @@ defmodule HTTParrot.PHandlerTest do
|
||||||
{:ok, {"multipart", "form-data", [{"boundary", "----WebKitFormBoundary8BEQxJvZANFsvRV9"}]}, :req5}}])
|
{:ok, {"multipart", "form-data", [{"boundary", "----WebKitFormBoundary8BEQxJvZANFsvRV9"}]}, :req5}}])
|
||||||
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {[:info], :req6})
|
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {[:info], :req6})
|
||||||
|
|
||||||
expect(JSEX, :is_json?, 1, false)
|
expect(JSX, :is_json?, 1, false)
|
||||||
expect(JSEX, :encode!, [{[[:info, {:form, [{}]}, {:files, [{"file1", "here is some cool\ntest data."}]}, {:data, ""}, {:json, nil}]], :response}])
|
expect(JSX, :encode!, [{[[:info, {:form, [{}]}, {:files, [{"file1", "here is some cool\ntest data."}]}, {:data, ""}, {:json, nil}]], :response}])
|
||||||
|
|
||||||
expect(:cowboy_req, :set_resp_body, [{[:response, :req6], :req7}])
|
expect(:cowboy_req, :set_resp_body, [{[:response, :req6], :req7}])
|
||||||
|
|
||||||
|
@ -222,8 +222,8 @@ defmodule HTTParrot.PHandlerTest do
|
||||||
{:ok, {"multipart", "form-data", [{"boundary", "----WebKitFormBoundary8BEQxJvZANFsvRV9"}]}, :req7}}])
|
{:ok, {"multipart", "form-data", [{"boundary", "----WebKitFormBoundary8BEQxJvZANFsvRV9"}]}, :req7}}])
|
||||||
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {[:info], :req8})
|
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {[:info], :req8})
|
||||||
|
|
||||||
expect(JSEX, :is_json?, 1, false)
|
expect(JSX, :is_json?, 1, false)
|
||||||
expect(JSEX, :encode!, [{[[:info, {:form, [{"key1", "value1"}]}, {:files, [{"file1", "here is some cool\ntest data"}]}, {:data, ""}, {:json, nil}]], :response}])
|
expect(JSX, :encode!, [{[[:info, {:form, [{"key1", "value1"}]}, {:files, [{"file1", "here is some cool\ntest data"}]}, {:data, ""}, {:json, nil}]], :response}])
|
||||||
|
|
||||||
expect(:cowboy_req, :set_resp_body, [{[:response, :req8], :req9}])
|
expect(:cowboy_req, :set_resp_body, [{[:response, :req8], :req9}])
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@ defmodule HTTParrot.StreamHandlerTest do
|
||||||
setup do
|
setup do
|
||||||
new :cowboy_req
|
new :cowboy_req
|
||||||
new HTTParrot.GeneralRequestInfo
|
new HTTParrot.GeneralRequestInfo
|
||||||
new JSEX
|
new JSX
|
||||||
on_exit fn -> unload end
|
on_exit fn -> unload end
|
||||||
:ok
|
:ok
|
||||||
end
|
end
|
||||||
|
@ -45,7 +45,7 @@ defmodule HTTParrot.StreamHandlerTest do
|
||||||
|
|
||||||
test "response must stream chunks" do
|
test "response must stream chunks" do
|
||||||
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {[:info], :req2})
|
expect(HTTParrot.GeneralRequestInfo, :retrieve, 1, {[:info], :req2})
|
||||||
expect(JSEX, :encode!, [{[[{:id, 0}, :info]], :json1},
|
expect(JSX, :encode!, [{[[{:id, 0}, :info]], :json1},
|
||||||
{[[{:id, 1}, :info]], :json2}])
|
{[[{:id, 1}, :info]], :json2}])
|
||||||
|
|
||||||
assert {{:chunked, func}, :req2, nil} = get_json(:req1, 2)
|
assert {{:chunked, func}, :req2, nil} = get_json(:req1, 2)
|
||||||
|
@ -58,6 +58,6 @@ defmodule HTTParrot.StreamHandlerTest do
|
||||||
assert_receive {:chunk, :json2}
|
assert_receive {:chunk, :json2}
|
||||||
|
|
||||||
assert validate HTTParrot.GeneralRequestInfo
|
assert validate HTTParrot.GeneralRequestInfo
|
||||||
assert validate JSEX
|
assert validate JSX
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,18 +5,18 @@ defmodule HTTParrot.UserAgentHandlerTest do
|
||||||
|
|
||||||
setup do
|
setup do
|
||||||
new :cowboy_req
|
new :cowboy_req
|
||||||
new JSEX
|
new JSX
|
||||||
on_exit fn -> unload end
|
on_exit fn -> unload end
|
||||||
:ok
|
:ok
|
||||||
end
|
end
|
||||||
|
|
||||||
test "returns prettified json with user agent" do
|
test "returns prettified json with user agent" do
|
||||||
expect(:cowboy_req, :header, [{["user-agent", :req1, "null"], {:user_agent, :req2}}])
|
expect(:cowboy_req, :header, [{["user-agent", :req1, "null"], {:user_agent, :req2}}])
|
||||||
expect(JSEX, :encode!, [{[[{"user-agent", :user_agent}]], :json}])
|
expect(JSX, :encode!, [{[[{"user-agent", :user_agent}]], :json}])
|
||||||
|
|
||||||
assert get_json(:req1, :state) == {:json, :req2, :state}
|
assert get_json(:req1, :state) == {:json, :req2, :state}
|
||||||
|
|
||||||
assert validate :cowboy_req
|
assert validate :cowboy_req
|
||||||
assert validate JSEX
|
assert validate JSX
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue