-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: avoid overriding user config if possible (#236)
* fix: avoid overriding user config if possible * feat: make transport option replacement work * fix: || %{} * fix: override correctly * docs: improve credential docs
- Loading branch information
1 parent
da37494
commit 1e0598b
Showing
6 changed files
with
172 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
defmodule GRPC.Client.Adapters.GunTest do | ||
use ExUnit.Case, async: true | ||
|
||
import GRPC.Factory | ||
|
||
alias GRPC.Client.Adapters.Gun | ||
|
||
describe "connect/2" do | ||
setup do | ||
server_credential = build(:credential) | ||
{:ok, _, port} = GRPC.Server.start(FeatureServer, 0, cred: server_credential) | ||
|
||
on_exit(fn -> | ||
:ok = GRPC.Server.stop(FeatureServer) | ||
end) | ||
|
||
%{ | ||
port: port, | ||
credential: | ||
build(:credential, | ||
ssl: Keyword.take(server_credential.ssl, [:certfile, :keyfile, :versions]) | ||
) | ||
} | ||
end | ||
|
||
test "connects insecurely (default options)", %{port: port, credential: credential} do | ||
channel = build(:channel, port: port, host: "localhost", cred: credential) | ||
|
||
assert {:ok, result} = Gun.connect(channel) | ||
|
||
assert %{channel | adapter_payload: %{conn_pid: result.adapter_payload.conn_pid}} == result | ||
end | ||
|
||
test "connects insecurely (custom options)", %{port: port, credential: credential} do | ||
channel = build(:channel, port: port, host: "localhost", cred: credential) | ||
|
||
# Ensure that it works | ||
assert {:ok, result} = Gun.connect(channel, %{transport_opts: [ip: :loopback]}) | ||
assert %{channel | adapter_payload: %{conn_pid: result.adapter_payload.conn_pid}} == result | ||
|
||
# Ensure that changing one of the options breaks things | ||
assert {:error, {:down, :badarg}} == | ||
Gun.connect(channel, %{transport_opts: [ip: "256.0.0.0"]}) | ||
end | ||
|
||
test "connects securely (default options)", %{port: port, credential: credential} do | ||
channel = | ||
build(:channel, | ||
port: port, | ||
scheme: "https", | ||
host: "localhost", | ||
cred: credential | ||
) | ||
|
||
assert {:ok, result} = Gun.connect(channel, %{tls_opts: channel.cred.ssl}) | ||
|
||
assert %{channel | adapter_payload: %{conn_pid: result.adapter_payload.conn_pid}} == result | ||
end | ||
|
||
test "connects securely (custom options)", %{port: port, credential: credential} do | ||
channel = | ||
build(:channel, | ||
port: port, | ||
scheme: "https", | ||
host: "localhost", | ||
cred: credential | ||
) | ||
|
||
# Ensure that it works | ||
assert {:ok, result} = | ||
Gun.connect(channel, %{ | ||
transport_opts: [certfile: credential.ssl[:certfile], ip: :loopback] | ||
}) | ||
|
||
assert %{channel | adapter_payload: %{conn_pid: result.adapter_payload.conn_pid}} == result | ||
|
||
# Ensure that changing one of the options breaks things | ||
assert {:error, :timeout} == | ||
Gun.connect(channel, %{ | ||
transport_opts: [ | ||
certfile: credential.ssl[:certfile] <> "invalidsuffix", | ||
ip: :loopback | ||
] | ||
}) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
defmodule GRPC.Factory do | ||
@moduledoc false | ||
|
||
alias GRPC.Channel | ||
alias GRPC.Credential | ||
|
||
@cert_path Path.expand("./tls/server1.pem", :code.priv_dir(:grpc)) | ||
@key_path Path.expand("./tls/server1.key", :code.priv_dir(:grpc)) | ||
@ca_path Path.expand("./tls/ca.pem", :code.priv_dir(:grpc)) | ||
|
||
def build(resource, attrs \\ %{}) do | ||
name = :"#{resource}_factory" | ||
|
||
data = | ||
if function_exported?(__MODULE__, name, 1) do | ||
apply(__MODULE__, name, [attrs]) | ||
else | ||
apply(__MODULE__, name, []) | ||
end | ||
|
||
Map.merge(data, Map.new(attrs)) | ||
end | ||
|
||
def channel_factory do | ||
%Channel{ | ||
host: "localhost", | ||
port: 1337, | ||
scheme: "http", | ||
cred: build(:credential), | ||
adapter: GRPC.Client.Adapters.Gun, | ||
adapter_payload: %{}, | ||
codec: GRPC.Codec.Proto, | ||
interceptors: [], | ||
compressor: nil, | ||
accepted_compressors: [], | ||
headers: [] | ||
} | ||
end | ||
|
||
def credential_factory do | ||
%Credential{ | ||
ssl: [ | ||
certfile: @cert_path, | ||
cacertfile: @ca_path, | ||
keyfile: @key_path, | ||
verify: :verify_peer, | ||
fail_if_no_peer_cert: true, | ||
versions: [:"tlsv1.2"] | ||
] | ||
} | ||
end | ||
end |