How to handle requests with signatures on karate tests?

你离开我真会死。 提交于 2021-01-20 06:57:37

问题


First of all, thanks for build karate it's a very useful for test API's and UI's. We are using it to test a lot of our endpoints but we would like to know if there is a way or which is the best approach to handle requests with signature as part of the request in the header.

In our case we have two headers:

  • ApiKey: this value is always the same
  • Signature: this value depends on the request body content

Is there any way to inject the signature value just before the request is executed based on the request body content?

Here you can see two samples of the requests

Sample 1:

   * url 'https://dev.sample.com'
   * path '/api/user/getAll' 
   * header Content-Type = 'application/json'
   * header ApiKey = 'XXX' 
   * header Signature = 'YYY'
    And request {  }
    When method POST
    Then status 200    

Sample 2:

   * url 'https://dev.sample.com'
   * path '/api/user/getAll' 
   * header Content-Type = 'application/json'
   * header ApiKey = 'XXX' 
   * header Signature = 'ZZZ'
    And request { name: 'John' }
    When method POST
    Then status 200    

Thanks


回答1:


Karate has a "hook" for generating headers, but as of now it is not "aware" of the currently built request body + headers: https://github.com/intuit/karate#configure-headers

We got a similar request here, and are thinking of adding this capability: How to retrieve raw request contents before making a REST call in Karate DSL?

Maybe the OAuth examples will give you the way forward for your case for now: https://stackoverflow.com/a/55055111/143475

Feel free to raise an enhancement request, and we can get this in to the next version (with your help to test it). I'm thinking - what if you are able to call karate.get('request') from within the header JS function.

But for now all you need to do is do something like this:

* def body = { some: 'json' }
* karate.set('requestBody', body)
* url someUrl
* request body
* method post

And in the header.js function

function fn() {
  var body = karate.get('requestBody');
  var sign = Utils.sign(body);
  return { Signature: sign };  
}


来源:https://stackoverflow.com/questions/65018596/how-to-handle-requests-with-signatures-on-karate-tests

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!