【问题标题】:Play framework check whether a key exists播放框架检查密钥是否存在
【发布时间】:2019-08-02 15:25:29
【问题描述】:

我使用 Play Framework 来简化 Scala 中的 JSON 解析。我的代码如下:

def getAuthToken(userid: String, password: String): String = {
    val body: JsValue = Json.obj("userid" -> userid , "password" -> password)

    val res = Json.parse(Http("https://<some url>")
        .postData(body.toString()).asString.body)

    res("token").toString()
}

现在,如果我提供正确的用户 ID 和密码,我肯定会在我的 res 对象中得到一个 token。所以我在 Scala 中编写了一个测试用例,目前看起来像这样,我没有传递有效的用户名和密码:

class ApiCheckerTest extends FunSuite {
    test("ApiChecker.getAuthToken") {
        assert(ApiChecker.getAuthToken("" , "") == null)
    }
}

当我尝试运行它时,我得到了这个异常:

token
java.util.NoSuchElementException: token
    at play.api.libs.json.JsLookup$.apply$extension1(JsLookup.scala:68)
    at ApiChecker$.getAuthToken(ApiChecker.scala:15)
    at ApiCheckerTest$$anonfun$1.apply(ApiCheckerTest.scala:5)
    at ApiCheckerTest$$anonfun$1.apply(ApiCheckerTest.scala:5)
    at org.scalatest.OutcomeOf$class.outcomeOf(OutcomeOf.scala:85)
    at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
    at org.scalatest.Transformer.apply(Transformer.scala:22)
    at org.scalatest.Transformer.apply(Transformer.scala:20)
    at org.scalatest.FunSuiteLike$$anon$1.apply(FunSuiteLike.scala:186)
    at org.scalatest.TestSuite$class.withFixture(TestSuite.scala:196)
    at org.scalatest.FunSuite.withFixture(FunSuite.scala:1560)
    at org.scalatest.FunSuiteLike$class.invokeWithFixture$1(FunSuiteLike.scala:183)
    ...

ApiChecker$.getAuthToken(ApiChecker.scala:15)getAuthToken 函数的最后一行。

如何测试token 中是否存在res

【问题讨论】:

    标签: scala playframework play-framework-2.7


    【解决方案1】:

    res("token")res.apply("token") 相同

    您有一个res,其类型为JsValue。而JsValue 有一个apply 方法,它接受String 并返回另一个JsValue。现在这个apply方法的问题是它也可以抛出异常,这里看看方法

    /**
       * Access a value of this array.
       *
       * @param fieldName Element index.
       */
      def apply(fieldName: String): JsValue = result match {
        case JsDefined(x) => x match {
          case arr: JsObject => arr.value.lift(fieldName) match {
            case Some(x) => x
            case None => throw new NoSuchElementException(String.valueOf(fieldName))
          }
          case _ =>
            throw new Exception(x + " is not a JsObject")
        }
        case x: JsUndefined =>
          throw new Exception(String.valueOf(x.error))
      }
    

    但是你写的函数,只返回一个字符串,让异常冒出来。

    def getAuthToken(userid: String, password: String): String
    

    所以有两种前进的方式,要么你改变返回类型也表明你的函数可以出错

    def getAuthToken(userid: String, password: String): Try[String] = {
        val body: JsValue = Json.obj("userid" -> userid , "password" -> password)
    
        val res = Json.parse(Http("https://<some url>")
            .postData(body.toString()).asString.body)
    
        Try(res("token").toString())
    }
    

    或者,保持相同的设置并仅在 try catch 块内调用您的方法。

    我会说使用Try[String] 返回类型,或者如果您只关心令牌是否存在,请使用Option[String] 返回类型。这是一个示例

    def getAuthToken(userid: String, password: String): Option[String] = {
        val body: JsValue = Json.obj("userid" -> userid , "password" -> password)
    
        val res = Json.parse(Http("https://<some url>")
            .postData(body.toString()).asString.body)
    
        Try(res("token").toString()).toOption
    }
    

    这使您的代码更安全且更易于测试,现在您只需检查结果是 None 而不是 Some(token)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-07
      • 2014-09-14
      • 1970-01-01
      • 2011-06-05
      • 2013-07-03
      • 2017-12-26
      相关资源
      最近更新 更多