diff options
author | Louis Pilfold <louis@lpil.uk> | 2021-09-07 19:36:31 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2021-09-07 19:36:31 +0100 |
commit | 945152e8075e04d91aac193608ecc84b769c7ba4 (patch) | |
tree | 3a60c06153419138ab58d97997709eb0d7fee5ee /test | |
parent | b611364f02679c6c44592082db6012f2c27a0750 (diff) | |
download | gleam_stdlib-945152e8075e04d91aac193608ecc84b769c7ba4.tar.gz gleam_stdlib-945152e8075e04d91aac193608ecc84b769c7ba4.zip |
URI percent encode and decode for JS
Diffstat (limited to 'test')
-rw-r--r-- | test/gleam/should.gleam | 2 | ||||
-rw-r--r-- | test/gleam/uri_test.gleam | 143 |
2 files changed, 71 insertions, 74 deletions
diff --git a/test/gleam/should.gleam b/test/gleam/should.gleam index 044190f..483b1a3 100644 --- a/test/gleam/should.gleam +++ b/test/gleam/should.gleam @@ -5,8 +5,6 @@ //// documentation](https://rebar3.org/docs/testing/eunit/). if erlang { - // TODO: Move this module into another package so it can be used as a - // dep only in test. pub external fn equal(a, a) -> Nil = "gleam_stdlib" "should_equal" diff --git a/test/gleam/uri_test.gleam b/test/gleam/uri_test.gleam index ca00200..1c8e0c1 100644 --- a/test/gleam/uri_test.gleam +++ b/test/gleam/uri_test.gleam @@ -5,6 +5,7 @@ import gleam/should import gleam/option.{None, Some} import gleam/string import gleam/list +import gleam/io pub fn full_parse_test() { let parsed = @@ -319,90 +320,88 @@ pub fn error_parsing_query_test() { should.equal(uri.parse_query("%C2"), Error(Nil)) } -if erlang { - pub fn query_to_string_test() { - let query_string = - uri.query_to_string([#("weebl bob", "1"), #("city", "örebro")]) - should.equal(query_string, "weebl+bob=1&city=%C3%B6rebro") - } +pub fn query_to_string_test() { + let query_string = + uri.query_to_string([#("weebl bob", "1"), #("city", "örebro")]) + should.equal(query_string, "weebl%20bob=1&city=%C3%B6rebro") +} - pub fn empty_query_to_string_test() { - let query_string = uri.query_to_string([]) - should.equal(query_string, "") - } +pub fn empty_query_to_string_test() { + let query_string = uri.query_to_string([]) + should.equal(query_string, "") +} - fn percent_codec_fixtures() { - [ - #(" ", "+"), - #(",", "%2C"), - #(";", "%3B"), - #(":", "%3A"), - #("!", "%21"), - #("?", "%3F"), - #("'", "%27"), - #("(", "%28"), - #(")", "%29"), - #("[", "%5B"), - #("@", "%40"), - #("/", "%2F"), - #("\\", "%5C"), - #("&", "%26"), - #("#", "%23"), - #("=", "%3D"), - #("~", "%7E"), - #("ñ", "%C3%B1"), - // Allowed chars - #("-", "-"), - #("_", "_"), - #(".", "."), - #("*", "*"), - #("100% great", "100%25+great"), - ] - } +const percent_codec_fixtures = [ + #(" ", "%20"), + #(",", "%2C"), + #(";", "%3B"), + #(":", "%3A"), + #("!", "!"), + #("?", "%3F"), + #("'", "'"), + #("(", "("), + #(")", ")"), + #("[", "%5B"), + #("@", "%40"), + #("/", "%2F"), + #("\\", "%5C"), + #("&", "%26"), + #("#", "%23"), + #("=", "%3D"), + #("~", "~"), + #("ñ", "%C3%B1"), + #("-", "-"), + #("_", "_"), + #(".", "."), + #("*", "*"), + #("100% great", "100%25%20great"), +] - pub fn percent_encode_test() { - percent_codec_fixtures() - |> list.map(fn(t) { - let #(a, b) = t - uri.percent_encode(a) - |> should.equal(b) - }) - } +// Allowed chars +pub fn percent_encode_test() { + percent_codec_fixtures + |> list.map(fn(t) { + let #(a, b) = t + uri.percent_encode(a) + |> should.equal(b) + }) +} - pub fn percent_encode_consistency_test() { - let k = "weebl bob[]" - let v = "ñaña (,:*~)" +pub fn percent_encode_consistency_test() { + let k = "weebl bob[]" + let v = "ñaña (,:*~)" - let query_string = uri.query_to_string([#(k, v)]) + let query_string = uri.query_to_string([#(k, v)]) - let encoded_key = uri.percent_encode(k) - let encoded_value = uri.percent_encode(v) - let manual_query_string = string.concat([encoded_key, "=", encoded_value]) + let encoded_key = uri.percent_encode(k) + let encoded_value = uri.percent_encode(v) + let manual_query_string = string.concat([encoded_key, "=", encoded_value]) - should.equal(query_string, manual_query_string) - } + should.equal(query_string, manual_query_string) +} - pub fn percent_decode_test() { - percent_codec_fixtures() - |> list.map(fn(t) { - let #(a, b) = t - uri.percent_decode(b) - |> should.equal(Ok(a)) - }) - } +pub fn percent_decode_test() { + percent_codec_fixtures + |> list.map(fn(t) { + let #(a, b) = t + uri.percent_decode(b) + |> should.equal(Ok(a)) + }) +} - pub fn percent_decode_consistency_test() { - let k = "weebl+bob[]" - let v = "%C3%B6rebro" - let query = string.concat([k, "=", v]) - assert Ok(parsed) = uri.parse_query(query) +pub fn percent_decode_consistency_test() { + let k = "weebl%20bob[]" + let v = "%C3%B6rebro" + let query = string.concat([k, "=", v]) + assert Ok(parsed) = uri.parse_query(query) - assert Ok(decoded_key) = uri.percent_decode(k) - assert Ok(decoded_value) = uri.percent_decode(v) + assert Ok(decoded_key) = uri.percent_decode(k) + assert Ok(decoded_value) = uri.percent_decode(v) - should.equal(parsed, [#(decoded_key, decoded_value)]) - } + should.equal(parsed, [#(decoded_key, decoded_value)]) +} +if erlang { pub fn parse_segments_test() { should.equal(uri.path_segments("/"), []) should.equal(uri.path_segments("/weebl/bob"), ["weebl", "bob"]) |