Skip to content

Commit 206e246

Browse files
authored
Accept custom http options (#319)
* Accept custom http options * Add HTTP options docs to README
1 parent 9ab49e6 commit 206e246

File tree

3 files changed

+62
-11
lines changed

3 files changed

+62
-11
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,28 @@ Coverage data is imported after tests are run.
549549
550550
See the `mix test` [Coverage documentation](https://hexdocs.pm/mix/Mix.Tasks.Test.html#module-coverage) for more information on `.coverdata`.
551551
552+
### Configuring HTTP Options in ExCoveralls
553+
554+
You can customize the HTTP options used by [`:httpc`](https://www.erlang.org/doc/man/httpc.html) when posting results. The example below shows how to specify a custom `cacertfile`:
555+
556+
```elixir
557+
config :excoveralls,
558+
http_options: [
559+
timeout: 10_000,
560+
ssl: [
561+
# Refer to the secure coding guide:
562+
# https://erlef.github.io/security-wg/secure_coding_and_deployment_hardening/inets
563+
verify: :verify_peer,
564+
depth: 2,
565+
customize_hostname_check: [
566+
match_fun: :public_key.pkix_verify_hostname_match_fun(:https)
567+
],
568+
cacertfile: to_charlist(System.fetch_env!("TEST_COVERAGE_CACERTFILE"))
569+
]
570+
```
571+
572+
By default, ExCoveralls uses the `cacertfile` from [`castore`](https://hexdocs.pm/castore/api-reference.html) when the dependency is installed. If it's not available and you're running Erlang `25` or later, the system will attempt to use the OS certificates via [`:public_key.cacerts_load/0`](https://www.erlang.org/doc/man/public_key.html#cacerts_load-0).
573+
552574
### Notes
553575
- If mock library is used, it will show some warnings during execution.
554576
- https://github.com/eproxus/meck/pull/17

lib/excoveralls/poster.ex

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,25 @@ defmodule ExCoveralls.Poster do
5959
body
6060
}
6161

62-
http_options = [
63-
timeout: 10_000,
64-
ssl:
65-
[
66-
verify: :verify_peer,
67-
depth: 2,
68-
customize_hostname_check: [
69-
match_fun: :public_key.pkix_verify_hostname_match_fun(:https)
62+
http_options =
63+
case Application.get_env(:excoveralls, :http_options) do
64+
[_ | _] = options ->
65+
options
66+
67+
_ ->
68+
[
69+
timeout: 10_000,
70+
ssl:
71+
[
72+
verify: :verify_peer,
73+
depth: 2,
74+
customize_hostname_check: [
75+
match_fun: :public_key.pkix_verify_hostname_match_fun(:https)
76+
]
77+
# https://erlef.github.io/security-wg/secure_coding_and_deployment_hardening/inets
78+
] ++ cacert_option()
7079
]
71-
# https://erlef.github.io/security-wg/secure_coding_and_deployment_hardening/inets
72-
] ++ cacert_option()
73-
]
80+
end
7481

7582
case :httpc.request(:post, request, http_options, sync: true, body_format: :binary) do
7683
{:ok, {{_protocol, status_code, _status_message}, _headers, _body}}

test/poster_test.exs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,26 @@ defmodule PosterTest do
4848
assert ExCoveralls.Poster.execute("{}", endpoint: endpoint) == :ok
4949
end) =~ ~r/maintenance/
5050
end
51+
52+
test "passes custom http options when configured", %{bypass: bypass, endpoint: endpoint} do
53+
Application.put_env(:excoveralls, :http_options, autoredirect: false)
54+
55+
on_exit(fn ->
56+
Application.delete_env(:excoveralls, :http_options)
57+
end)
58+
59+
Bypass.expect_once(bypass, "POST", "/api/v1/jobs", fn conn ->
60+
conn
61+
|> Plug.Conn.put_resp_header("location", Path.join(endpoint, "redirected") |> IO.inspect())
62+
|> Plug.Conn.resp(302, "")
63+
end)
64+
65+
assert_raise(
66+
ExCoveralls.ReportUploadError,
67+
"Failed to upload the report to '#{endpoint}' (reason: status_code = 302, body = ).",
68+
fn ->
69+
ExCoveralls.Poster.execute("{}", endpoint: endpoint) == :ok
70+
end
71+
)
72+
end
5173
end

0 commit comments

Comments
 (0)