From b108d7d82ba597fd9aade38acc5533697c952ff0 Mon Sep 17 00:00:00 2001 From: Gareth Pendleton Date: Sun, 7 Sep 2025 13:28:50 +0100 Subject: [PATCH] test: Finish test suite --- src/uri.gleam | 204 +++++++++++-- test/uri_test.gleam | 701 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 877 insertions(+), 28 deletions(-) diff --git a/src/uri.gleam b/src/uri.gleam index 2d6a805..13e9321 100644 --- a/src/uri.gleam +++ b/src/uri.gleam @@ -20,14 +20,6 @@ pub type Uri { pub const empty_uri = Uri(None, None, None, None, "", None, None) -pub fn main() { - // parse("test://asd@test!te%20ste/a/b/c?a=31#asd") |> echo - // parse("test:/blah") |> echo - parse("foo://user:password@localhost") |> echo - uri.parse("foo://user:password@localhost") |> echo - // uri.parse("../../") |> echo -} - pub fn parse(uri: String) -> Result(Uri, Nil) { case parse_scheme(uri) { Ok(#(scheme, rest)) -> { @@ -96,22 +88,26 @@ fn parse_relative_part(str: String) -> Result(#(Uri, String), Nil) { fn parse_absolute(str: String) -> Result(#(Uri, String), Nil) { case str { "/" <> rest -> { - use #(seg1, rest) <- result.try(do_parse_segment_nz(rest)) + let assert Ok(#(seg, rest)) = + parse_optional(rest, parse_this_then( + [ + do_parse_segment_nz, + get_multiple_optional_result( + fn(str) { + case str { + "/" <> rest -> { + do_parse_segment(rest, do_parse_pchar, "/") + } + _ -> Error(Nil) + } + }, + _, + ), + ], + _, + )) - let #(segs, rest) = - get_multiple_optional( - fn(str) { - case str { - "/" <> rest -> { - do_parse_segment(rest, do_parse_pchar, "/") - } - _ -> Error(Nil) - } - }, - rest, - ) - - Ok(#(Uri(None, None, None, None, "/" <> seg1 <> segs, None, None), rest)) + Ok(#(Uri(None, None, None, None, "/" <> seg, None, None), rest)) } _ -> Error(Nil) } @@ -155,6 +151,17 @@ fn parse_noscheme(str: String) -> Result(#(Uri, String), Nil) { Ok(#(Uri(None, None, None, None, seg1 <> segs, None, None), rest)) } +fn parse_optional(str, opt_fn) { + case opt_fn(str) { + Error(Nil) -> Ok(#("", str)) + Ok(r) -> Ok(r) + } +} + +fn get_multiple_optional_result(opt_fn, str: String) { + get_multiple_optional(opt_fn, str) |> Ok +} + fn get_multiple_optional(opt_fn, str: String) { case get_multiple(opt_fn, str) { Error(_) -> #("", str) @@ -224,6 +231,149 @@ fn parse_host(str: String) { } fn parse_ip_literal(str: String) { + case str { + "[" <> rest -> { + use #(ip, rest) <- result.try(list.fold_until( + [parse_ipv6, parse_ipfuture], + Error(Nil), + get_parser_fn(rest), + )) + case rest { + "]" <> rest -> Ok(#(ip, rest)) + _ -> Error(Nil) + } + } + _ -> Error(Nil) + } +} + +fn parse_ipv6(str: String) { + list.fold_until( + [ + parse_this_then([parse_min_max(_, 6, 6, parse_h16_colon), parse_ls32], _), + parse_this_then( + [parse_colons, parse_min_max(_, 5, 5, parse_h16_colon), parse_ls32], + _, + ), + parse_this_then( + [ + parse_optional(_, parse_h16), + parse_colons, + parse_min_max(_, 4, 4, parse_h16_colon), + parse_ls32, + ], + _, + ), + parse_this_then( + [ + parse_optional(_, parse_this_then([parse_h16s(_, 1), parse_h16], _)), + parse_colons, + parse_min_max(_, 3, 3, parse_h16_colon), + parse_ls32, + ], + _, + ), + parse_this_then( + [ + parse_optional(_, parse_this_then([parse_h16s(_, 2), parse_h16], _)), + parse_colons, + parse_min_max(_, 2, 2, parse_h16_colon), + parse_ls32, + ], + _, + ), + parse_this_then( + [ + parse_optional(_, parse_this_then([parse_h16s(_, 3), parse_h16], _)), + parse_colons, + parse_min_max(_, 1, 1, parse_h16_colon), + parse_ls32, + ], + _, + ), + parse_this_then( + [ + parse_optional(_, parse_this_then([parse_h16s(_, 4), parse_h16], _)), + parse_colons, + parse_ls32, + ], + _, + ), + parse_this_then( + [ + parse_optional(_, parse_this_then([parse_h16s(_, 5), parse_h16], _)), + parse_colons, + parse_h16, + ], + _, + ), + parse_this_then( + [ + parse_optional(_, parse_this_then([parse_h16s(_, 6), parse_h16], _)), + parse_colons, + ], + _, + ), + ], + Error(Nil), + get_parser_fn(str), + ) +} + +fn parse_h16s(str: String, max) { + parse_min_max(str, 0, max, parse_h16_colon) +} + +fn parse_colons(str: String) { + case str { + "::" <> rest -> Ok(#("::", rest)) + _ -> Error(Nil) + } +} + +fn parse_this_then( + parsers: List(fn(String) -> Result(#(String, String), Nil)), + str: String, +) { + list.fold_until(parsers, Ok(#("", str)), fn(acc, parser) { + let assert Ok(#(res, str)) = acc + case parser(str) { + Ok(#(res2, rest)) -> { + Continue(Ok(#(res <> res2, rest))) + } + Error(Nil) -> Stop(Error(Nil)) + } + }) +} + +fn parse_ls32(str: String) -> Result(#(String, String), Nil) { + list.fold_until([parse_h16_pair, parse_ipv4], Error(Nil), get_parser_fn(str)) +} + +fn parse_h16_pair(str: String) { + use #(h16a, rest) <- result.try(parse_h16(str)) + case rest { + ":" <> rest -> { + use #(h16b, rest) <- result.try(parse_h16(rest)) + Ok(#(h16a <> ":" <> h16b, rest)) + } + _ -> Error(Nil) + } +} + +fn parse_h16(str: String) { + parse_hex_digits(str, 1, 4) +} + +fn parse_h16_colon(str: String) { + use #(h16, rest) <- result.try(parse_h16(str)) + case rest { + ":" <> rest -> Ok(#(h16 <> ":", rest)) + _ -> Error(Nil) + } +} + +fn parse_ipfuture(str: String) { Error(Nil) } @@ -530,13 +680,13 @@ fn get_parser_fn( } } -fn parse_hex_digits(str, min, max) { +fn parse_min_max(str, min, max, parse_fn) { use <- bool.guard(when: min < 0 || max <= 0 || min > max, return: Error(Nil)) case list.repeat("", max) |> list.fold_until(Ok(#("", str, 0)), fn(acc, _) { let assert Ok(#(hex, str, i)) = acc - case parse_hex_digit(str) { + case parse_fn(str) { Error(_) -> case i < min { True -> Stop(Error(Nil)) @@ -551,6 +701,10 @@ fn parse_hex_digits(str, min, max) { } } +fn parse_hex_digits(str, min, max) { + parse_min_max(str, min, max, parse_hex_digit) +} + fn parse_hex_digit(str) { case str { "0" as l <> rest diff --git a/test/uri_test.gleam b/test/uri_test.gleam index 7a49a77..7e38d0e 100644 --- a/test/uri_test.gleam +++ b/test/uri_test.gleam @@ -1,4 +1,4 @@ -import gleam/option.{Some} +import gleam/option.{None, Some} import gleeunit/should import startest.{describe, it} import uri.{Uri} @@ -9,7 +9,7 @@ pub fn main() { pub fn parse_scheme_tests() { describe("scheme parsing", [ - it("should parse", fn() { + it("simple parse", fn() { uri.parse("") |> should.equal(Ok(uri.empty_uri)) uri.parse("foo") |> should.equal(Ok(Uri(..uri.empty_uri, path: "foo"))) @@ -37,7 +37,7 @@ pub fn parse_scheme_tests() { pub fn parse_userinfo_tests() { describe("userinfo parsing", [ - it("should parse", fn() { + it("simple parse", fn() { uri.parse("user:password@localhost") |> should.equal(Ok( Uri(..uri.empty_uri, scheme: Some("user"), path: "password@localhost"), @@ -83,6 +83,701 @@ pub fn parse_userinfo_tests() { ), )) }), + it("percent encoding", fn() { + uri.parse("user:%E5%90%88@%E6%B0%97%E9%81%93") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("user"), + path: "%E5%90%88@%E6%B0%97%E9%81%93", + ), + )) + uri.parse("%E5%90%88%E6%B0%97%E9%81%93@") + |> should.equal(Ok( + Uri(..uri.empty_uri, path: "%E5%90%88%E6%B0%97%E9%81%93@"), + )) + uri.parse("/%E5%90%88%E6%B0%97%E9%81%93@") + |> should.equal(Ok( + Uri(..uri.empty_uri, path: "/%E5%90%88%E6%B0%97%E9%81%93@"), + )) + uri.parse("%E5%90%88@%E6%B0%97%E9%81%93") + |> should.equal(Ok( + Uri(..uri.empty_uri, path: "%E5%90%88@%E6%B0%97%E9%81%93"), + )) + uri.parse("//%E5%90%88@%E6%B0%97%E9%81%93") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + host: Some("%E6%B0%97%E9%81%93"), + userinfo: Some("%E5%90%88"), + ), + )) + uri.parse("//%E5%90%88:%E6%B0%97@%E9%81%93") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + host: Some("%E9%81%93"), + userinfo: Some("%E5%90%88:%E6%B0%97"), + ), + )) + uri.parse("foo:/%E5%90%88%E6%B0%97%E9%81%93@") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("foo"), + path: "/%E5%90%88%E6%B0%97%E9%81%93@", + ), + )) + uri.parse("foo://%E5%90%88@%E6%B0%97%E9%81%93") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("foo"), + userinfo: Some("%E5%90%88"), + host: Some("%E6%B0%97%E9%81%93"), + ), + )) + uri.parse("foo://%E5%90%88:%E6%B0%97@%E9%81%93") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("foo"), + userinfo: Some("%E5%90%88:%E6%B0%97"), + host: Some("%E9%81%93"), + ), + )) + uri.parse("//%E5%90%88@%E6%B0%97%E9%81%93@") |> should.be_error + uri.parse("foo://%E5%90%88@%E6%B0%97%E9%81%93@") |> should.be_error + }), + ]) +} + +pub fn parse_host_tests() { + describe("host parsing", [ + it("simple parse", fn() { + uri.parse("//hostname") + |> should.equal(Ok(Uri(..uri.empty_uri, host: Some("hostname")))) + uri.parse("foo://hostname") + |> should.equal(Ok( + Uri(..uri.empty_uri, scheme: Some("foo"), host: Some("hostname")), + )) + uri.parse("foo://user@hostname") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("foo"), + userinfo: Some("user"), + host: Some("hostname"), + ), + )) + }), + it("ipv4 parse", fn() { + uri.parse("//127.0.0.1") + |> should.equal(Ok(Uri(..uri.empty_uri, host: Some("127.0.0.1")))) + uri.parse("//127.0.0.1/over/there") + |> should.equal(Ok( + Uri(..uri.empty_uri, host: Some("127.0.0.1"), path: "/over/there"), + )) + uri.parse("//127.0.0.1?name=ferret") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + host: Some("127.0.0.1"), + query: Some("name=ferret"), + ), + )) + uri.parse("//127.0.0.1#nose") + |> should.equal(Ok( + Uri(..uri.empty_uri, host: Some("127.0.0.1"), fragment: Some("nose")), + )) + + uri.parse("//127.0.0.x") + |> should.equal(Ok(Uri(..uri.empty_uri, host: Some("127.0.0.x")))) + uri.parse("//1227.0.0.1") + |> should.equal(Ok(Uri(..uri.empty_uri, host: Some("1227.0.0.1")))) + }), + it("ipv6 parse", fn() { + uri.parse("//[::127.0.0.1]") + |> should.equal(Ok(Uri(..uri.empty_uri, host: Some("::127.0.0.1")))) + uri.parse("//[2001:0db8:0000:0000:0000:0000:1428:07ab]") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + host: Some("2001:0db8:0000:0000:0000:0000:1428:07ab"), + ), + )) + uri.parse("//[::127.0.0.1]/over/there") + |> should.equal(Ok( + Uri(..uri.empty_uri, host: Some("::127.0.0.1"), path: "/over/there"), + )) + uri.parse("//[::127.0.0.1]?name=ferret") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + host: Some("::127.0.0.1"), + query: Some("name=ferret"), + ), + )) + uri.parse("//[::127.0.0.1]#nose") + |> should.equal(Ok( + Uri(..uri.empty_uri, host: Some("::127.0.0.1"), fragment: Some("nose")), + )) + + uri.parse("//[::127.0.0.x]") |> should.be_error + uri.parse("//[::1227.0.0.1]") |> should.be_error + uri.parse("//[2001:0db8:0000:0000:0000:0000:1428:G7ab]") + |> should.be_error + }), + ]) +} + +pub fn parse_port_tests() { + describe("port parsing", [ + it("simple parse", fn() { + uri.parse("/:8042") + |> should.equal(Ok(Uri(..uri.empty_uri, path: "/:8042"))) + uri.parse("//:8042") + |> should.equal(Ok(Uri(..uri.empty_uri, host: Some(""), port: Some(8042)))) + uri.parse("//example.com:8042") + |> should.equal(Ok( + Uri(..uri.empty_uri, host: Some("example.com"), port: Some(8042)), + )) + uri.parse("foo:/:8042") + |> should.equal(Ok( + Uri(..uri.empty_uri, scheme: Some("foo"), path: "/:8042"), + )) + uri.parse("foo://:8042") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("foo"), + host: Some(""), + port: Some(8042), + ), + )) + uri.parse("foo://example.com:8042") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("foo"), + host: Some("example.com"), + port: Some(8042), + ), + )) + uri.parse(":8042") |> should.be_error + uri.parse("//:8042x") |> should.be_error + }), + it("undefined port", fn() { + uri.parse("/:") + |> should.equal(Ok(Uri(..uri.empty_uri, path: "/:"))) + uri.parse("//:") + |> should.equal(Ok(Uri(..uri.empty_uri, host: Some(""), port: None))) + uri.parse("//example.com:") + |> should.equal(Ok( + Uri(..uri.empty_uri, host: Some("example.com"), port: None), + )) + uri.parse("foo:/:") + |> should.equal(Ok(Uri(..uri.empty_uri, scheme: Some("foo"), path: "/:"))) + uri.parse("foo://:") + |> should.equal(Ok( + Uri(..uri.empty_uri, scheme: Some("foo"), host: Some(""), port: None), + )) + uri.parse("foo://example.com:") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("foo"), + host: Some("example.com"), + port: None, + ), + )) + uri.parse(":") |> should.be_error + uri.parse("//:x") |> should.be_error + }), + ]) +} + +pub fn parse_path_tests() { + describe("path parsing", [ + it("simple parse", fn() { + uri.parse("over/there") + |> should.equal(Ok(Uri(..uri.empty_uri, path: "over/there"))) + uri.parse("/over/there") + |> should.equal(Ok(Uri(..uri.empty_uri, path: "/over/there"))) + uri.parse("foo:/over/there") + |> should.equal(Ok( + Uri(..uri.empty_uri, scheme: Some("foo"), path: "/over/there"), + )) + uri.parse("foo://example.com/over/there") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("foo"), + host: Some("example.com"), + path: "/over/there", + ), + )) + uri.parse("foo://example.com:8042/over/there") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("foo"), + host: Some("example.com"), + port: Some(8042), + path: "/over/there", + ), + )) + }), + ]) +} + +pub fn parse_query_tests() { + describe("query parsing", [ + it("simple parse", fn() { + uri.parse("foo:?name=ferret") + |> should.equal(Ok( + Uri(..uri.empty_uri, scheme: Some("foo"), query: Some("name=ferret")), + )) + uri.parse("foo:over/there?name=ferret") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("foo"), + path: "over/there", + query: Some("name=ferret"), + ), + )) + uri.parse("foo:/over/there?name=ferret") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("foo"), + path: "/over/there", + query: Some("name=ferret"), + ), + )) + uri.parse("foo://example.com?name=ferret") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("foo"), + host: Some("example.com"), + query: Some("name=ferret"), + ), + )) + uri.parse("foo://example.com/?name=ferret") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("foo"), + host: Some("example.com"), + path: "/", + query: Some("name=ferret"), + ), + )) + + uri.parse("?name=ferret") + |> should.equal(Ok( + Uri(..uri.empty_uri, path: "", query: Some("name=ferret")), + )) + uri.parse("over/there?name=ferret") + |> should.equal(Ok( + Uri(..uri.empty_uri, path: "over/there", query: Some("name=ferret")), + )) + uri.parse("/?name=ferret") + |> should.equal(Ok( + Uri(..uri.empty_uri, path: "/", query: Some("name=ferret")), + )) + uri.parse("/over/there?name=ferret") + |> should.equal(Ok( + Uri(..uri.empty_uri, path: "/over/there", query: Some("name=ferret")), + )) + uri.parse("//example.com?name=ferret") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + host: Some("example.com"), + query: Some("name=ferret"), + ), + )) + uri.parse("//example.com/?name=ferret") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + host: Some("example.com"), + path: "/", + query: Some("name=ferret"), + ), + )) + }), + it("percent encoding", fn() { + uri.parse("foo://example.com/?name=%E5%90%88%E6%B0%97%E9%81%93") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("foo"), + host: Some("example.com"), + path: "/", + query: Some("name=%E5%90%88%E6%B0%97%E9%81%93"), + ), + )) + uri.parse("//example.com/?name=%E5%90%88%E6%B0%97%E9%81%93") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + host: Some("example.com"), + path: "/", + query: Some("name=%E5%90%88%E6%B0%97%E9%81%93"), + ), + )) + }), + ]) +} + +pub fn parse_fragment_tests() { + describe("fragment parsing", [ + it("simple parse", fn() { + uri.parse("foo:#nose") + |> should.equal(Ok( + Uri(..uri.empty_uri, scheme: Some("foo"), fragment: Some("nose")), + )) + uri.parse("foo:over/there#nose") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("foo"), + path: "over/there", + fragment: Some("nose"), + ), + )) + uri.parse("foo:/over/there#nose") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("foo"), + path: "/over/there", + fragment: Some("nose"), + ), + )) + uri.parse("foo://example.com#nose") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("foo"), + host: Some("example.com"), + fragment: Some("nose"), + ), + )) + uri.parse("foo://example.com/#nose") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("foo"), + host: Some("example.com"), + path: "/", + fragment: Some("nose"), + ), + )) + uri.parse("foo://example.com#nose") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("foo"), + host: Some("example.com"), + fragment: Some("nose"), + ), + )) + + uri.parse("#nose") + |> should.equal(Ok(Uri(..uri.empty_uri, fragment: Some("nose")))) + uri.parse("over/there#nose") + |> should.equal(Ok( + Uri(..uri.empty_uri, path: "over/there", fragment: Some("nose")), + )) + uri.parse("/#nose") + |> should.equal(Ok( + Uri(..uri.empty_uri, path: "/", fragment: Some("nose")), + )) + uri.parse("/over/there#nose") + |> should.equal(Ok( + Uri(..uri.empty_uri, path: "/over/there", fragment: Some("nose")), + )) + uri.parse("//example.com#nose") + |> should.equal(Ok( + Uri(..uri.empty_uri, host: Some("example.com"), fragment: Some("nose")), + )) + uri.parse("//example.com/#nose") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + host: Some("example.com"), + path: "/", + fragment: Some("nose"), + ), + )) + }), + it("percent encoding", fn() { + uri.parse("foo://example.com#%E5%90%88%E6%B0%97%E9%81%93") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("foo"), + host: Some("example.com"), + fragment: Some("%E5%90%88%E6%B0%97%E9%81%93"), + ), + )) + uri.parse("//example.com/#%E5%90%88%E6%B0%97%E9%81%93") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + host: Some("example.com"), + path: "/", + fragment: Some("%E5%90%88%E6%B0%97%E9%81%93"), + ), + )) + }), + ]) +} + +fn parse_special_tests() { + describe("special parsing", [ + it("special 1", fn() { + uri.parse("//?") + |> should.equal(Ok(Uri(..uri.empty_uri, host: Some(""), query: Some("")))) + uri.parse("//#") + |> should.equal(Ok( + Uri(..uri.empty_uri, host: Some(""), fragment: Some("")), + )) + uri.parse("//?#") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + host: Some(""), + query: Some(""), + fragment: Some(""), + ), + )) + uri.parse("foo://?") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("foo"), + host: Some(""), + query: Some(""), + ), + )) + uri.parse("foo://#") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("foo"), + host: Some(""), + fragment: Some(""), + ), + )) + uri.parse("foo://?#") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("foo"), + host: Some(""), + query: Some(""), + fragment: Some(""), + ), + )) + uri.parse("///") + |> should.equal(Ok(Uri(..uri.empty_uri, host: Some(""), path: "/"))) + uri.parse("///hostname") + |> should.equal(Ok( + Uri(..uri.empty_uri, host: Some(""), path: "/hostname"), + )) + uri.parse("///?") + |> should.equal(Ok( + Uri(..uri.empty_uri, host: Some(""), path: "/", query: Some("")), + )) + uri.parse("///#") + |> should.equal(Ok( + Uri(..uri.empty_uri, host: Some(""), path: "/", fragment: Some("")), + )) + uri.parse("///?#") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + host: Some(""), + path: "/", + query: Some(""), + fragment: Some(""), + ), + )) + uri.parse("//foo?") + |> should.equal(Ok( + Uri(..uri.empty_uri, host: Some("foo"), query: Some("")), + )) + uri.parse("//foo#") + |> should.equal(Ok( + Uri(..uri.empty_uri, host: Some("foo"), fragment: Some("")), + )) + uri.parse("//foo?#") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + host: Some("foo"), + query: Some(""), + fragment: Some(""), + ), + )) + uri.parse("//foo/") + |> should.equal(Ok(Uri(..uri.empty_uri, host: Some("foo"), path: "/"))) + uri.parse("http://foo?") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("http"), + host: Some("foo"), + query: Some(""), + ), + )) + uri.parse("http://foo#") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("http"), + host: Some("foo"), + fragment: Some(""), + ), + )) + uri.parse("http://foo?#") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("http"), + host: Some("foo"), + query: Some(""), + fragment: Some(""), + ), + )) + uri.parse("http://foo/") + |> should.equal(Ok( + Uri(..uri.empty_uri, scheme: Some("http"), host: Some("foo"), path: "/"), + )) + uri.parse("http://foo:80?") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("http"), + host: Some("foo"), + port: Some(80), + query: Some(""), + ), + )) + uri.parse("http://foo:80#") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("http"), + host: Some("foo"), + port: Some(80), + fragment: Some(""), + ), + )) + uri.parse("http://foo:80?#") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("http"), + host: Some("foo"), + port: Some(80), + query: Some(""), + fragment: Some(""), + ), + )) + uri.parse("http://foo:80/") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("http"), + host: Some("foo"), + port: Some(80), + path: "/", + ), + )) + uri.parse("?") + |> should.equal(Ok(Uri(..uri.empty_uri, path: "", query: Some("")))) + uri.parse("??") + |> should.equal(Ok(Uri(..uri.empty_uri, path: "", query: Some("?")))) + uri.parse("???") + |> should.equal(Ok(Uri(..uri.empty_uri, path: "", query: Some("??")))) + uri.parse("#") + |> should.equal(Ok(Uri(..uri.empty_uri, path: "", fragment: Some("")))) + uri.parse("##") + |> should.equal(Ok(Uri(..uri.empty_uri, path: "", fragment: Some("#")))) + uri.parse("###") + |> should.equal(Ok(Uri(..uri.empty_uri, path: "", fragment: Some("##")))) + }), + it("special 2", fn() { + uri.parse("a://:1/") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("a"), + host: Some(""), + port: Some(1), + path: "/", + ), + )) + uri.parse("a:/a/") + |> should.equal(Ok(Uri(..uri.empty_uri, scheme: Some("a"), path: "/a/"))) + uri.parse("//@") + |> should.equal(Ok( + Uri(..uri.empty_uri, host: Some(""), path: "", userinfo: Some("")), + )) + uri.parse("foo://@") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("foo"), + host: Some(""), + path: "", + userinfo: Some(""), + ), + )) + uri.parse("//@/") + |> should.equal(Ok( + Uri(..uri.empty_uri, host: Some(""), path: "/", userinfo: Some("")), + )) + uri.parse("foo://@/") + |> should.equal(Ok( + Uri( + ..uri.empty_uri, + scheme: Some("foo"), + host: Some(""), + path: "/", + userinfo: Some(""), + ), + )) + uri.parse("//localhost:/") + |> should.equal(Ok( + Uri(..uri.empty_uri, host: Some("localhost"), path: "/"), + )) + uri.parse("//:") + |> should.equal(Ok( + Uri(..uri.empty_uri, host: Some("localhost"), path: ""), + )) + }), + ]) +} + +pub fn parse_failure_tests() { + describe("fail parsing", [ + it("failure", fn() { + uri.parse("å") |> should.be_error + uri.parse("aå:/foo") |> should.be_error + uri.parse("foo://usär@host") |> should.be_error + uri.parse("//host/path?foö=bar") |> should.be_error + uri.parse("//host/path#foö") |> should.be_error + uri.parse("//[:::127.0.0.1]") |> should.be_error + uri.parse("//localhost:A8") |> should.be_error + }), ]) } // gleeunit test functions end in `_test`