Skip to content

Commit

Permalink
Merge pull request #332 from edgurgel/mix-format
Browse files Browse the repository at this point in the history
mix format
  • Loading branch information
edgurgel authored Jun 2, 2018
2 parents 0ad9681 + c2ae74a commit 22fc895
Show file tree
Hide file tree
Showing 8 changed files with 464 additions and 231 deletions.
4 changes: 4 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["mix.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ elixir:
otp_release:
- 19.3
- 20.2
script:
- mix test
- if [[ `elixir -v` = *"1.6"* ]]; then mix format --check-formatted; fi
4 changes: 2 additions & 2 deletions lib/httpoison.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ end

defmodule HTTPoison.AsyncRedirect do
defstruct id: nil, to: nil, headers: []
@type t :: %__MODULE__{id: reference, to: String.t, headers: list}
@type t :: %__MODULE__{id: reference, to: String.t(), headers: list}
end

defmodule HTTPoison.AsyncEnd do
Expand All @@ -38,7 +38,7 @@ defmodule HTTPoison.Error do
@type t :: %__MODULE__{id: reference | nil, reason: any}

def message(%__MODULE__{reason: reason, id: nil}), do: inspect(reason)
def message(%__MODULE__{reason: reason, id: id}), do: "[Reference: #{id}] - #{inspect reason}"
def message(%__MODULE__{reason: reason, id: id}), do: "[Reference: #{id}] - #{inspect(reason)}"
end

defmodule HTTPoison do
Expand Down
270 changes: 183 additions & 87 deletions lib/httpoison/base.ex

Large diffs are not rendered by default.

14 changes: 9 additions & 5 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ defmodule HTTPoison.Mixfile do
"""

def project do
[ app: :httpoison,
[
app: :httpoison,
version: "1.2.0",
elixir: "~> 1.5",
name: "HTTPoison",
description: @description,
package: package(),
deps: deps(),
source_url: "https://github.com/edgurgel/httpoison" ]
source_url: "https://github.com/edgurgel/httpoison"
]
end

def application do
Expand All @@ -27,13 +29,15 @@ defmodule HTTPoison.Mixfile do
{:httparrot, "~> 1.0", only: :test},
{:meck, "~> 0.8.2", only: :test},
{:earmark, "~> 1.0", only: :dev},
{:ex_doc, "~> 0.14", only: :dev},
{:ex_doc, "~> 0.14", only: :dev}
]
end

defp package do
[ maintainers: ["Eduardo Gurgel Pinho"],
[
maintainers: ["Eduardo Gurgel Pinho"],
licenses: ["MIT"],
links: %{"Github" => "https://github.com/edgurgel/httpoison"} ]
links: %{"Github" => "https://github.com/edgurgel/httpoison"}
]
end
end
237 changes: 157 additions & 80 deletions test/httpoison_base_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,57 @@ defmodule HTTPoisonBaseTest do
defmodule ExampleParamsOptions do
use HTTPoison.Base
def process_url(url), do: "http://" <> url
def process_request_options(options), do: Keyword.merge(options, [params: Map.merge(options[:params], %{key: "fizz"})])

def process_request_options(options),
do: Keyword.merge(options, params: Map.merge(options[:params], %{key: "fizz"}))
end

setup do
new :hackney
on_exit fn -> unload() end
new(:hackney)
on_exit(fn -> unload() end)
:ok
end

test "request body using Example" do
expect(:hackney, :request, [{[:post, "http://localhost", {:req_headers, []}, {:req_body, "body"}, [{:connect_timeout, 10}]],
{:ok, 200, "headers", :client}}])
expect(:hackney, :request, [
{[
:post,
"http://localhost",
{:req_headers, []},
{:req_body, "body"},
[{:connect_timeout, 10}]
], {:ok, 200, "headers", :client}}
])

expect(:hackney, :body, 1, {:ok, "response"})

assert Example.post!("localhost", "body") ==
%HTTPoison.Response{ status_code: {:code, 200},
headers: {:headers, "headers"},
body: {:resp_body, "response"},
request_url: "http://localhost" }
%HTTPoison.Response{
status_code: {:code, 200},
headers: {:headers, "headers"},
body: {:resp_body, "response"},
request_url: "http://localhost"
}

assert validate :hackney
assert validate(:hackney)
end

test "request body using params example" do
expect(:hackney, :request, [{[:get, "http://localhost?foo=bar&key=fizz", [], "", []], {:ok, 200, "headers", :client}}])
expect(:hackney, :request, [
{[:get, "http://localhost?foo=bar&key=fizz", [], "", []], {:ok, 200, "headers", :client}}
])

expect(:hackney, :body, 1, {:ok, "response"})

assert ExampleParamsOptions.get!("localhost", [], params: %{foo: "bar"}) ==
%HTTPoison.Response{ status_code: 200,
headers: "headers",
body: "response",
request_url: "http://localhost?foo=bar&key=fizz" }
%HTTPoison.Response{
status_code: 200,
headers: "headers",
body: "response",
request_url: "http://localhost?foo=bar&key=fizz"
}

assert validate :hackney
assert validate(:hackney)
end

test "request raises error tuple" do
Expand All @@ -62,82 +79,128 @@ defmodule HTTPoisonBaseTest do

assert HTTPoison.get("http://localhost") == {:error, %HTTPoison.Error{reason: reason}}

assert validate :hackney
assert validate(:hackney)
end

test "passing connect_timeout option" do
expect(:hackney, :request, [{[:post, "http://localhost", [], "body", [connect_timeout: 12345]],
{:ok, 200, "headers", :client}}])
expect(:hackney, :request, [
{[:post, "http://localhost", [], "body", [connect_timeout: 12345]],
{:ok, 200, "headers", :client}}
])

expect(:hackney, :body, 1, {:ok, "response"})

assert HTTPoison.post!("localhost", "body", [], timeout: 12345) ==
%HTTPoison.Response{ status_code: 200,
headers: "headers",
body: "response",
request_url: "http://localhost" }
%HTTPoison.Response{
status_code: 200,
headers: "headers",
body: "response",
request_url: "http://localhost"
}

assert validate :hackney
assert validate(:hackney)
end

test "passing recv_timeout option" do
expect(:hackney, :request, [{[:post, "http://localhost", [], "body", [recv_timeout: 12345]],
{:ok, 200, "headers", :client}}])
expect(:hackney, :request, [
{[:post, "http://localhost", [], "body", [recv_timeout: 12345]],
{:ok, 200, "headers", :client}}
])

expect(:hackney, :body, 1, {:ok, "response"})

assert HTTPoison.post!("localhost", "body", [], recv_timeout: 12345) ==
%HTTPoison.Response{ status_code: 200,
headers: "headers",
body: "response",
request_url: "http://localhost" }
%HTTPoison.Response{
status_code: 200,
headers: "headers",
body: "response",
request_url: "http://localhost"
}

assert validate :hackney
assert validate(:hackney)
end

test "passing proxy option" do
expect(:hackney, :request, [{[:post, "http://localhost", [], "body", [proxy: "proxy"]],
{:ok, 200, "headers", :client}}])
expect(:hackney, :request, [
{[:post, "http://localhost", [], "body", [proxy: "proxy"]], {:ok, 200, "headers", :client}}
])

expect(:hackney, :body, 1, {:ok, "response"})

assert HTTPoison.post!("localhost", "body", [], proxy: "proxy") ==
%HTTPoison.Response{ status_code: 200,
headers: "headers",
body: "response",
request_url: "http://localhost" }
%HTTPoison.Response{
status_code: 200,
headers: "headers",
body: "response",
request_url: "http://localhost"
}

assert validate :hackney
assert validate(:hackney)
end

test "passing socks5 options" do
expect(:hackney, :request, [{
[:post, "http://localhost", [], "body", [
socks5_pass: "secret",
socks5_user: "user",
proxy: {:socks5, 'localhost', 1080}
]],
{:ok, 200, "headers", :client}}])
expect(:hackney, :request, [
{[
:post,
"http://localhost",
[],
"body",
[
socks5_pass: "secret",
socks5_user: "user",
proxy: {:socks5, 'localhost', 1080}
]
], {:ok, 200, "headers", :client}}
])

expect(:hackney, :body, 1, {:ok, "response"})

assert HTTPoison.post!("localhost", "body", [], proxy: {:socks5, 'localhost', 1080}, socks5_user: "user", socks5_pass: "secret") ==
%HTTPoison.Response{ status_code: 200,
headers: "headers",
body: "response",
request_url: "http://localhost" }
assert HTTPoison.post!(
"localhost",
"body",
[],
proxy: {:socks5, 'localhost', 1080},
socks5_user: "user",
socks5_pass: "secret"
) ==
%HTTPoison.Response{
status_code: 200,
headers: "headers",
body: "response",
request_url: "http://localhost"
}

assert validate :hackney
assert validate(:hackney)
end

test "passing proxy option with proxy_auth" do
expect(:hackney, :request, [{[:post, "http://localhost", [], "body", [proxy_auth: {"username", "password"}, proxy: "proxy"]],
{:ok, 200, "headers", :client}}])
expect(:hackney, :request, [
{[
:post,
"http://localhost",
[],
"body",
[proxy_auth: {"username", "password"}, proxy: "proxy"]
], {:ok, 200, "headers", :client}}
])

expect(:hackney, :body, 1, {:ok, "response"})

assert HTTPoison.post!("localhost", "body", [], [proxy: "proxy", proxy_auth: {"username", "password"}]) ==
%HTTPoison.Response{ status_code: 200,
headers: "headers",
body: "response",
request_url: "http://localhost" }
assert HTTPoison.post!(
"localhost",
"body",
[],
proxy: "proxy",
proxy_auth: {"username", "password"}
) ==
%HTTPoison.Response{
status_code: 200,
headers: "headers",
body: "response",
request_url: "http://localhost"
}

assert validate :hackney
assert validate(:hackney)
end

test "having http_proxy env variable set on http requests" do
Expand Down Expand Up @@ -205,44 +268,58 @@ defmodule HTTPoisonBaseTest do
end

test "passing ssl option" do
expect(:hackney, :request, [{[:post, "http://localhost", [], "body", [ssl_options: [certfile: "certs/client.crt"]]],
{:ok, 200, "headers", :client}}])
expect(:hackney, :request, [
{[:post, "http://localhost", [], "body", [ssl_options: [certfile: "certs/client.crt"]]],
{:ok, 200, "headers", :client}}
])

expect(:hackney, :body, 1, {:ok, "response"})

assert HTTPoison.post!("localhost", "body", [], ssl: [certfile: "certs/client.crt"]) ==
%HTTPoison.Response{ status_code: 200,
headers: "headers",
body: "response",
request_url: "http://localhost" }
%HTTPoison.Response{
status_code: 200,
headers: "headers",
body: "response",
request_url: "http://localhost"
}

assert validate :hackney
assert validate(:hackney)
end

test "passing follow_redirect option" do
expect(:hackney, :request, [{[:post, "http://localhost", [], "body", [follow_redirect: true]],
{:ok, 200, "headers", :client}}])
expect(:hackney, :request, [
{[:post, "http://localhost", [], "body", [follow_redirect: true]],
{:ok, 200, "headers", :client}}
])

expect(:hackney, :body, 1, {:ok, "response"})

assert HTTPoison.post!("localhost", "body", [], follow_redirect: true) ==
%HTTPoison.Response{ status_code: 200,
headers: "headers",
body: "response",
request_url: "http://localhost" }
%HTTPoison.Response{
status_code: 200,
headers: "headers",
body: "response",
request_url: "http://localhost"
}

assert validate :hackney
assert validate(:hackney)
end

test "passing max_redirect option" do
expect(:hackney, :request, [{[:post, "http://localhost", [], "body", [max_redirect: 2]],
{:ok, 200, "headers", :client}}])
expect(:hackney, :request, [
{[:post, "http://localhost", [], "body", [max_redirect: 2]], {:ok, 200, "headers", :client}}
])

expect(:hackney, :body, 1, {:ok, "response"})

assert HTTPoison.post!("localhost", "body", [], max_redirect: 2) ==
%HTTPoison.Response{ status_code: 200,
headers: "headers",
body: "response",
request_url: "http://localhost" }
%HTTPoison.Response{
status_code: 200,
headers: "headers",
body: "response",
request_url: "http://localhost"
}

assert validate :hackney
assert validate(:hackney)
end
end
Loading

0 comments on commit 22fc895

Please sign in to comment.