Skip to content

Commit a44f395

Browse files
committed
Add the Response#status method
1 parent 84bdccb commit a44f395

3 files changed

Lines changed: 48 additions & 10 deletions

File tree

lib/rage/controller/api.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,9 @@ def initialize(env, params)
451451
@__rendered = false
452452
end
453453
454+
# @private
455+
attr_reader :__status, :__headers, :__body
456+
454457
# Get the request object. See {Rage::Request}.
455458
# @return [Rage::Request]
456459
def request
@@ -460,7 +463,7 @@ def request
460463
# Get the response object. See {Rage::Response}.
461464
# @return [Rage::Response]
462465
def response
463-
@response ||= Rage::Response.new(@__headers, @__body)
466+
@response ||= Rage::Response.new(self)
464467
end
465468
466469
# Get the cookie object. See {Rage::Cookies}.

lib/rage/response.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,26 @@ class Rage::Response
88
LAST_MODIFIED_HEADER = "Last-Modified"
99

1010
# @private
11-
def initialize(headers, body)
12-
@headers = headers
13-
@body = body
11+
def initialize(controller)
12+
@controller = controller
13+
end
14+
15+
# Returns the HTTP status code of the response.
16+
# @return [Integer]
17+
def status
18+
@controller.__status
1419
end
1520

1621
# Returns the content of the response as a string. This contains the contents of any calls to `render`.
1722
# @return [String]
1823
def body
19-
@body[0] || ""
24+
@controller.__body[0] || ""
2025
end
2126

2227
# Returns the headers for the response.
2328
# @return [Hash]
2429
def headers
25-
@headers
30+
@controller.__headers
2631
end
2732

2833
# Returns ETag response header or +nil+ if it's empty.

spec/rage/response_spec.rb

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
# frozen_string_literal: true
22

33
RSpec.describe Rage::Response do
4+
subject { described_class.new(controller) }
5+
6+
let(:controller) { RageController::API.new(nil, nil) }
47
let(:headers) { {} }
5-
let(:body) { [] }
68

7-
subject { described_class.new(headers, body) }
9+
before do
10+
headers.each do |key, value|
11+
controller.headers[key] = value
12+
end
13+
end
814

915
describe "#etag" do
1016
let(:etag) { "1234" }
@@ -116,11 +122,35 @@
116122
end
117123

118124
context "with non-empty body" do
119-
let(:body) { [:test_body] }
125+
before do
126+
controller.render plain: "test_body"
127+
end
120128

121129
it "returns the body" do
122-
expect(subject.body).to eq(:test_body)
130+
expect(subject.body).to eq("test_body")
123131
end
124132
end
125133
end
134+
135+
context "#status" do
136+
it "defaults to 204" do
137+
expect(subject.status).to eq(204)
138+
end
139+
140+
it "tracks status updates via head" do
141+
controller.head 401
142+
expect(subject.status).to eq(401)
143+
144+
controller.head 403
145+
expect(subject.status).to eq(403)
146+
end
147+
148+
it "tracks status updates via render" do
149+
controller.render status: 401
150+
expect(subject.status).to eq(401)
151+
152+
controller.head 403
153+
expect(subject.status).to eq(403)
154+
end
155+
end
126156
end

0 commit comments

Comments
 (0)