问题
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