Skip to content

Commit

Permalink
Add process_request_options/1 to HTTPoison.Base
Browse files Browse the repository at this point in the history
  • Loading branch information
drewolson committed Jan 6, 2017
1 parent ac934bd commit 174c672
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ end

defp process_request_headers(headers), do: headers

defp process_request_options(options), do: options

defp process_response_chunk(chunk), do: chunk

defp process_headers(headers), do: headers
Expand Down
8 changes: 8 additions & 0 deletions lib/httpoison/base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ defmodule HTTPoison.Base do
@spec process_request_headers(term) :: [{binary, term}]
def process_request_headers(headers)
# Called to arbitrarily process the request options before sending them
# with the request.
@spec process_request_options(keyword) :: keyword
def process_request_options(options)
# Called before returning the response body returned by a request to the
# caller.
@spec process_response_body(binary) :: term
Expand Down Expand Up @@ -94,6 +99,8 @@ defmodule HTTPoison.Base do
end
def process_request_headers(headers), do: headers

def process_request_options(options), do: options

def process_response_chunk(chunk), do: chunk

def process_headers(headers), do: headers
Expand Down Expand Up @@ -160,6 +167,7 @@ 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
6 changes: 4 additions & 2 deletions test/httpoison_base_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule HTTPoisonBaseTest do
def process_url(url), do: "http://" <> url
def process_request_body(body), do: {:req_body, body}
def process_request_headers(headers), do: {:req_headers, headers}
def process_request_options(options), do: Keyword.put(options, :timeout, 10)
def process_response_body(body), do: {:resp_body, body}
def process_headers(headers), do: {:headers, headers}
def process_status_code(code), do: {:code, code}
Expand All @@ -17,6 +18,7 @@ defmodule HTTPoisonBaseTest do
defp process_url(url), do: "http://" <> url
defp process_request_body(body), do: {:req_body, body}
defp process_request_headers(headers), do: {:req_headers, headers}
defp process_request_options(options), do: Keyword.put(options, :timeout, 10)
defp process_response_body(body), do: {:resp_body, body}
defp process_headers(headers), do: {:headers, headers}
defp process_status_code(code), do: {:code, code}
Expand All @@ -29,7 +31,7 @@ defmodule HTTPoisonBaseTest do
end

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

Expand All @@ -42,7 +44,7 @@ defmodule HTTPoisonBaseTest do
end

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

Expand Down

0 comments on commit 174c672

Please sign in to comment.