在POST期间,请求正文中的变量替换为{}

时间:2018-06-11 18:39:40

标签: rest karate

我使用的是Karate DSL(版本0.6.1.1),我有以下情况:

  Scenario:
    Given url baseURL
    * def email = "test_email@test_domain.com"
    * def query = "SELECT * FROM public.users where username='" + email + "' ORDER BY user_id ASC"
    * def dbUrl = databaseUrl + "databaseName"
    * def config = { username: '#(databaseUsername)', password: '#(databasePassword)', url: '#(dbUrl)', driverClassName: 'org.postgresql.Driver' }
    * def DbUtils = Java.type('utility.database.DbUtils')
    * def db = new DbUtils(config)
    * def results = db.readRow(query)
    * def supplierId = results.user_id
    * print "SUPPLIERID: " + supplierId

    Given path '/path/to/endpoint'
    And header content-type = 'application/json'
    And request { supplierId: #(supplierId) }
    When method POST
    Then status 200

utility.database.DbUtils是一个自定义类,readRow()是该类的单行成员,返回JdbcTemplate.queryForMap(query)的结果。

运行此方案会提供以下日志:


    10:53:17.477 [main] INFO  com.intuit.karate - [print] SUPPLIERID: 957750e7-ee6b-486d-977c-05c8ac7bb589
    10:53:17.506 [main] INFO  com.intuit.karate - request:{
      "supplierId": 957750e7-ee6b-486d-977c-05c8ac7bb589
    }
    10:53:18.372 [main] DEBUG com.intuit.karate - 
    1 > POST http://baseurl.com/path/to/endpoint
    1 > Accept-Encoding: gzip,deflate
    1 > Connection: Keep-Alive
    1 > Content-Length: 17
    1 > Content-Type: application/json
    1 > Host: base.url.com
    1 > User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_162)
    {"supplierId":{}}

您可以看到,首先请求正文的值为supplierId 957750e7-ee6b-486d-977c-05c8ac7bb589。完成POST后,supplierId只是{}。这似乎与supplierId由数据库调用填充的事实有关。如果我用简单的* def supplierId = "957750e7-ee6b-486d-977c-05c8ac7bb589"替换对数据库的调用,请求正文将按预期显示。

为什么在进行POST调用时supplierId会被{}替换,如何阻止它进行此操作?

2 个答案:

答案 0 :(得分:1)

看起来你的嵌入式表达式语法错误了。我认为这个改变应该修复,请注意字符串引号:

And request { supplierId: '#(supplierId)' }

答案 1 :(得分:1)

看起来这是一个类型转换问题。我通过将* def supplierId = results.user_id替换为* string supplierId = results.user_id来实现它。