Skip to content

Commit aa8c6a6

Browse files
authored
Support path prefixes (#3)
1 parent dd60dd7 commit aa8c6a6

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

smithy4s-fetch.scala

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ private[smithy4s_fetch] case class SimpleRestJsonCodecs(
137137

138138
def fromSmithy4sHttpUri(uri: smithy4s.http.HttpUri): String = {
139139
val qp = uri.queryParams
140-
val newValue = {
140+
val protocol = {
141141
uri.scheme match
142142
case Http => "http"
143143
case Https => "https"
@@ -167,7 +167,7 @@ private[smithy4s_fetch] case class SimpleRestJsonCodecs(
167167

168168
b
169169

170-
s"$newValue://$hostName$port$path$query"
170+
s"$protocol://$hostName$port$path$query"
171171
}
172172

173173
def toSmithy4sHttpResponse(
@@ -233,7 +233,14 @@ private[smithy4s_fetch] case class SimpleRestJsonCodecs(
233233
uriScheme,
234234
uri.host,
235235
uri.port.toIntOption,
236-
uri.pathname.split("/"),
236+
uri.pathname
237+
// drop the guaranteed leading slash, so that we don't produce an empty segment for it
238+
.tail
239+
// splitting an empty path would produce a single element, so we special-case to empty
240+
.match {
241+
case "" => IndexedSeq.empty
242+
case other => other.split("/")
243+
},
237244
uri.searchParams
238245
.entries()
239246
.toIterator

smithy4s-fetch.test.scala

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import weaver.{FunSuiteIO, IOSuite}
2424
import scala.concurrent.duration.*
2525
import scala.scalajs.js.Promise
2626
import smithy4s.http.HttpUri
27+
import org.scalajs.dom.URL
2728

2829
object UnitTest extends FunSuiteIO:
2930
val uri =
@@ -73,6 +74,44 @@ object UnitTest extends FunSuiteIO:
7374
"https://localhost:9999/1/2/3"
7475
)
7576

77+
test("Base URI with no path prefix"):
78+
val result = smithy4s_fetch.SimpleRestJsonCodecs
79+
.toSmithy4sHttpUri(new URL("http://localhost"))
80+
.path
81+
82+
expect(result.isEmpty)
83+
84+
test("Base URI with no path prefix (with slash)"):
85+
val result = smithy4s_fetch.SimpleRestJsonCodecs
86+
.toSmithy4sHttpUri(new URL("http://localhost/"))
87+
.path
88+
89+
expect(result.isEmpty)
90+
91+
test("Base URI with path prefix"):
92+
expect.same(
93+
smithy4s_fetch.SimpleRestJsonCodecs
94+
.toSmithy4sHttpUri(new URL("http://localhost/prefix"))
95+
.path,
96+
IndexedSeq("prefix")
97+
)
98+
99+
test("Base URI with no path prefix, including empty segments"):
100+
expect.same(
101+
smithy4s_fetch.SimpleRestJsonCodecs
102+
.toSmithy4sHttpUri(new URL("http://localhost/foo//bar//baz/"))
103+
.path,
104+
IndexedSeq("foo", "", "bar", "", "baz")
105+
)
106+
107+
test("Base URI with path prefix, trailing slash doesn't matter"):
108+
expect.same(
109+
smithy4s_fetch.SimpleRestJsonCodecs
110+
.toSmithy4sHttpUri(new URL("http://localhost/foo/")),
111+
smithy4s_fetch.SimpleRestJsonCodecs
112+
.toSmithy4sHttpUri(new URL("http://localhost/foo"))
113+
)
114+
76115
@annotation.experimental
77116
object IntegrationTest extends IOSuite:
78117
val service = API.service[IOService]

0 commit comments

Comments
 (0)