Skip to content

Commit

Permalink
Support have_http_status with Rack::MockResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
cbliard committed Jun 25, 2024
1 parent 591ef0b commit 690fb49
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/rspec/rails/matchers/have_http_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ def as_test_response(obj)
resp.request = ActionDispatch::Request.new({})
end
::ActionDispatch::TestResponse.from_response(obj)
elsif obj.respond_to?(:status) && obj.respond_to?(:headers) && obj.respond_to?(:body)
# Hack to support `Rack::MockResponse` without having to load
# Rack or catch `NameError`s for the undefined constants
resp = ::ActionDispatch::Response.new(obj.status, obj.headers, obj.body)
resp.request = ::ActionDispatch::Request.new({})
::ActionDispatch::TestResponse.from_response(obj)
else
raise TypeError, "Invalid response type: #{obj}"
end
Expand Down
13 changes: 13 additions & 0 deletions spec/rspec/rails/matchers/have_http_status_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ def create_response(opts = {})
end
end

context "given something that acts as a Rack::MockResponse" do
it "returns true for a response with the same code" do
response = instance_double(
'::Rack::MockResponse',
status: code,
headers: {},
body: ""
)

expect(matcher.matches?(response)).to be(true)
end
end

it "returns false given another type" do
response = Object.new

Expand Down

0 comments on commit 690fb49

Please sign in to comment.