Skip to content

Commit

Permalink
Add new Response.[] and MockResponse.[] which are very similar. F…
Browse files Browse the repository at this point in the history
…ixes rack#1094.
  • Loading branch information
ioquatix committed Jan 13, 2020
1 parent 256ea3e commit 4e10997
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/rack/mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ def self.env_for(uri = "", opts = {})
# MockRequest.

class MockResponse < Rack::Response
class << self
alias [] new
end

# Headers
attr_reader :original_headers, :cookies

Expand Down
4 changes: 4 additions & 0 deletions lib/rack/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ module Rack
# Your application's +call+ should end returning Response#finish.

class Response
def self.[] (status, headers, body)
self.new(body, status, headers)
end

attr_accessor :length, :status, :body
attr_reader :header
alias headers header
Expand Down
11 changes: 11 additions & 0 deletions test/spec_mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,17 @@
end

describe Rack::MockResponse do
it 'has standard constructor' do
headers = { "header" => "value" }
body = ["body"]

response = Rack::MockResponse[200, headers, body]

response.status.must_equal 200
response.headers.must_equal headers
response.body.must_equal body.join
end

it "provide access to the HTTP status" do
res = Rack::MockRequest.new(app).get("")
res.must_be :successful?
Expand Down
11 changes: 11 additions & 0 deletions test/spec_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@
require 'stringio'

describe Rack::Response do
it 'has standard constructor' do
headers = { "header" => "value" }
body = ["body"]

response = Rack::Response[200, headers, body]

response.status.must_equal 200
response.headers.must_equal headers
response.body.must_equal body
end

it 'has cache-control methods' do
response = Rack::Response.new
cc = 'foo'
Expand Down

0 comments on commit 4e10997

Please sign in to comment.