scalaakkaakka-streamakka-httpakka-testkit

How to test akka-http route to handle exception?


I have a simple akka-http application to upload files in chunks to the server. It has two routes, the first / opens a HTML form to search the file, and the second route links the upload button to the logic that divide the file in chunks and upload it. Then I constructed an unit test (a.k.a. Spec) to test these two routes. The first is very simple, it just identify that I can open the web page. but the second I would like to test:

  1. if there is no file on the form I should identify the exception on the Spec
  2. if there is a file on the form I check the logic and test if it really uploads the file.

So, this is my akka-http application:

import akka.Done
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{ContentTypes, HttpEntity, Multipart}
import akka.http.scaladsl.server.Directives._
import akka.stream.ThrottleMode
import akka.stream.scaladsl.{FileIO, Sink, Source}
import akka.util.ByteString

import java.io.File
import scala.concurrent.Future
import scala.concurrent.duration._
import scala.util.{Failure, Success}

object UploadingFiles {
  implicit val system = ActorSystem("UploadingFiles")
  val NO_OF_MESSAGES = 1
  val filesRoutes = {
    (pathEndOrSingleSlash & get) {
      complete(
        HttpEntity(
          ContentTypes.`text/html(UTF-8)`,
          """
            |<html>
            |  <body>
            |    <form action="http://localhost:8080/upload" method="post" enctype="multipart/form-data">
            |      <input type="file" name="myFile" multiple>
            |      <button type="submit">Upload</button>
            |    </form>
            |  </body>
            |</html>
          """.stripMargin
        )
      )
    } ~ (path("upload") & post & extractLog) { log =>
      // handling uploading files using multipart/form-data
      entity(as[Multipart.FormData]) { formData =>
        // handle file payload
        val partsSource: Source[Multipart.FormData.BodyPart, Any] = formData.parts
        val filePartsSink: Sink[Multipart.FormData.BodyPart, Future[Done]] =
          Sink.foreach[Multipart.FormData.BodyPart] { bodyPart =>
            if (bodyPart.name == "myFile") {
              // create a file
              val filename = "download/" + bodyPart.filename.getOrElse("tempFile_" + System.currentTimeMillis())
              val file = new File(filename)

              log.info(s"writing to file: $filename")
              val fileContentsSource: Source[ByteString, _] = bodyPart.entity.dataBytes
              val fileContentsSink: Sink[ByteString, _] = FileIO.toPath(file.toPath)

              val publishRate = NO_OF_MESSAGES / 1
              // writing the data to the file using akka-stream graph
              fileContentsSource
                .throttle(publishRate, 2 seconds, publishRate, ThrottleMode.shaping)
                .runWith(fileContentsSink)
            }
          }
        val writeOperationFuture = partsSource.runWith(filePartsSink)
        onComplete(writeOperationFuture) {
          case Success(value) => complete("file uploaded =)")
          case Failure(exception) => complete(s"file failed to upload: $exception")
        }
      }
    }
  }

  def main(args: Array[String]): Unit = {
    println("access the browser at: localhost:8080")
    Http().newServerAt("localhost", 8080).bindFlow(filesRoutes)
  }
}

I would like to catch the string with an exception s"file failed to upload: $exception" on the akka-http application in my Spec:

import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.testkit.{RouteTestTimeout, ScalatestRouteTest}
import org.github.felipegutierrez.explore.akka.classic.http.server.highlevel.UploadingFiles.filesRoutes
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

import scala.concurrent.duration._

class UploadingFilesSpec extends AnyWordSpec
  with Matchers
  with ScalatestRouteTest {

  implicit val timeout: RouteTestTimeout = RouteTestTimeout(2 seconds)

  "A basic GET request to open the html form" should {
    "return OK [200]" in {
      Get("/") ~> filesRoutes ~> check {
        status shouldBe StatusCodes.OK
      }
    }
  }
  "A POST request upload without a file" should {
    "return NOT OK" in {
      Post("/upload") ~> filesRoutes ~> check {
        handled should ===(false) // THIS WORKS

        // THIS DOES NOT WORK
        rejection should ===(UnsupportedRequestContentTypeRejection(Set(ContentTypes.`text/plain(UTF-8)`)))
        println(rejections)
        rejections.foreach { e: Rejection =>
          println(e.toString)
          println(e.asInstanceOf[UnsupportedRequestContentTypeRejection].contentType)
        }
        // THIS DOES NOT WORK
        rejection should ===(
          UnsupportedRequestContentTypeRejection(
            Set(ContentTypes.`text/plain(UTF-8)`),
            Some(ContentTypes.`text/plain(UTF-8)`)
          )
        )
      }
    }
  }
}

But it seems that the test kit is testing the whole content of the exception, and I want to make it generic for any kind of exception. Here is the error:

akka.http.scaladsl.server.UnsupportedRequestContentTypeRejection@bdc8014 did not equal akka.http.scaladsl.server.UnsupportedRequestContentTypeRejection@1f443fae
ScalaTestFailureLocation: org.github.felipegutierrez.explore.akka.classic.http.server.highlevel.UploadingFilesSpec at (UploadingFilesSpec.scala:30)
Expected :akka.http.scaladsl.server.UnsupportedRequestContentTypeRejection@1f443fae
Actual   :akka.http.scaladsl.server.UnsupportedRequestContentTypeRejection@bdc8014

Solution

  • Please note that Multipart.FormData, as you expect in your route, has the following media type:

    def mediaType = MediaTypes.`multipart/form-data`
    

    The way to create ContentTypeRange from it, is:

    ContentTypeRange(MediaRange.apply(MediaTypes.`multipart/form-data`))
    

    and it has no content type.

    Therefore, the following should work:

    "A POST request upload without a file" should {
      "return NOT OK" in {
        Post("/upload") ~> filesRoutes ~> check {
          handled should ===(false) // THIS WORKS
    
          rejection should ===(UnsupportedRequestContentTypeRejection(
            Set(ContentTypeRange(MediaRange.apply(MediaTypes.`multipart/form-data`))), 
            Some(ContentTypes.NoContentType))
          )
        }
      }
    }