feat: Origin function and tests

This commit is contained in:
2025-09-08 15:05:54 +01:00
parent 62943e11e7
commit 38e2012945
3 changed files with 75 additions and 2 deletions

View File

@@ -97,7 +97,7 @@ pub fn normalise(uri: Uri) -> Uri {
Uri(scheme, userinfo, host, port, path, query, fragment) Uri(scheme, userinfo, host, port, path, query, fragment)
} }
fn scheme_normalisation( pub fn scheme_normalisation(
port: Option(Int), port: Option(Int),
scheme: Option(String), scheme: Option(String),
) -> Option(Int) { ) -> Option(Int) {

View File

@@ -119,5 +119,16 @@ pub fn parse_query(query: String) -> Result(List(#(String, String)), Nil) {
} }
pub fn origin(uri: Uri) -> Result(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)
}
} }

View File

@@ -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` // gleeunit test functions end in `_test`
// pub fn uri_test() { // pub fn uri_test() {
// match("uri:") // match("uri:")