【问题标题】:Grails Canoo Webtest plugin: invoke() fails to send JSON data in POST requestGrails Canoo Webtest 插件:invoke() 无法在 POST 请求中发送 JSON 数据
【发布时间】:2012-10-31 09:56:15
【问题描述】:

我正在评估 Canoo Webtest 作为 Grails 插件的自动化集成/功能测试。

我有一个正在尝试测试的 REST 应用程序,但 Canoo Webtest 似乎无法在 POST 请求中正确发送 JSON 数据。我的测试代码如下

invoke( description:"Add a product to shopping cart", 
        url:'shoppingCart/add', method:'POST', 
        content:'{"class":"shop.service.Product", "name":"A product", "description":"Manufactured by X", "price":99.9}'
        //contentFile: '../product.json'
        )

无论我使用 contentFile 方法还是将数据内联为内容属性,请求正文都是空的。测试报告显示数据已正确发送,但错误页面显示错误说明“JSONException:缺少值。在 ' 的字符 0 处。作为 GET 请求响应的 JSON 数据恢复正常。

我已经用 curl 测试了相同的功能,它工作得非常好。我在 Canoo Webtest 设置中缺少什么吗?

谢谢。

编辑:我使用 Grails 1.3.7 以防万一

【问题讨论】:

    标签: webtest canoo canoo-webtest


    【解决方案1】:

    好的。我开始尝试使用 invoke() 并且似乎将属性 soapAction 值设置为 true 就可以了。

    我不知道为什么会这样。属性的documentation of invoke() 表示

    soapAction
      Required? no
      If the HTTP method is POST and is in fact a SOAP POST request, 
      this allows the SOAP Action header to be set. Ignored for GETs.
    

    显然它设置了一些需要的请求标头。没查过是哪一个。

    感谢您的阅读:)

    尚不能将此答案标记为正确答案,但会在可能的情况下这样做。

    【讨论】:

    • 我检查了标题。使用soapAction 属性时有一些区别。最重要的 content-type 设置为 text/xml; charset=UTF-8 而不是 application/x-www-form-urlencoded。 MIME 类型未设置为 JSON,但它可以工作。我想该工具会受益于明确设置内容类型的可能性。
    猜你喜欢
    • 2013-01-16
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-11
    • 1970-01-01
    相关资源
    最近更新 更多