diff --git a/smithy4s-fetch.scala b/smithy4s-fetch.scala index 8362b43..c4be626 100644 --- a/smithy4s-fetch.scala +++ b/smithy4s-fetch.scala @@ -137,7 +137,7 @@ private[smithy4s_fetch] case class SimpleRestJsonCodecs( def fromSmithy4sHttpUri(uri: smithy4s.http.HttpUri): String = { val qp = uri.queryParams - val newValue = { + val protocol = { uri.scheme match case Http => "http" case Https => "https" @@ -167,7 +167,7 @@ private[smithy4s_fetch] case class SimpleRestJsonCodecs( b - s"$newValue://$hostName$port$path$query" + s"$protocol://$hostName$port$path$query" } def toSmithy4sHttpResponse( @@ -233,7 +233,14 @@ private[smithy4s_fetch] case class SimpleRestJsonCodecs( uriScheme, uri.host, uri.port.toIntOption, - uri.pathname.split("/"), + uri.pathname + // drop the guaranteed leading slash, so that we don't produce an empty segment for it + .tail + // splitting an empty path would produce a single element, so we special-case to empty + .match { + case "" => IndexedSeq.empty + case other => other.split("/") + }, uri.searchParams .entries() .toIterator diff --git a/smithy4s-fetch.test.scala b/smithy4s-fetch.test.scala index 0e217f3..18d036d 100644 --- a/smithy4s-fetch.test.scala +++ b/smithy4s-fetch.test.scala @@ -24,6 +24,7 @@ import weaver.{FunSuiteIO, IOSuite} import scala.concurrent.duration.* import scala.scalajs.js.Promise import smithy4s.http.HttpUri +import org.scalajs.dom.URL object UnitTest extends FunSuiteIO: val uri = @@ -73,6 +74,44 @@ object UnitTest extends FunSuiteIO: "https://localhost:9999/1/2/3" ) + test("Base URI with no path prefix"): + val result = smithy4s_fetch.SimpleRestJsonCodecs + .toSmithy4sHttpUri(new URL("http://localhost")) + .path + + expect(result.isEmpty) + + test("Base URI with no path prefix (with slash)"): + val result = smithy4s_fetch.SimpleRestJsonCodecs + .toSmithy4sHttpUri(new URL("http://localhost/")) + .path + + expect(result.isEmpty) + + test("Base URI with path prefix"): + expect.same( + smithy4s_fetch.SimpleRestJsonCodecs + .toSmithy4sHttpUri(new URL("http://localhost/prefix")) + .path, + IndexedSeq("prefix") + ) + + test("Base URI with no path prefix, including empty segments"): + expect.same( + smithy4s_fetch.SimpleRestJsonCodecs + .toSmithy4sHttpUri(new URL("http://localhost/foo//bar//baz/")) + .path, + IndexedSeq("foo", "", "bar", "", "baz") + ) + + test("Base URI with path prefix, trailing slash doesn't matter"): + expect.same( + smithy4s_fetch.SimpleRestJsonCodecs + .toSmithy4sHttpUri(new URL("http://localhost/foo/")), + smithy4s_fetch.SimpleRestJsonCodecs + .toSmithy4sHttpUri(new URL("http://localhost/foo")) + ) + @annotation.experimental object IntegrationTest extends IOSuite: val service = API.service[IOService]