Skip to content

Commit

Permalink
Merge pull request #135 from yisraelU/main
Browse files Browse the repository at this point in the history
handle docs path
  • Loading branch information
Baccata committed Mar 11, 2022
2 parents ce45b6a + 1de97d1 commit 0407bcb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,22 @@ private[smithy4s] abstract class Docs[F[_]](

val actualPath: Path = Uri.Path.unsafeFromString("/" + path)

object DocPath {
def unapply(p: Path): Boolean = {
p match {
case `actualPath` => true
case `actualPath` / "" => true
case `actualPath` / file if file.equalsIgnoreCase("index.html") => true
case _ => false
}
}
}
def routes: HttpRoutes[F] = HttpRoutes.of[F] {
case GET -> `actualPath` =>
case GET -> DocPath() =>
PermanentRedirect(
Location(Uri.unsafeFromString(s"/$path/index.html?url=/$jsonSpec"))
)

case request @ GET -> `actualPath` / filePath =>
val resource = s"$swaggerUiPath/$filePath"
staticResource(resource, Some(request)).getOrElseF(NotFound())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,25 @@ object DocsSpec extends SimpleIOSuite with TestCompat {
val app = docs(path).routes.orNotFound

test(s"GET /$path redirects to expected location") {
val request =
Request[IO](
method = Method.GET,
uri = Uri.unsafeFromString(s"/$path/index.html")
)
app.run(request).map { response =>
val redirectUri = response.headers
.get(CIString("Location"))
.map(_.head)
.map(_.value)

expect(response.status == Status.PermanentRedirect) and
expect.eql(
redirectUri,
Some(s"/$path/index.html?url=/foobar.test-spec.json")
)
}
}
test(s"GET /$path/ redirects to expected location") {
val request =
Request[IO](method = Method.GET, uri = Uri.unsafeFromString(s"/$path"))
app.run(request).map { response =>
Expand All @@ -43,7 +62,22 @@ object DocsSpec extends SimpleIOSuite with TestCompat {
)
}
}
test(s"GET /$path/index.html redirects to expected location") {
val request =
Request[IO](method = Method.GET, uri = Uri.unsafeFromString(s"/$path"))
app.run(request).map { response =>
val redirectUri = response.headers
.get(CIString("Location"))
.map(_.head)
.map(_.value)

expect(response.status == Status.PermanentRedirect) and
expect.eql(
redirectUri,
Some(s"/$path/index.html?url=/foobar.test-spec.json")
)
}
}
test(s"GET $path/test-file.json fetches requested file") {
val filePath = s"/$path/test-file.json"
val request =
Expand Down

0 comments on commit 0407bcb

Please sign in to comment.