Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActiveResource#exists? does not work at all

#3020

ActiveResource#exists? which sends a HEAD request to find whether a resource exists, fails to work under all conditions.

Here is the problem: For HEAD requests, {nil => "application/xml"} is sent as header information to Net:HTTP, and the latter complains. This wasn't probably detected so far, as tests do not fail due to http mock.

Following is the relevant code from ActiveResource::Connection

    HTTP_FORMAT_HEADER_NAMES = {  :get => 'Accept',
      :put => 'Content-Type',
      :post => 'Content-Type',
      :delete => 'Accept'
    }

    def head(path, headers = {})
      request(:head, path, build_request_headers(headers))
    end

    def build_request_headers(headers, http_method=nil)
      authorization_header.update(default_header).update(http_format_header(http_method)).update(headers)
    end

    def http_format_header(http_method)
      {HTTP_FORMAT_HEADER_NAMES[http_method] => format.mime_type}
    end

This is fixed by making sure that there is a key for head in HTTP_FORMAT_HEADER_NAMES. I've attached a patch for Rails 2.3-stable(patch for edge Rails coming next..)

Reported by Jatinder Singh · August 9th, 2009 @ 05:27 AM

State: resolved
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Jatinder Singh
  2. Gabe da Silveira
    Gabe da Silveira
    • Tag changed from activeresource, bugmash, patch to activeresource, bugmash, patch, verified

    verified both patches apply cleanly, and test fails without the fix

    +1 on the approach of sending the Accept header. Even though the body is not sent, the server still needs to know what was requested.

    August 9th, 2009 @ 06:21 AM

  3. Elise Huard
    Elise Huard

    verified both patches, the tests fail without the fix, and code is nice and clean.
    +1 makes sense.

    August 9th, 2009 @ 11:32 AM

  4. Elad Meidar
    Elad Meidar

    +1 makes sense, both patches apply here and tests pass with the patches.

    August 9th, 2009 @ 04:01 PM

  5. Rizwan Reza
    Rizwan Reza

    verified

    +1 Both patches work correctly.

    August 9th, 2009 @ 04:53 PM

  6. Repository
  7. Repository
  8. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from activeresource, bugmash, patch, verified to activeresource, patch, verified

    August 9th, 2009 @ 10:47 PM

  9. Elliot Winkler
    Elliot Winkler

    This is actually the exact same problem I posted way back in #1223 ActiveResource 'head' request sends headers with a nil key, so I think that one can be closed.

    August 9th, 2009 @ 11:34 PM

  10. Jatinder Singh
    Jatinder Singh

    I should have been more proactive in looking for similar tickets. sorry about stealing it from you Elliot.

    August 9th, 2009 @ 11:39 PM