1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
# frozen_string_literal: true
# Released under the MIT License.
# Copyright, 2023-2025, by Samuel Williams.
require "protocol/http/response"
require "protocol/http/request"
describe Protocol::HTTP::Response do
let(:headers) {Protocol::HTTP::Headers.new}
let(:body) {nil}
InformationalResponse = Sus::Shared("informational response") do
it "should be informational" do
expect(response).to be(:informational?)
expect(response.as_json).to have_keys(status: be_within(100...200))
end
it "should not be a failure" do
expect(response).not.to be(:failure?)
end
end
SuccessfulResponse = Sus::Shared("successful response") do
it "should be successful" do
expect(response).to be(:success?)
expect(response.as_json).to have_keys(status: be_within(200...300))
end
it "should be final" do
expect(response).to be(:final?)
end
it "should not be informational" do
expect(response).not.to be(:informational?)
end
it "should not be a failure" do
expect(response).not.to be(:failure?)
end
end
RedirectionResponse = Sus::Shared("redirection response") do
it "should be final" do
expect(response).to be(:final?)
end
it "should be a redirection" do
expect(response).to be(:redirection?)
expect(response.as_json).to have_keys(status: be_within(300...400))
end
it "should not be informational" do
expect(response).not.to be(:informational?)
end
it "should not be a failure" do
expect(response).not.to be(:failure?)
end
end
FailureResponse = Sus::Shared("failure response") do
it "should not be successful" do
expect(response).not.to be(:success?)
end
it "should be final" do
expect(response).to be(:final?)
end
it "should not be informational" do
expect(response).not.to be(:informational?)
end
it "should be a failure" do
expect(response).to be(:failure?)
expect(response.as_json).to have_keys(status: be_within(400...600))
end
end
RedirectUsingOriginalMethod = Sus::Shared("redirect using original method") do
it "should preserve the method when following the redirect" do
expect(response).to be(:preserve_method?)
end
end
RedirectUsingGetAllowed = Sus::Shared("redirect using get allowed") do
it "should not preserve the method when following the redirect" do
expect(response).not.to be(:preserve_method?)
end
end
with "100 Continue" do
let(:response) {subject.new("HTTP/1.1", 100, headers)}
it "should have attributes" do
expect(response).to have_attributes(
version: be == "HTTP/1.1",
status: be == 100,
headers: be == headers,
body: be == nil,
protocol: be == nil
)
end
with "#as_json" do
it "generates a JSON representation" do
expect(response.as_json).to have_keys(
version: be == "HTTP/1.1",
status: be == 100,
headers: be == headers.as_json,
body: be == nil,
protocol: be == nil,
)
end
it "generates a JSON string" do
expect(JSON.dump(response)).to be == response.to_json
end
end
it_behaves_like InformationalResponse
it "should be a continue" do
expect(response).to be(:continue?)
end
it "should have a String representation" do
expect(response.to_s).to be == "100 HTTP/1.1"
end
it "should have an Array representation" do
expect(response.to_ary).to be == [100, headers, nil]
end
end
with "301 Moved Permanently" do
let(:response) {subject.new("HTTP/1.1", 301, headers, body)}
it_behaves_like RedirectionResponse
it_behaves_like RedirectUsingGetAllowed
end
with "302 Moved Permanently" do
let(:response) {subject.new("HTTP/1.1", 301, headers, body)}
it_behaves_like RedirectionResponse
it_behaves_like RedirectUsingGetAllowed
end
with "307 Temporary Redirect" do
let(:response) {subject.new("HTTP/1.1", 307, headers, body)}
it_behaves_like RedirectionResponse
it_behaves_like RedirectUsingOriginalMethod
end
with "308 Permanent Redirect" do
let(:response) {subject.new("HTTP/1.1", 308, headers, body)}
it_behaves_like RedirectionResponse
it_behaves_like RedirectUsingOriginalMethod
end
with "200 OK" do
let(:body) {Protocol::HTTP::Body::Buffered.wrap("Hello, World!")}
let(:response) {subject.new("HTTP/1.0", 200, headers, body)}
it "should have attributes" do
expect(response).to have_attributes(
version: be == "HTTP/1.0",
status: be == 200,
headers: be == headers,
body: be == body,
protocol: be_nil,
peer: be_nil,
)
end
with "#as_json" do
it "generates a JSON representation" do
expect(response.as_json).to have_keys(
version: be == "HTTP/1.0",
status: be == 200,
headers: be == headers.as_json,
body: be == body.as_json,
protocol: be == nil,
)
end
it "generates a JSON string" do
expect(JSON.dump(response)).to be == response.to_json
end
end
it_behaves_like SuccessfulResponse
it "should be ok" do
expect(response).to be(:ok?)
end
it "should not be a redirection" do
expect(response).not.to be(:redirection?)
end
it "should not be a hijack" do
expect(response).not.to be(:hijack?)
end
it "should not be a continue" do
expect(response).not.to be(:continue?)
end
it "should have a String representation" do
expect(response.to_s).to be == "200 HTTP/1.0"
end
it "should have an Array representation" do
expect(response.to_ary).to be == [200, headers, body]
end
end
with "400 Bad Request" do
let(:response) {subject.new("HTTP/1.1", 400, headers, body)}
it_behaves_like FailureResponse
it "should be a bad request" do
expect(response).to be(:bad_request?)
end
end
with "500 Internal Server Error" do
let(:response) {subject.new("HTTP/1.1", 500, headers, body)}
it_behaves_like FailureResponse
it "should be an internal server error" do
expect(response).to be(:internal_server_error?)
end
end
with ".for_exception" do
let(:exception) {StandardError.new("Something went wrong")}
let(:response) {subject.for_exception(exception)}
it "should have a 500 status" do
expect(response.status).to be == 500
expect(response.body.read).to be =~ /Something went wrong/
end
end
with "unmodified cached response" do
let(:response) {subject.new("HTTP/1.1", 304, headers, body)}
it "should have attributes" do
expect(response).to have_attributes(
version: be == "HTTP/1.1",
status: be == 304,
headers: be == headers,
body: be == body,
protocol: be == nil
)
end
it "should not be successful" do
expect(response).not.to be(:success?)
end
it "should be a redirection" do
expect(response).to be(:redirection?)
end
it "should be not modified" do
expect(response).to be(:not_modified?)
end
end
with ".[]" do
let(:body) {Protocol::HTTP::Body::Buffered.wrap("Hello, World!")}
let(:headers) {Protocol::HTTP::Headers[{"accept" => "text/html"}]}
it "creates a new response" do
response = subject[200, headers]
expect(response).to have_attributes(
version: be_nil,
status: be == 200,
headers: be == headers,
body: be_nil,
protocol: be_nil
)
end
it "creates a new response with keyword arguments" do
response = subject[200, headers: headers, body: body]
expect(response).to have_attributes(
version: be_nil,
status: be == 200,
headers: be == headers,
body: be == body,
protocol: be_nil
)
end
it "converts header hash to headers instance" do
response = subject[200, {"accept" => "text/html"}]
expect(response).to have_attributes(
headers: be == headers,
)
end
it "converts array body to buffered body" do
response = subject[200, headers: headers, body: ["Hello, World!"]]
expect(response).to have_attributes(
body: be_a(Protocol::HTTP::Body::Buffered)
)
end
end
end
|