空手道框架 - 使用变量作为键设置标头

问题描述 投票:0回答:1

我有一个相当简单的功能文件,它为 HTTP 调用设置身份验证标头,但是我希望标头的名称(即密钥)可以根据环境进行配置。

我的配置如下:

    var config = { 
        apiKeyHeader: 'x-first-key',
        apiKey: 'someValue';
    }

    if (env == 'test') { 
        config.apiKeyHeader = 'x-other-key';
    }

然后在功能文件中:

    Scenario: call with header key
      * print apiKeyHeader
      Given path '/myApi'
      And header apiKeyHeader = apiKey
      When method get
      Then status 200 

测试的输出是这样的:

    14:00:00.123 [main] INFO com.intuit.karate - [print] x-first-key
    1 > GET http://localhost:8080/myApi
    1 > apiKeyHeader:  someValue

变量设置正确,但是在标头中使用文字字符串

apiKeyHeader
设置。我怎样才能让它使用变量的值?

karate
1个回答
0
投票

使用

headers
并且有多种方法可以 为 JSON 变量设置键

* def headerKey: 'x-first-key',
* def headerData = {}
* headerData[headerKey] = 'someValue'
# url, etc
* headers headerData
© www.soinside.com 2019 - 2024. All rights reserved.