2

我在 akka-http 应用程序中有一条路线,它通过Http().cachedHostConnectionPoolHttps. 我想以正确的方式对其进行测试。但不确定应该如何:(

这是这条路线的样子:

val routes: Route = pathPrefix("access-tokens") {
  pathPrefix(Segment) { userId =>
    parameters('refreshToken) { refreshToken =>
      onSuccess(accessTokenActor ? GetAccessToken(userId, refreshToken)) {
        case token: AccessToken => complete(ok(token.toJson))
        case AccessTokenError => complete(internalServerError("There was problems while retriving the access token"))
      }
    }
  }
}

这条路线的背后隐藏accessTokenActor了所有逻辑发生的地方,这里是:

class AccessTokenActor extends Actor with ActorLogging with APIConfig {

  implicit val actorSystem = context.system
  import context.dispatcher
  implicit val materializer = ActorMaterializer()

  import AccessTokenActor._

  val connectionFlow = Http().cachedHostConnectionPoolHttps[String]("www.service.token.provider.com")

  override def receive: Receive = {
    case get: GetAccessToken => {
      val senderActor = sender()
        Source.fromFuture(Future.successful(
          HttpRequest(
            HttpMethods.GET,
            "/oauth2/token",
            Nil,
            FormData(Map(
              "clientId" -> youtubeClientId,"clientSecret" -> youtubeSecret,"refreshToken" -> get.refreshToken))
              .toEntity(HttpCharsets.`UTF-8`)) -> get.channelId
          )
        )
        .via(connectionFlow)
        .map {
          case (Success(resp), id) => resp.status match {
            case StatusCodes.OK => Unmarshal(resp.entity).to[AccessTokenModel]
              .map(senderActor ! AccessToken(_.access_token))
            case _ => senderActor ! AccessTokenError
          }
          case _ => senderActor ! AccessTokenError
        }
    }.runWith(Sink.head)
    case _ => log.info("Unknown message")
  }

  }

所以问题是如何更好地测试这条路线,记住带有流的演员也存在于它的引擎盖下。

4

1 回答 1

4

作品

按照目前的组织方式,测试路由逻辑的一个困难是很难隔离功能。没有 Route就不可能测试你的Route逻辑Actor,没有 Route 就很难测试你的 Actor 查询。

我认为函数组合会更好地为您服务,这样您就可以隔离您要测试的内容。

首先抽象出Actor查询(询问):

sealed trait TokenResponse
case class AccessToken() extends TokenResponse {...} 
case object AccessTokenError extends TokenResponse

val queryActorForToken : (ActorRef) => (GetAccessToken) => Future[TokenResponse] = 
  (ref) => (getAccessToken) => (ref ? getAccessToken).mapTo[TokenResponse]

现在将您的routes值转换为将查询函数作为参数的高阶方法:

val actorRef : ActorRef = ??? //not shown in question

type TokenQuery = GetAccessToken => Future[TokenResponse]

val actorTokenQuery : TokenQuery = queryActorForToken(actorRef)

val errorMsg = "There was problems while retriving the access token"

def createRoute(getToken : TokenQuery = actorTokenQuery) : Route = 
  pathPrefix("access-tokens") {
    pathPrefix(Segment) { userId =>
      parameters('refreshToken) { refreshToken =>
        onSuccess(getToken(GetAccessToken(userId, refreshToken))) {
          case token: AccessToken => complete(ok(token.toJson))
          case AccessTokenError   => complete(internalServerError(errorMsg))
        }
      }
    }
  }

//original routes
val routes = createRoute()

测试

现在您可以queryActorForToken在不需要 a 的情况下进行测试,并且您可以在不需要演员 的情况下Route测试该方法!createRoute

您可以使用始终返回预定义令牌的注入函数来测试 createRoute:

val testToken : AccessToken = ???

val alwaysSuccceedsRoute = createRoute(_ => Success(testToken))

Get("/access-tokens/fooUser?refreshToken=bar" ~> alwaysSucceedsRoute ~> check {
  status shouldEqual StatusCodes.Ok
  responseAs[String] shouldEqual testToken.toJson
}

或者,您可以使用从不返回令牌的注入函数来测试 createRoute:

val alwaysFailsRoute = createRoute(_ => Success(AccessTokenError))

Get("/access-tokens/fooUser?refreshToken=bar" ~> alwaysFailsRoute ~> check {
  status shouldEqual StatusCodes.InternalServerError
  responseAs[String] shouldEqual errorMsg
}
于 2017-06-20T17:48:20.570 回答