简体   繁体   English

无法识别 JS 文件中的缓冲区(使用空手道框架进行 API 测试自动化)

[英]Buffer in JS file isn't recognized (API tests automation with Karate Framework)

We're automating our test with karate framework.我们正在使用空手道框架自动化我们的测试。 In one of our features we need to decode a token and get a scope in the response.在我们的一项功能中,我们需要解码一个令牌并在响应中获得一个范围。 Everything works well, except this code in js.一切正常,除了 js 中的这段代码。

function(token) {
    return JSON.parse(new Buffer(token.split('.')[1],'base64').toString('ascii')).scope;
}

Error:错误:

Caused by: <eval>:2 ReferenceError: "Buffer" is not defined
Caused by: jdk.nashorn.internal.runtime.ECMAException

In official tutorials it is said that javascript is 'native' to karate, so we don't understand why Buffer is not recognized?在官方教程中说 javascript 是空手道的“原生”,所以我们不明白为什么 Buffer 不被识别? What we should do?我们应该怎么做? Thanks for any help谢谢你的帮助

I was able to successfully base64Decode a JWT token payload to JSON using the following code without the need for a Java method:我能够使用以下代码成功将 JWT 令牌有效负载 base64Decode 到 JSON,而无需 Java 方法:

  Background:
    * def parseJwt =
  """
  function(token) {
      var base64Url = token.split('.')[1];
      var base64Str = base64Url.replace(/-/g, '+').replace(/_/g, '/');
      var Base64 = Java.type('java.util.Base64');
      var decoded = Base64.getDecoder().decode(base64Str);
      var String = Java.type('java.lang.String')
      return new String(decoded)
  };
  """

  Scenario: JWT Token
    Given path  'jwt/authenticate'
    And header x-goog-authenticated-user-email = 'email'
    And request {}
    When method get
    Then status 200
    * json result = parseJwt(responseHeaders['Set-Cookie'][0])
    * match result == {permissions: [1,2,3], iss: "us", exp: "#number", email: "email"}

Note: It does seem to be required to use json rather than def as Karate does better if it parses the string to json itself.注意:似乎确实需要使用json而不是def因为如果将字符串解析为 json 本身,Karate 会做得更好。 Also, you may obtain the token from a header rather than a cookie as in this example if so, just change the responseHeader that you are looking for.此外,您可以像本示例中那样从标头而不是 cookie 获取令牌,如果是这样,只需更改您正在查找的 responseHeader。

I'm pretty sure that Buffer is advanced / non-standard or NodeJS so it probably is not supported by the JVM JS engine (Nashorn).我很确定Buffer是高级/非标准或 NodeJS,因此 JVM JS 引擎 (Nashorn) 可能不支持它。

Here's my recommendation.这是我的建议。 For this case, do the work using Java utilities.对于这种情况,请使用 Java 实用程序来完成这项工作。

For example, look at the Karate basic-auth example in the doc which uses Base64 encoding.例如,查看使用 Base64 编码的文档中的Karate basic-auth 示例

If it is really complex, simply create a Java static function, it will be much easier to test as a side-benefit.如果真的很复杂,只需创建一个Java静态函数,作为附带好处进行测试会容易得多。 Hope this helps !希望这可以帮助 !

With respect to the post Karate: Problem with requests using bearer token |关于职位空手道:使用不记名令牌的请求问题 | Karate is showing "org/apache/commons/codec/binary/Base64" in response instead of HTTP 401 空手道在响应中显示“org/apache/commons/codec/binary/Base64”而不是 HTTP 401

My query was marked duplicate hence posting an answer/solution which we have found in our project.我的查询被标记为重复,因此发布了我们在项目中找到的答案/解决方案。

We did some debugging in karate core about the error.我们在空手道核心中对错误进行了一些调试。 It seemed a dependency was missing.似乎缺少依赖项。

在此处输入图片说明

We added the dependency in the POM.xml我们在 POM.xml 中添加了依赖

<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.15</version>
</dependency>

Afterwards the problem was resolved and we started getting HTTP 401. I am not sure it can be added to the karate core.后来问题解决了,我们开始得到HTTP 401。我不确定它是否可以添加到空手道核心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM