首先,感谢构建空手道,它对于测试 API 和 UI 非常有用。我们正在使用它来测试许多端点,但我们想知道是否有一种方法或哪种方法是处理带有签名作为标头中请求的一部分的请求的最佳方法。
在我们的例子中,我们有两个标头:
-
ApiKey:这个值总是相同的
-
签名:该值取决于请求体内容
有没有办法在请求执行之前根据请求正文内容注入签名值?
在这里您可以看到请求的两个示例
样本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
样本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
Karate 有一个用于生成标头的“钩子”,但截至目前它还不“知道”当前构建的请求正文 + 标头:https://github.com/intuit/karate#configure-headers https://github.com/intuit/karate#configure-headers
我们在这里收到了类似的请求,并正在考虑添加此功能:如何在 Karate DSL 中进行 REST 调用之前检索原始请求内容? https://stackoverflow.com/q/64700645/143475
也许 OAuth 示例将为您提供目前案例的前进方向:https://stackoverflow.com/a/55055111/143475 https://stackoverflow.com/a/55055111/143475
请随意提出增强请求,我们可以将其纳入下一版本(在您的帮助下进行测试)。我在想——如果你能打电话怎么办karate.get('request')
来自标头 JS 函数。
但现在你需要做的就是做这样的事情:
* def body = { some: 'json' }
* karate.set('requestBody', body)
* url someUrl
* request body
* method post
并且在header.js
功能
function fn() {
var body = karate.get('requestBody');
var sign = Utils.sign(body);
return { Signature: sign };
}
编辑:这将在空手道 1.0 及以后版本中实现:https://github.com/intuit/karate/issues/1385 https://github.com/intuit/karate/issues/1385
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)