用参数正确调用授权空手道功能 [英] Properly calling an authorization Karate feature with arguments

查看:42
本文介绍了用参数正确调用授权空手道功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有身份验证功能,但想使其可重用.我无法通过到目前为止尝试过的方法进行工作.

I have an authentication feature working, but would like to make it reusable. I'm not able to get working through the methods I've tried so far.

有效的独立方法是:

# Created at 10/4/18
Feature: #Authentication Management
Background:
  * url gatewayUrl
  * def myid = 'myid'
  * def mysecret = 'mysecret'
  Scenario: # Generate authentication token for x user
    Given path 'mypath'
    And header Content-Type = 'application/x-www-form-urlencoded'
    And request 'grant_type=api_key&myid=' + myid + '&mysecret=' + mysecret
    When method post
    Then status 200
    * print response.Token

这是该请求的有效POST请求:

Here is the working POST request for this one:

1 > POST <authURL>
1 > Accept-Encoding: gzip,deflate
1 > Connection: Keep-Alive
1 > Content-Length: 108
1 > Content-Type: application/x-www-form-urlencoded; charset=UTF-8
1 > Host: <authURL>
1 > User-Agent: Apache-HttpClient/4.5.5 (Java/10.0.2)
grant_type=api_key&myid=myId&mysecret=mySecret

我想用执行通话功能中的参数替换 myid mysecret .使用'#(myid)'和'(#mysecret)'似乎在任何地方都行不通,但在定义参数时有效.有没有办法进行这种替换,或者有另一种等效的方法来建立要发送的请求数据?

I'd like to replace myid and mysecret with the args from the feature that performs the call. Using '#(myid)' and '(#mysecret)' doesn't seem to work anywhere but when defining param(s). Is there a way to do this replacement, or another equivalent way of building up the request data to send?

谢谢

推荐答案

当然值得仔细阅读文档:

Of course it is worth reading through the docs carefully: https://github.com/intuit/karate#calling-other-feature-files

尝试一下:

Feature: #Authentication Management
Scenario: # Generate authentication token for x user
  Given url gatewayUrl      
  And path 'mypath'
  And header Content-Type = 'application/x-www-form-urlencoded'
  And request 'grant_type=api_key&myid=' + myid + '&mysecret=' + mysecret
  When method post
  Then status 200

然后调用如下:

Feature: main
Background:
  * def result = call read('auth.feature') { myid: 'actualid', mysecret: 'actualsecret' }
  * def token = result.response.Token
  # remaining test uses token

这篇关于用参数正确调用授权空手道功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆