Karate: Is there a http-request hook in karate, that gets called automatically after every API call, and whose behaviour I can modify?

一曲冷凌霜 提交于 2020-05-05 14:27:08

问题


I want a common implementation to write the: request-body, request-method & response-body to an output file for all the HTTP requests that I make in any of the karate feature files.

I have read through the documentation, and the closest option that I found was the afterScenario hook. However this doesn't help me in cases where I want to make multiple API calls in the same scenario.

I also tried using karate.prevRequest object by passing it to java function that I wrote. While this helps me capture the information that I want, I have to keep calling my java function after every API call, which I don't want to do.

I want to be able to write a hook that gets called after every API call and gives me access to request-body, request-headers, response-headers, response-body, request-method and request URL.

This should be fairly straight forward, clearly I seem to be missing something here.


回答1:


First, I personally think that this is a mis-use of Karate, and you seem to be more interested in reports than actually doing testing. Take some time to think about it. Karate has excellent report integration and you are just wasting your time doing this in my very honest, sincere opinion. No one has asked for this.

Anyway. There is a new ExecutionHook in 0.9.5.RC4, details here: https://github.com/intuit/karate/issues/970#issuecomment-557443551

You can implement afterStep, see if it is a method step and then do this. If you need changes, open an issue.

EDIT: just remembered - in the develop branch we added a way to intercept all requests and responses: https://twitter.com/karatedsl/status/1195240779213496320 - will be happy to get your feedback and see if we can merge this concept with the above hook - or improve the interfaces if needed




回答2:


The ExecutionHook supports quite a lot different use cases.

Steps can be aborted or changed

Request and Response can be sniffed

See ExecutionHookExampleTest on how to use an ExecutionHook.



来源:https://stackoverflow.com/questions/59075792/karate-is-there-a-http-request-hook-in-karate-that-gets-called-automatically-a

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