Skip to content

Rack status code name changes breaks HttpStatusMatcher #2763

Closed
@darrenboyd

Description

@darrenboyd

What Ruby, Rails and RSpec versions are you using?

Ruby version: 3.3.3
Rails version: 7.1.3.4
RSpec version: 3.13.0
Rack version: 3.1.3

Observed behaviour

Rack has recently changed the name of the 422 status code from unprocessable_entity to unprocessable_content. However, both names will still resolve if Rack's public API is being used. However, the HttpStatusMatcher is looking at the Rack Constant directly, instead of using the provided API.

My suggestion is for the code in have_http_status.rb that looks like (around line 218)...

def set_expected_code!
  @expected ||=
    Rack::Utils::SYMBOL_TO_STATUS_CODE.fetch(expected_status) do
      raise ArgumentError,
            "Invalid HTTP status: #{expected_status.inspect}"
    end
end

be changed to....

def set_expected_code!
  @expected ||= Rack::Utils.status_code(expected_status)
end

Here's the behavior of the Rack::Utils.status_code method...

irb> Rack::Utils.status_code(:unprocessable_content)
  => 422
irb> Rack::Utils.status_code(:unprocessable_entity)
  => 422
irb> Rack::Utils.status_code(:invalidcode)
(irb):4:in `<main>': Unrecognized status code :invalidcode (ArgumentError)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions