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

add uri tests #48

Merged
merged 1 commit into from
May 13, 2016
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
6 changes: 3 additions & 3 deletions lib/stripe/uri.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ defmodule Stripe.URI do
Enumerable.impl_for(value) ->
pair(to_string(key), [], value)
true ->
param_name = key |> to_string |> URI.encode
param_value = value |> to_string |> URI.encode
param_name = key |> to_string |> URI.encode_www_form
param_value = value |> to_string |> URI.encode_www_form

"#{param_name}=#{param_value}"
end
Expand All @@ -57,7 +57,7 @@ defmodule Stripe.URI do

defp build_key(root, parents) do
path = Enum.map_join parents, "", fn x ->
param = x |> to_string |> URI.encode
param = x |> to_string |> URI.encode_www_form
"[#{param}]"
end

Expand Down
38 changes: 38 additions & 0 deletions test/stripe/uri_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
defmodule Stripe.UriTest do
use ExUnit.Case

test "keyword list encoding" do
assert Stripe.URI.encode_query([a: 1]) == "a=1"
assert Stripe.URI.encode_query([a: 1, b: 2]) == "a=1&b=2"
assert Stripe.URI.encode_query(["a": "1", "b": "2"]) == "a=1&b=2"
assert Stripe.URI.encode_query([a: nil, b: nil]) == "a=&b="
assert Stripe.URI.encode_query(["foo z": "true"]) == "foo+z=true"
assert Stripe.URI.encode_query(["test &": "test &"]) == "test+%26=test+%26"
assert Stripe.URI.encode_query([a: "4test ~1.x"]) == "a=4test+~1.x"
assert Stripe.URI.encode_query([a: "poll:146%"]) == "a=poll%3A146%25"
assert Stripe.URI.encode_query([a: "/\n+/ゆ"]) == "a=%2F%0A%2B%2F%E3%82%86"
assert Stripe.URI.encode_query([a: "/\n+/ゆ"]) == "a=%2F%0A%2B%2F%E3%82%86"
end

test "nested list encoding" do
assert Stripe.URI.encode_query([a: [a: 1]]) == "a[a]=1"
assert Stripe.URI.encode_query([a: [b: 1]]) == "a[b]=1"
assert Stripe.URI.encode_query([a: [b: [c: 1]]]) == "a[b][c]=1"
assert Stripe.URI.encode_query([a: [b: ["test &": "test &"]]]) == "a[b][test+%26]=test+%26"
assert Stripe.URI.encode_query([a: [a: "/\n+/ゆ"]]) == "a[a]=%2F%0A%2B%2F%E3%82%86"
assert Stripe.URI.encode_query(%{a: [a: "test &"]}) == "a[a]=test+%26"
end

test "map list encoding" do
assert Stripe.URI.encode_query(%{a: 1}) == "a=1"
assert Stripe.URI.encode_query(%{a: 1, b: 2}) == "a=1&b=2"
assert Stripe.URI.encode_query(%{"a": "1", "b": "2"}) == "a=1&b=2"
assert Stripe.URI.encode_query(%{a: nil, b: nil}) == "a=&b="
assert Stripe.URI.encode_query(%{"foo z": "true"}) == "foo+z=true"
assert Stripe.URI.encode_query(%{"test &": "test &"}) == "test+%26=test+%26"
assert Stripe.URI.encode_query(%{a: "4test ~1.x"}) == "a=4test+~1.x"
assert Stripe.URI.encode_query(%{a: "poll:146%"}) == "a=poll%3A146%25"
assert Stripe.URI.encode_query(%{a: "/\n+/ゆ"}) == "a=%2F%0A%2B%2F%E3%82%86"
assert Stripe.URI.encode_query(%{a: "/\n+/ゆ"}) == "a=%2F%0A%2B%2F%E3%82%86"
end
end