Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion core/jvm/src/test/scala/krop/route/ResponseSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class ResponseSuite extends CatsEffectSuite {
} yield response
}

test("static file response succeeds when file exists") {
test("static file response succeeds when file exists (String path)") {
val request =
Http4sRequest(method = Method.GET, uri = uri"http://example.org/")

Expand All @@ -71,4 +71,36 @@ class ResponseSuite extends CatsEffectSuite {
.assert
} yield response
}

test("static file response succeeds when file exists (Fs2Path)") {
val request =
Http4sRequest(method = Method.GET, uri = uri"http://example.org/")

for {
builder <- WebSocketBuilder[IO]
runtime = JvmRuntime.krop(builder)
path = fs2.io.file.Path("project/plugins.sbt")
response <- Response
.staticFile(path)
.respond(request, ())(using runtime)
.map(_.status.isSuccess)
.assert
} yield response
}

test("static file response succeeds when file exists (Java Path)") {
val request =
Http4sRequest(method = Method.GET, uri = uri"http://example.org/")

for {
builder <- WebSocketBuilder[IO]
runtime = JvmRuntime.krop(builder)
javaPath = java.nio.file.Paths.get("project/plugins.sbt")
response <- Response
.staticFile(javaPath)
.respond(request, ())(using runtime)
.map(_.status.isSuccess)
.assert
} yield response
}
}
12 changes: 12 additions & 0 deletions core/shared/src/main/scala/krop/route/Response.scala
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,18 @@ object Response {
def staticFile(path: String): Response[Unit, Array[Byte]] =
Response.StaticFile(fs2.io.file.Path(path))

/** Respond with a file loaded from the filesystem. The `path` is the fs2 path
* to the file.
*/
def staticFile(path: Fs2Path): Response[Unit, Array[Byte]] =
Response.StaticFile(path)

/** Respond with a file loaded from the filesystem. The `path` is the Java NIO
* path to the file.
*/
def staticFile(path: java.nio.file.Path): Response[Unit, Array[Byte]] =
Response.StaticFile(fs2.io.file.Path(path.toString))

def badRequest[R, P](entity: Entity[R, P]): Response[R, P] =
status(Status.BadRequest, entity)

Expand Down