在 API 的 RSpec 测试中,PUT 方法返回 405 [英] PUT method return 405 in RSpec test for API

查看:33
本文介绍了在 API 的 RSpec 测试中,PUT 方法返回 405的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Goliath-Grape 并使用 RSpec for TDD 构建 RESTful Web 服务器.当进行 API PUT 调用 (/api/v1/users/:id) 以从浏览器更新现有记录时,我得到预期的 204 响应.

I am building RESTful web server using Goliath-Grape and using RSpec for TDD. When make the API PUT call (/api/v1/users/:id) to update an existing record from the browser I get the expected 204 response.

但是当我通过 RSpec 测试相同的 API 调用时,我得到了 405.响应标头如下所示:

But when I test the same API call through RSpec I get back 405. And the response header looks like this:

PUT /api/v1/users/:id
{"ALLOW"=>"OPTIONS, POST, GET, HEAD", "CONTENT_TYPE"=>"text/plain", "CONTENT_LENGTH"=>"0", "SERVER"=>"Goliath", "DATE"=>"Fri, 09 Aug 2013 01:37:09 GMT"}

来自 api_spec.rb 的代码片段

Code snippet from api_spec.rb

describe "PUT /api/v1/users/:id" do
  it "update a user and return 204" do
    with_api(Application, api_options) do
      put_request(:path => "/api/v1/users/#{user_id}", :body => '{"user": {"email": "test_again@example.com"}', :head => {'Content-Type' => 'application/json'}) do |c|
        # .....
      end
    end
  end
end

更新方法的代码片段:

# Update
put "/:id" do
  if User.where(_id: params['id']).exists?
    User.where(_id: params['id']).update(params['user'])
    status(204)
  else
    error! "Bad Request", 400
  end
end

知道为什么它在 RSpec 测试中被破坏了.

Any idea why its breaking in RSpec test.

谢谢

推荐答案

请检查:

use Goliath::Rack::Validation::RequestMethod, %w(GET POST PUT DELETE)

这篇关于在 API 的 RSpec 测试中,PUT 方法返回 405的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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