Skip to content

Commit 895dc4f

Browse files
committed
Properly underscore :rfc3986
1 parent c41ff4c commit 895dc4f

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
* [String] Add `:turkic` mode option to String case functions
4040
* [System] Add `System.trap_signal/3` and `System.untrap_signal/2`
4141
* [Tuple] Add `Tuple.sum/1` and `Tuple.product/1`
42-
* [URI] Add functions for RFC3986 compliant encoding and decoding of queries
42+
* [URI] Support RFC3986 compliant encoding and decoding of queries via the `:rfc3986` option
4343

4444
#### ExUnit
4545

lib/elixir/lib/uri.ex

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ defmodule URI do
8686
per `encode_www_form/1`. This is the format typically used by browsers on
8787
query strings and form data. It encodes " " as "+".
8888
89-
* `:rfc_3986` - (since v1.12.0) the same as `:www_form` except it encodes
89+
* `:rfc3986` - (since v1.12.0) the same as `:www_form` except it encodes
9090
" " as "%20" according [RFC 3986](https://tools.ietf.org/html/rfc3986).
9191
This is the best option if you are encoding in a non-browser situation,
9292
since encoding spaces as "+" can be ambiguous to URI parsers. This can
@@ -105,14 +105,14 @@ defmodule URI do
105105
"key=value+with+spaces"
106106
107107
iex> query = %{"key" => "value with spaces"}
108-
iex> URI.encode_query(query, :rfc_3986)
108+
iex> URI.encode_query(query, :rfc3986)
109109
"key=value%20with%20spaces"
110110
111111
iex> URI.encode_query(%{key: [:a, :list]})
112112
** (ArgumentError) encode_query/2 values cannot be lists, got: [:a, :list]
113113
114114
"""
115-
@spec encode_query(Enum.t(), :rfc_3986 | :www_form) :: binary
115+
@spec encode_query(Enum.t(), :rfc3986 | :www_form) :: binary
116116
def encode_query(enumerable, encoding \\ :www_form) do
117117
Enum.map_join(enumerable, "&", &encode_kv_pair(&1, encoding))
118118
end
@@ -125,7 +125,7 @@ defmodule URI do
125125
raise ArgumentError, "encode_query/2 values cannot be lists, got: #{inspect(value)}"
126126
end
127127

128-
defp encode_kv_pair({key, value}, :rfc_3986) do
128+
defp encode_kv_pair({key, value}, :rfc3986) do
129129
encode(Kernel.to_string(key), &char_unreserved?/1) <>
130130
"=" <> encode(Kernel.to_string(value), &char_unreserved?/1)
131131
end
@@ -148,7 +148,7 @@ defmodule URI do
148148
`decode_www_form/1`. This is the format typically used by browsers on
149149
query strings and form data. It decodes "+" as " ".
150150
151-
* `:rfc_3986` - (since v1.12.0) keys and values are decoded as per
151+
* `:rfc3986` - (since v1.12.0) keys and values are decoded as per
152152
`decode/1`. The result is the same as `:www_form` except for leaving "+"
153153
as is in line with [RFC 3986](https://tools.ietf.org/html/rfc3986).
154154
@@ -164,11 +164,11 @@ defmodule URI do
164164
iex> URI.decode_query("percent=oh+yes%21", %{"starting" => "map"})
165165
%{"percent" => "oh yes!", "starting" => "map"}
166166
167-
iex> URI.decode_query("percent=oh+yes%21", %{}, :rfc_3986)
167+
iex> URI.decode_query("percent=oh+yes%21", %{}, :rfc3986)
168168
%{"percent" => "oh+yes!"}
169169
170170
"""
171-
@spec decode_query(binary, %{optional(binary) => binary}, :rfc_3986 | :www_form) :: %{
171+
@spec decode_query(binary, %{optional(binary) => binary}, :rfc3986 | :www_form) :: %{
172172
optional(binary) => binary
173173
}
174174
def decode_query(query, map \\ %{}, encoding \\ :www_form)
@@ -227,7 +227,7 @@ defmodule URI do
227227
`decode_www_form/1`. This is the format typically used by browsers on
228228
query strings and form data. It decodes "+" as " ".
229229
230-
* `:rfc_3986` - (since v1.12.0) keys and values are decoded as per
230+
* `:rfc3986` - (since v1.12.0) keys and values are decoded as per
231231
`decode/1`. The result is the same as `:www_form` except for leaving "+"
232232
as is in line with [RFC 3986](https://tools.ietf.org/html/rfc3986).
233233
@@ -241,11 +241,11 @@ defmodule URI do
241241
iex> URI.query_decoder("food=bread%26butter&drinks=tap%20water+please") |> Enum.to_list()
242242
[{"food", "bread&butter"}, {"drinks", "tap water please"}]
243243
244-
iex> URI.query_decoder("food=bread%26butter&drinks=tap%20water+please", :rfc_3986) |> Enum.to_list()
244+
iex> URI.query_decoder("food=bread%26butter&drinks=tap%20water+please", :rfc3986) |> Enum.to_list()
245245
[{"food", "bread&butter"}, {"drinks", "tap water+please"}]
246246
247247
"""
248-
@spec query_decoder(binary, :rfc_3986 | :www_form) :: Enumerable.t()
248+
@spec query_decoder(binary, :rfc3986 | :www_form) :: Enumerable.t()
249249
def query_decoder(query, encoding \\ :www_form) when is_binary(query) do
250250
Stream.unfold(query, &decode_next_query_pair(&1, encoding))
251251
end
@@ -277,7 +277,7 @@ defmodule URI do
277277
decode_www_form(string)
278278
end
279279

280-
defp decode_with_encoding(string, :rfc_3986) do
280+
defp decode_with_encoding(string, :rfc3986) do
281281
decode(string)
282282
end
283283

lib/elixir/test/elixir/uri_test.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ defmodule URITest do
2323
assert URI.encode_query([{"foo", "bar"}, {"baz", "quux"}]) == "foo=bar&baz=quux"
2424

2525
assert URI.encode_query([{"foo z", :bar}]) == "foo+z=bar"
26-
assert URI.encode_query([{"foo z", :bar}], :rfc_3986) == "foo%20z=bar"
26+
assert URI.encode_query([{"foo z", :bar}], :rfc3986) == "foo%20z=bar"
2727
assert URI.encode_query([{"foo z", :bar}], :www_form) == "foo+z=bar"
2828

2929
assert URI.encode_query([{"foo[]", "+=/?&# Ñ"}]) ==
3030
"foo%5B%5D=%2B%3D%2F%3F%26%23+%C3%91"
3131

32-
assert URI.encode_query([{"foo[]", "+=/?&# Ñ"}], :rfc_3986) ==
32+
assert URI.encode_query([{"foo[]", "+=/?&# Ñ"}], :rfc3986) ==
3333
"foo%5B%5D=%2B%3D%2F%3F%26%23%20%C3%91"
3434

3535
assert URI.encode_query([{"foo[]", "+=/?&# Ñ"}], :www_form) ==
@@ -53,7 +53,7 @@ defmodule URITest do
5353
assert URI.decode_query("q=search%20query&cookie=ab%26cd&block+buster=") ==
5454
%{"block buster" => "", "cookie" => "ab&cd", "q" => "search query"}
5555

56-
assert URI.decode_query("q=search%20query&cookie=ab%26cd&block+buster=", %{}, :rfc_3986) ==
56+
assert URI.decode_query("q=search%20query&cookie=ab%26cd&block+buster=", %{}, :rfc3986) ==
5757
%{"block+buster" => "", "cookie" => "ab&cd", "q" => "search query"}
5858

5959
assert URI.decode_query("something=weird%3Dhappening") == %{"something" => "weird=happening"}
@@ -70,7 +70,7 @@ defmodule URITest do
7070
expected = [{"q", "search query"}, {"cookie", "ab&cd"}, {"block buster", ""}]
7171
assert Enum.map(decoder, & &1) == expected
7272

73-
decoder = URI.query_decoder("q=search%20query&cookie=ab%26cd&block+buster=", :rfc_3986)
73+
decoder = URI.query_decoder("q=search%20query&cookie=ab%26cd&block+buster=", :rfc3986)
7474
expected = [{"q", "search query"}, {"cookie", "ab&cd"}, {"block+buster", ""}]
7575
assert Enum.map(decoder, & &1) == expected
7676
end

0 commit comments

Comments
 (0)