Skip to content

Commit 6c533bf

Browse files
authored
Introduce explicit support for informational responses. (#42)
* Add a little bit more documentation.
1 parent 67b3e8e commit 6c533bf

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

lib/protocol/http/response.rb

+21
Original file line numberDiff line numberDiff line change
@@ -29,42 +29,63 @@ def hijack?
2929
false
3030
end
3131

32+
# Whether the status is 100 (continue).
3233
def continue?
3334
@status == 100
3435
end
3536

37+
# Whether the status is considered informational.
38+
def informational?
39+
@status and @status >= 100 && @status < 200
40+
end
41+
42+
# Whether the status is considered final. Note that 101 is considered final.
43+
def final?
44+
# 101 is effectively a final status.
45+
@status and @status >= 200 || @status == 101
46+
end
47+
48+
# Whether the status is 200 (ok).
3649
def ok?
3750
@status == 200
3851
end
3952

53+
# Whether the status is considered successful.
4054
def success?
4155
@status and @status >= 200 && @status < 300
4256
end
4357

58+
# Whether the status is 206 (partial content).
4459
def partial?
4560
@status == 206
4661
end
4762

63+
# Whether the status is considered a redirection.
4864
def redirection?
4965
@status and @status >= 300 && @status < 400
5066
end
5167

68+
# Whether the status is 304 (not modified).
5269
def not_modified?
5370
@status == 304
5471
end
5572

73+
# Whether the status is 307 (temporary redirect) and should preserve the method of the request when following the redirect.
5674
def preserve_method?
5775
@status == 307 || @status == 308
5876
end
5977

78+
# Whether the status is considered a failure.
6079
def failure?
6180
@status and @status >= 400 && @status < 600
6281
end
6382

83+
# Whether the status is 400 (bad request).
6484
def bad_request?
6585
@status == 400
6686
end
6787

88+
# Whether the status is 500 (internal server error).
6889
def internal_server_error?
6990
@status == 500
7091
end

test/protocol/http/response.rb

+62-1
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,47 @@
1010
let(:headers) {Protocol::HTTP::Headers.new}
1111
let(:body) {nil}
1212

13+
InformationalResponse = Sus::Shared("informational response") do
14+
it "should be informational" do
15+
expect(response).to be(:informational?)
16+
end
17+
18+
it "should not be a failure" do
19+
expect(response).not.to be(:failure?)
20+
end
21+
end
22+
1323
SuccessfulResponse = Sus::Shared("successful response") do
1424
it "should be successful" do
1525
expect(response).to be(:success?)
1626
end
1727

28+
it "should be final" do
29+
expect(response).to be(:final?)
30+
end
31+
32+
it "should not be informational" do
33+
expect(response).not.to be(:informational?)
34+
end
35+
1836
it "should not be a failure" do
1937
expect(response).not.to be(:failure?)
2038
end
2139
end
2240

2341
RedirectionResponse = Sus::Shared("redirection response") do
42+
it "should be final" do
43+
expect(response).to be(:final?)
44+
end
45+
2446
it "should be a redirection" do
2547
expect(response).to be(:redirection?)
2648
end
2749

50+
it "should not be informational" do
51+
expect(response).not.to be(:informational?)
52+
end
53+
2854
it "should not be a failure" do
2955
expect(response).not.to be(:failure?)
3056
end
@@ -35,6 +61,14 @@
3561
expect(response).not.to be(:success?)
3662
end
3763

64+
it "should be final" do
65+
expect(response).to be(:final?)
66+
end
67+
68+
it "should not be informational" do
69+
expect(response).not.to be(:informational?)
70+
end
71+
3872
it "should be a failure" do
3973
expect(response).to be(:failure?)
4074
end
@@ -51,7 +85,34 @@
5185
expect(response).not.to be(:preserve_method?)
5286
end
5387
end
54-
88+
89+
with "100 Continue" do
90+
let(:response) {subject.new("HTTP/1.1", 100, headers)}
91+
92+
it "should have attributes" do
93+
expect(response).to have_attributes(
94+
version: be == "HTTP/1.1",
95+
status: be == 100,
96+
headers: be == headers,
97+
body: be == nil,
98+
protocol: be == nil
99+
)
100+
end
101+
102+
it_behaves_like InformationalResponse
103+
104+
it "should be a continue" do
105+
expect(response).to be(:continue?)
106+
end
107+
108+
it "should have a String representation" do
109+
expect(response.to_s).to be == "100 HTTP/1.1"
110+
end
111+
112+
it "should have an Array representation" do
113+
expect(response.to_ary).to be == [100, headers, nil]
114+
end
115+
end
55116

56117
with "301 Moved Permanently" do
57118
let(:response) {subject.new("HTTP/1.1", 301, headers, body)}

0 commit comments

Comments
 (0)