aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Wilson <nijolas.wilson@gmail.com>2024-06-14 17:37:03 +1000
committerLouis Pilfold <louis@lpil.uk>2024-07-17 13:07:16 +0100
commitf31f5ef4239e4418eaa21bfc8c118b3cf40fceda (patch)
treea483ee0d8cb0ee64d032427e06782c790ec22b1e
parent2bd61406c2c215cfcbecab1a0ce20543a0eca267 (diff)
downloadgleam_stdlib-f31f5ef4239e4418eaa21bfc8c118b3cf40fceda.tar.gz
gleam_stdlib-f31f5ef4239e4418eaa21bfc8c118b3cf40fceda.zip
Correct implementation of uri.origin and add tests for default ports
-rw-r--r--src/gleam/uri.gleam24
-rw-r--r--test/gleam/uri_test.gleam18
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("")