Created
August 24, 2019 20:47
-
-
Save jsam/bae2e6de3241256bc74e7fb9e5ebd3f8 to your computer and use it in GitHub Desktop.
http4s file upload test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.renku.http | |
import java.io.File | |
import java.net.URL | |
import cats.syntax.functor._ | |
import cats.implicits._ | |
import cats.effect.{ContextShift, ExitCode, IO, IOApp, Sync} | |
import org.http4s.client.dsl.Http4sClientDsl | |
import org.http4s.headers._ | |
import org.http4s.multipart.{Multipart, Part} | |
import org.http4s.{Method, Response, _} | |
import org.scalatest.FlatSpec | |
import scala.concurrent.ExecutionContext.global | |
class UploadServiceSpec extends FlatSpec with Http4sClientDsl[IO] { | |
implicit val contextShift: ContextShift[IO] = IO.contextShift(global) | |
val res = IO(getClass.getResource("/loremipsum.txt")) | |
def multipart(url: URL): Multipart[IO] = Multipart[IO](Vector( | |
Part.formData("name", "test"), | |
Part.fileData( | |
"filename", | |
url, | |
global, | |
`Content-Type`(MediaType.multipart.`form-data`) | |
) | |
)) | |
it should "check file uploading" in { | |
val request = for { | |
body <- res.map(multipart) | |
req <- Method.POST(body, uri"/upload") | |
} yield req.withHeaders(body.headers) | |
val query: Option[Response[IO]] = UploadService.routes | |
.run(request.unsafeRunSync()) | |
.value | |
.unsafeRunSync() | |
query match { | |
case Some(response) => | |
assert(response.status == org.http4s.Status.Ok) | |
val uploadedFile = new File("/tmp/renku-core/loremipsum.txt") | |
assert(uploadedFile.exists) | |
assert(uploadedFile.isFile) | |
assert(uploadedFile.length > 0) | |
uploadedFile.delete() | |
case None => fail("no reply") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment