|
1 | 1 | import gleam/uri |
2 | 2 | import gleam/should |
| 3 | +import gleam/string |
| 4 | +import gleam/list |
3 | 5 | import gleam/option.{None, Some} |
4 | 6 |
|
5 | 7 | pub fn full_parse_test() { |
@@ -92,6 +94,78 @@ pub fn empty_query_to_string_test() { |
92 | 94 | should.equal(query_string, "") |
93 | 95 | } |
94 | 96 |
|
| 97 | +fn percent_codec_fixtures() { |
| 98 | + [ |
| 99 | + tuple(" ", "+"), |
| 100 | + tuple(",", "%2C"), |
| 101 | + tuple(";", "%3B"), |
| 102 | + tuple(":", "%3A"), |
| 103 | + tuple("!", "%21"), |
| 104 | + tuple("?", "%3F"), |
| 105 | + tuple("'", "%27"), |
| 106 | + tuple("(", "%28"), |
| 107 | + tuple(")", "%29"), |
| 108 | + tuple("[", "%5B"), |
| 109 | + tuple("@", "%40"), |
| 110 | + tuple("/", "%2F"), |
| 111 | + tuple("\\", "%5C"), |
| 112 | + tuple("&", "%26"), |
| 113 | + tuple("#", "%23"), |
| 114 | + tuple("=", "%3D"), |
| 115 | + tuple("~", "%7E"), |
| 116 | + tuple("ñ", "%C3%B1"), |
| 117 | + // Allowed chars |
| 118 | + tuple("-", "-"), |
| 119 | + tuple("_", "_"), |
| 120 | + tuple(".", "."), |
| 121 | + tuple("*", "*"), |
| 122 | + tuple("100% great", "100%25+great"), |
| 123 | + ] |
| 124 | +} |
| 125 | + |
| 126 | +pub fn percent_encode_test() { |
| 127 | + percent_codec_fixtures() |
| 128 | + |> list.map(fn(t) { |
| 129 | + let tuple(a, b) = t |
| 130 | + uri.percent_encode(a) |
| 131 | + |> should.equal(b) |
| 132 | + }) |
| 133 | +} |
| 134 | + |
| 135 | +pub fn percent_encode_consistency_test() { |
| 136 | + let k = "foo bar[]" |
| 137 | + let v = "ñaña (,:*~)" |
| 138 | + |
| 139 | + assert query_string = uri.query_to_string([tuple(k, v)]) |
| 140 | + |
| 141 | + let encoded_key = uri.percent_encode(k) |
| 142 | + let encoded_value = uri.percent_encode(v) |
| 143 | + let manual_query_string = string.concat([encoded_key, "=", encoded_value]) |
| 144 | + |
| 145 | + should.equal(query_string, manual_query_string) |
| 146 | +} |
| 147 | + |
| 148 | +pub fn percent_decode_test() { |
| 149 | + percent_codec_fixtures() |
| 150 | + |> list.map(fn(t) { |
| 151 | + let tuple(a, b) = t |
| 152 | + uri.percent_decode(b) |
| 153 | + |> should.equal(Ok(a)) |
| 154 | + }) |
| 155 | +} |
| 156 | + |
| 157 | +pub fn percent_decode_consistency_test() { |
| 158 | + let k = "foo+bar[]" |
| 159 | + let v = "%C3%B6rebro" |
| 160 | + let query = string.concat([k, "=", v]) |
| 161 | + assert Ok(parsed) = uri.parse_query(query) |
| 162 | + |
| 163 | + assert Ok(decoded_key) = uri.percent_decode(k) |
| 164 | + assert Ok(decoded_value) = uri.percent_decode(v) |
| 165 | + |
| 166 | + should.equal(parsed, [tuple(decoded_key, decoded_value)]) |
| 167 | +} |
| 168 | + |
95 | 169 | pub fn parse_segments_test() { |
96 | 170 | should.equal(uri.path_segments("/"), []) |
97 | 171 | should.equal(uri.path_segments("/foo/bar"), ["foo", "bar"]) |
|
0 commit comments