0

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

kobra
  • 4,853
  • 10
  • 32
  • 33
  • 1
    you can use browser's Network view (say, in Chrome) to see how the request from the browser is different from your test's request. – akonsu Aug 09 '13 at 03:50
  • 405 is method not available, and `PUT` is not listed in the response. What does the `Grape` resource definition look like? How are you making the call from the browser (most browsers do not support PUT directly, so are you using a javascript console or framework e.g. jQuery - if so what is the test command that works?) – Neil Slater Aug 09 '13 at 08:48
  • Thanks Neil. I have added code for my update method, and I am using Postman (Google Chrome plugin) to make PUT request (https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=en). – kobra Aug 11 '13 at 23:11

1 Answers1

0

please check:

use Goliath::Rack::Validation::RequestMethod, %w(GET POST PUT DELETE)
Achrome
  • 7,773
  • 14
  • 36
  • 45
blackanger
  • 76
  • 4