Skip to content

Commit

Permalink
Support path prefixes (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
kubukoz authored Jul 7, 2024
1 parent dd60dd7 commit aa8c6a6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
13 changes: 10 additions & 3 deletions smithy4s-fetch.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions smithy4s-fetch.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit aa8c6a6

Please sign in to comment.