diff options
-rw-r--r-- | src/gleam/uri.gleam | 24 | ||||
-rw-r--r-- | test/gleam/uri_test.gleam | 18 |
2 files changed, 26 insertions, 16 deletions
diff --git a/src/gleam/uri.gleam b/src/gleam/uri.gleam index 05dfb9d..508cfef 100644 --- a/src/gleam/uri.gleam +++ b/src/gleam/uri.gleam @@ -361,20 +361,18 @@ pub fn to_string(uri: Uri) -> String { /// pub fn origin(uri: Uri) -> Result(String, Nil) { let Uri(scheme: scheme, host: host, port: port, ..) = uri - case scheme { - Some("https") if port == Some(443) -> { - let origin = Uri(scheme, None, host, None, "", None, None) - Ok(to_string(origin)) - } - Some("http") if port == Some(80) -> { - let origin = Uri(scheme, None, host, None, "", None, None) - Ok(to_string(origin)) - } - Some(s) if s == "http" || s == "https" -> { - let origin = Uri(scheme, None, host, port, "", None, None) - Ok(to_string(origin)) + case host, scheme { + Some(h), Some("https") if port == Some(443) -> + Ok(string.concat(["https://", h])) + Some(h), Some("http") if port == Some(80) -> + Ok(string.concat(["http://", h])) + Some(h), Some(s) if s == "http" || s == "https" -> { + case port { + Some(p) -> Ok(string.concat([s, "://", h, ":", int.to_string(p)])) + None -> Ok(string.concat([s, "://", h])) + } } - _ -> Error(Nil) + _, _ -> Error(Nil) } } diff --git a/test/gleam/uri_test.gleam b/test/gleam/uri_test.gleam index 506c246..2d2732a 100644 --- a/test/gleam/uri_test.gleam +++ b/test/gleam/uri_test.gleam @@ -409,19 +409,19 @@ pub fn parse_segments_test() { pub fn origin1_test() { let assert Ok(parsed) = uri.parse("http://example.test/path?weebl#bob") uri.origin(parsed) - |> should.equal(Ok("http://example.test/")) + |> should.equal(Ok("http://example.test")) } pub fn origin2_test() { let assert Ok(parsed) = uri.parse("http://example.test:8080") uri.origin(parsed) - |> should.equal(Ok("http://example.test:8080/")) + |> should.equal(Ok("http://example.test:8080")) } pub fn origin3_test() { let assert Ok(parsed) = uri.parse("https://example.test") uri.origin(parsed) - |> should.equal(Ok("https://example.test/")) + |> should.equal(Ok("https://example.test")) } pub fn origin4_test() { @@ -448,6 +448,18 @@ pub fn origin7_test() { |> should.equal(Error(Nil)) } +pub fn origin8_test() { + let assert Ok(parsed) = uri.parse("https://mozilla.org:443/") + uri.origin(parsed) + |> should.equal(Ok("https://mozilla.org")) +} + +pub fn origin9_test() { + let assert Ok(parsed) = uri.parse("http://localhost:80/") + uri.origin(parsed) + |> should.equal(Ok("http://localhost")) +} + pub fn merge1_test() { let assert Ok(a) = uri.parse("/relative") let assert Ok(b) = uri.parse("") |