How to retrieve raw request contents before making a REST call in Karate DSL?

孤人 提交于 2021-01-28 20:19:56

问题


I am basically trying to generate AWS signature in order to use them for Authorization. We have third party java libraries (uk.co.lucasweb.aws.v4.signer) to generate this AWS signature. Using the concept of Java Interop, I would be able to call them and retrieve the signature. But I will have to pass all the request details (url, header, param) in the arguments in order to generate it. (getSignature() is a custom java method which was written using the prescribed library)

getSignature() method's implementation would look something like this

   String contentSha256 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
    HttpRequest request = new HttpRequest("GET", new URI(URL));
    String signature = Signer.builder()
            .awsCredentials(new AwsCredentials(ACCESS_KEY, SECRET_KEY))
            .header("Host", "examplebucket.s3.amazonaws.com")
            .header("x-amz-date", "20130524T000000Z")
            .header("x-amz-content-sha256", contentSha256)
            .buildS3(request, contentSha256)
            .getSignature();

Karate DSL :

* def awsUtils = Java.type('customJavaClasses.AWSUtils')
* url 'https://sit.ecom.com/atp-config-entity-dev'

Scenario: get AWS signature

 Given path '/v1/atp-config-entity/config'
 And header wtr-correlation-id = '58f3de9b-6276-4f05-a78d-0a39c6044877'
 And header customer-id = '279809907'
 And header principal-id = '279809906'
 And header X-Amz-Date = '20200325T120000Z'
 And def requestPayload = read('request.json')
 And request requestPayload 

Once the request is composed using Karate DSL, I need a way to retrieve the contents, so that I can pass them in the arguments as shown below

And def signature = awsUtils.getSignature(url, headers, params);
And header Authorization = signature
When method post
Then status 200

I am aware that we have a provision to do that after making a call using karate.prevRequest. But do we have any provision to retrieve the raw request data before we make the call. Have referred oAuth2 and signer documentation as well but couldn't find any solution


回答1:


You can see if the ExecutionHook gives you a way, else this may be a feature request: https://stackoverflow.com/a/59080128/143475

We are actually doing a refactor of the code right now - and I agree this use-case is important, so we can consider it.



来源:https://stackoverflow.com/questions/64700645/how-to-retrieve-raw-request-contents-before-making-a-rest-call-in-karate-dsl

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