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

Fix tests for OTP26 #1166

Merged
merged 2 commits into from
Sep 24, 2023
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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ jobs:
elixir: 1.10.4
otp: 21.3
- pair:
elixir: 1.14.4
otp: 25.3
elixir: 1.15.6
otp: 26.0
lint: lint
steps:
- uses: actions/checkout@v3
Expand Down
4 changes: 2 additions & 2 deletions lib/plug/conn/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ defmodule Plug.Conn.Query do

Maps can be encoded:

iex> encode(%{foo: "bar", baz: "bat"})
"baz=bat&foo=bar"
iex> encode(%{foo: "bar"})
"foo=bar"

Encoding keyword lists preserves the order of the fields:

Expand Down
23 changes: 15 additions & 8 deletions test/plug/conn/query_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,18 @@

describe "encode" do
test "data" do
assert encode(%{foo: "bar", baz: "bat"}) == "baz=bat&foo=bar"
assert encode(%{foo: "bar", baz: "bat"}) in ["baz=bat&foo=bar", "foo=bar&baz=bat"]

assert encode(%{foo: nil}) == "foo="
assert encode(%{foo: "bå®"}) == "foo=b%C3%A5%C2%AE"
assert encode(%{foo: 1337}) == "foo=1337"
assert encode(%{foo: ["bar", "baz"]}) == "foo[]=bar&foo[]=baz"

assert encode(%{users: %{name: "hello", age: 17}}) == "users[age]=17&users[name]=hello"
assert encode(%{users: %{name: "hello", age: 17}}) in [
"users[name]=hello&users[age]=17",
"users[age]=17&users[name]=hello"
]

assert encode(%{users: [name: "hello", age: 17]}) == "users[name]=hello&users[age]=17"

assert encode(%{users: [name: "hello", age: 17, name: "goodbye"]}) ==
Expand All @@ -89,7 +93,10 @@

assert encode(%{foo: [""]}) == "foo[]="

assert encode(%{foo: ["bar", "baz"], bat: [1, 2]}) == "bat[]=1&bat[]=2&foo[]=bar&foo[]=baz"
assert encode(%{foo: ["bar", "baz"], bat: [1, 2]}) in [
"bat[]=1&bat[]=2&foo[]=bar&foo[]=baz",
"foo[]=bar&foo[]=baz&bat[]=1&bat[]=2"
]

assert encode(%{x: %{y: %{z: 1}}}) == "x[y][z]=1"
assert encode(%{x: %{y: %{z: [1]}}}) == "x[y][z][]=1"
Expand All @@ -105,14 +112,14 @@
test "with custom encoder" do
encoder = &(&1 |> to_string |> String.duplicate(2))

assert encode(%{foo: "bar", baz: "bat"}, encoder) == "baz=batbat&foo=barbar"
assert encode(%{foo: ["bar", "baz"]}, encoder) == "foo[]=barbar&foo[]=bazbaz"
assert encode(%{foo: URI.parse("/bar")}, encoder) == "foo=%2Fbar%2Fbar"
assert encode([foo: "bar", baz: "bat"], encoder) == "foo=barbar&baz=batbat"
assert encode([foo: ["bar", "baz"]], encoder) == "foo[]=barbar&foo[]=bazbaz"
assert encode([foo: URI.parse("/bar")], encoder) == "foo=%2Fbar%2Fbar"
end

test "ignores empty maps or lists" do
assert encode(%{filter: %{}, foo: "bar", baz: "bat"}) == "baz=bat&foo=bar"
assert encode(%{filter: [], foo: "bar", baz: "bat"}) == "baz=bat&foo=bar"
assert encode(filter: %{}, foo: "bar", baz: "bat") == "foo=bar&baz=bat"
assert encode(filter: [], foo: "bar", baz: "bat") == "foo=bar&baz=bat"
end

test "raises when there's a map with 0 or >1 elems in a list" do
Expand Down Expand Up @@ -174,7 +181,7 @@
end

defp decode_pair(pairs) do
Enum.reduce(Enum.reverse(pairs), %{}, &Plug.Conn.Query.decode_pair(&1, &2))

Check warning on line 184 in test/plug/conn/query_test.exs

View workflow job for this annotation

GitHub Actions / test (1.10.4, 21.3)

Plug.Conn.Query.decode_pair/2 is deprecated. Use decode_init/0, decode_each/2, and decode_done/2 instead

Check warning on line 184 in test/plug/conn/query_test.exs

View workflow job for this annotation

GitHub Actions / test (1.15.6, 26, lint)

Plug.Conn.Query.decode_pair/2 is deprecated. Use decode_init/0, decode_each/2, and decode_done/2 instead
end
end
end
Loading