PUT method return 405 in RSpec test for API

混江龙づ霸主 提交于 2019-12-12 02:29:44

问题


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.

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"}

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

Code snippet for update method:

# 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

Any idea why its breaking in RSpec test.

Thanks


回答1:


please check:

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


来源:https://stackoverflow.com/questions/18139135/put-method-return-405-in-rspec-test-for-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!