diff --git a/README.md b/README.md index fd28720..641e8f9 100644 --- a/README.md +++ b/README.md @@ -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. \ No newline at end of file diff --git a/lib/amqp/connection.ex b/lib/amqp/connection.ex index c7528cd..5bc8e50 100644 --- a/lib/amqp/connection.ex +++ b/lib/amqp/connection.ex @@ -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), @@ -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, []), @@ -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. """ diff --git a/test/connection_test.exs b/test/connection_test.exs index 1cf8d34..45bf706 100644 --- a/test/connection_test.exs +++ b/test/connection_test.exs @@ -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 @@ -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:bar@amqp.test.com:12345" {:ok, amqp_params} = uri |> String.to_charlist() |> :amqp_uri.parse()