From 38e201294509c2835c7e59eced6d9654d4499282 Mon Sep 17 00:00:00 2001 From: Gareth Pendleton Date: Mon, 8 Sep 2025 15:05:54 +0100 Subject: [PATCH] feat: Origin function and tests --- src/internal/utils.gleam | 2 +- src/uri.gleam | 13 ++++++++- test/uri_test.gleam | 62 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) diff --git a/src/internal/utils.gleam b/src/internal/utils.gleam index f1ec041..74031b1 100644 --- a/src/internal/utils.gleam +++ b/src/internal/utils.gleam @@ -97,7 +97,7 @@ pub fn normalise(uri: Uri) -> Uri { Uri(scheme, userinfo, host, port, path, query, fragment) } -fn scheme_normalisation( +pub fn scheme_normalisation( port: Option(Int), scheme: Option(String), ) -> Option(Int) { diff --git a/src/uri.gleam b/src/uri.gleam index a64b081..01669b9 100644 --- a/src/uri.gleam +++ b/src/uri.gleam @@ -119,5 +119,16 @@ pub fn parse_query(query: String) -> Result(List(#(String, String)), Nil) { } pub fn origin(uri: Uri) -> Result(String, Nil) { - todo + case uri.scheme, uri.host, utils.scheme_normalisation(uri.port, uri.scheme) { + Some("http" as scheme), Some(host), port + | Some("https" as scheme), Some(host), port + -> { + let port = + port + |> option.map(fn(p) { ":" <> int.to_string(p) }) + |> option.unwrap("") + Ok(scheme <> "://" <> host <> port) + } + _, _, _ -> Error(Nil) + } } diff --git a/test/uri_test.gleam b/test/uri_test.gleam index 8ac9206..38d85f1 100644 --- a/test/uri_test.gleam +++ b/test/uri_test.gleam @@ -1317,6 +1317,68 @@ pub fn query_to_string_tests() { }), ]) } + +pub fn origin_tests() { + describe("origin", [ + it("basic origin", fn() { + uri.parse("http://") + |> should.be_ok + |> uri.origin + |> should.be_ok + |> should.equal("http://") + uri.parse("http://example.com/test") + |> should.be_ok + |> uri.origin + |> should.be_ok + |> should.equal("http://example.com") + uri.parse("http://example.com:8080/test") + |> should.be_ok + |> uri.origin + |> should.be_ok + |> should.equal("http://example.com:8080") + uri.parse("http://example.com:80/test") + |> should.be_ok + |> uri.origin + |> should.be_ok + |> should.equal("http://example.com") + uri.parse("https://example.com:443/test") + |> should.be_ok + |> uri.origin + |> should.be_ok + |> should.equal("https://example.com") + uri.parse("http://example.com:443/test") + |> should.be_ok + |> uri.origin + |> should.be_ok + |> should.equal("http://example.com:443") + uri.parse("https://") + |> should.be_ok + |> uri.origin + |> should.be_ok + |> should.equal("https://") + uri.parse("http://example.com?query=no") + |> should.be_ok + |> uri.origin + |> should.be_ok + |> should.equal("http://example.com") + uri.parse("http:///nohost") + |> should.be_ok + |> uri.origin + |> should.be_ok + |> should.equal("http://") + }), + it("errors", fn() { + uri.parse("/nohost") + |> should.be_ok + |> uri.origin + |> should.be_error + uri.parse("file:///unknown/nohost") + |> should.be_ok + |> uri.origin + |> should.be_error + }), + ]) +} // gleeunit test functions end in `_test` // pub fn uri_test() { // match("uri:")