【问题标题】:Convert the following CURL PUT to test the JSON request in rails转换以下 CURL PUT 以在 rails 中测试 JSON 请求
【发布时间】:2015-01-22 12:33:03
【问题描述】:

如何将以下 CURL PUT 请求转换为 rspec 并测试 JSON 解析器的行为。

curl -D - -X PUT --data "not json" -H "Content-Type: application/json" http://localhost:3000

以及如何检查响应?提前谢谢你。

【问题讨论】:

    标签: ruby-on-rails json rspec


    【解决方案1】:

    最好用vcr gem https://github.com/vcr/vcr

    只需像 https://github.com/vcr/vcr#usage 一样创建脚本,但需要正确的 HTTP 请求。

     VCR.use_cassette("json_test") do
       req = Net::HTTP::Put.new('http://localhost:3000', 'not json')
       req.content_type = 'application/json'
       res = Net::HTTP.get_response(req)
     end
    

    这个真实的 HTTP 请求将被记录一次,并在 rspec 中使用本地文件。

    【讨论】:

      【解决方案2】:

      您可以使用-i 选项:

      -i, --include

      (HTTP) 在输出中包含 HTTP 标头。 HTTP 标头包括服务器名称、文档日期、HTTP 版本等内容...

      来自http://curl.haxx.se/docs/manpage.html

      【讨论】:

        猜你喜欢
        • 2017-08-03
        • 1970-01-01
        • 1970-01-01
        • 2021-12-05
        • 1970-01-01
        • 1970-01-01
        • 2014-05-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多