Skip to content

Commit

Permalink
Fixes process_request_options(options) for adding more params via bas…
Browse files Browse the repository at this point in the history
…e module
  • Loading branch information
Romain Haenni committed Feb 6, 2017
1 parent 93f48d6 commit 971bd58
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/httpoison/base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ defmodule HTTPoison.Base do
@spec request(atom, binary, body, headers, Keyword.t) :: {:ok, Response.t | AsyncResponse.t}
| {:error, Error.t}
def request(method, url, body \\ "", headers \\ [], options \\ []) do
options = process_request_options(options)
url =
if Keyword.has_key?(options, :params) do
url <> "?" <> URI.encode_query(options[:params])
Expand All @@ -167,7 +168,6 @@ defmodule HTTPoison.Base do
url = process_url(to_string(url))
body = process_request_body(body)
headers = process_request_headers(headers)
options = process_request_options(options)
HTTPoison.Base.request(__MODULE__, method, url, body, headers, options, &process_status_code/1, &process_headers/1, &process_response_body/1)
end

Expand Down
18 changes: 18 additions & 0 deletions test/httpoison_base_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ defmodule HTTPoisonBaseTest do
defp process_status_code(code), do: {:code, code}
end

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"})])
end

setup do
new :hackney
on_exit fn -> unload() end
Expand Down Expand Up @@ -56,6 +62,18 @@ defmodule HTTPoisonBaseTest do
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, :body, 1, {:ok, "response"})

assert ExampleParamsOptions.get!("localhost", [], params: %{foo: "bar"}) ==
%HTTPoison.Response{ status_code: 200,
headers: "headers",
body: "response" }

assert validate :hackney
end

test "request raises error tuple" do
reason = {:closed, "Something happened"}
expect(:hackney, :request, 5, {:error, reason})
Expand Down

0 comments on commit 971bd58

Please sign in to comment.