Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for port to be configured as a string #163

Merged
merged 4 commits into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,4 @@ Update amqp to [1.1.0](https://github.com/pma/amqp/releases/tag/v1.1.0) or a gre

#### Does the library support AMQP 1.0?

Currently the library doesn't support AMQP 1.0 and there is no plan to do so at the moment. Our main aim here (at least for now) is to provide a thin wrapper around [amqp_client](https://hex.pm/packages/amqp_client) for Elixir programmers.
Currently the library doesn't support AMQP 1.0 and there is no plan to do so at the moment. Our main aim here (at least for now) is to provide a thin wrapper around [amqp_client](https://hex.pm/packages/amqp_client) for Elixir programmers.
28 changes: 16 additions & 12 deletions lib/amqp/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,12 @@ defmodule AMQP.Connection do
username: keys_get(options, params, :username),
password: keys_get(options, params, :password),
virtual_host: keys_get(options, params, :virtual_host),
host: keys_get(options, params, :host) |> to_charlist,
port: keys_get(options, params, :port),
channel_max: keys_get(options, params, :channel_max),
frame_max: keys_get(options, params, :frame_max),
heartbeat: keys_get(options, params, :heartbeat),
connection_timeout: keys_get(options, params, :connection_timeout),
host: keys_get(options, params, :host) |> to_charlist(),
port: keys_get(options, params, :port) |> normalize_int_opt(),
channel_max: keys_get(options, params, :channel_max) |> normalize_int_opt(),
frame_max: keys_get(options, params, :frame_max) |> normalize_int_opt(),
heartbeat: keys_get(options, params, :heartbeat) |> normalize_int_opt(),
connection_timeout: keys_get(options, params, :connection_timeout) |> normalize_int_opt(),
ssl_options: keys_get(options, params, :ssl_options),
client_properties: keys_get(options, params, :client_properties),
socket_options: keys_get(options, params, :socket_options),
Expand All @@ -171,12 +171,12 @@ defmodule AMQP.Connection do
username: Keyword.get(options, :username, "guest"),
password: Keyword.get(options, :password, "guest"),
virtual_host: Keyword.get(options, :virtual_host, "/"),
host: Keyword.get(options, :host, 'localhost') |> to_charlist,
port: Keyword.get(options, :port, :undefined),
channel_max: Keyword.get(options, :channel_max, 0),
frame_max: Keyword.get(options, :frame_max, 0),
heartbeat: Keyword.get(options, :heartbeat, 10),
connection_timeout: Keyword.get(options, :connection_timeout, 50000),
host: Keyword.get(options, :host, 'localhost') |> to_charlist(),
port: Keyword.get(options, :port, :undefined) |> normalize_int_opt(),
channel_max: Keyword.get(options, :channel_max, 0) |> normalize_int_opt(),
frame_max: Keyword.get(options, :frame_max, 0) |> normalize_int_opt(),
heartbeat: Keyword.get(options, :heartbeat, 10) |> normalize_int_opt(),
connection_timeout: Keyword.get(options, :connection_timeout, 50000) |> normalize_int_opt(),
ssl_options: Keyword.get(options, :ssl_options, :none),
client_properties: Keyword.get(options, :client_properties, []),
socket_options: Keyword.get(options, :socket_options, []),
Expand All @@ -188,6 +188,10 @@ defmodule AMQP.Connection do
)
end

# If an integer value is configured as a string, cast it to an integer where applicable
defp normalize_int_opt(value) when is_binary(value), do: String.to_integer(value)
defp normalize_int_opt(value), do: value

@doc """
Closes an open Connection.
"""
Expand Down
27 changes: 26 additions & 1 deletion test/connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ defmodule ConnectionTest do
end

test "open connection with host as binary" do
assert {:ok, conn} = Connection.open(host: "localhost")
assert {:ok, conn} = Connection.open(host: "localhost", port: 5672)
assert :ok = Connection.close(conn)
end

test "open connection with port as binary" do
assert {:ok, conn} = Connection.open(host: "localhost", port: "5672")
assert :ok = Connection.close(conn)
end

Expand All @@ -35,6 +40,26 @@ defmodule ConnectionTest do
assert :ok = Connection.close(conn)
end

test "open connection with uri, name, port as an integer, and options " do
assert {:ok, conn} =
Connection.open("amqp://nonexistent", "my-connection",
host: 'localhost',
port: 5672
)

assert :ok = Connection.close(conn)
end

test "open connection with uri, name, port as a string, and options " do
assert {:ok, conn} =
Connection.open("amqp://nonexistent", "my-connection",
host: 'localhost',
port: "5672"
)

assert :ok = Connection.close(conn)
end

test "override uri with options" do
uri = "amqp://foo:[email protected]:12345"
{:ok, amqp_params} = uri |> String.to_charlist() |> :amqp_uri.parse()
Expand Down