在API的RSpec测试中PUT方法返回405



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

但当我通过RSpec测试相同的API调用时,我会返回405。响应标头如下所示:

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 的代码片段

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测试中崩溃了吗。

感谢

请检查:

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

最新更新