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 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
|
RSpec.describe HTTP::Headers do
subject(:headers) { described_class.new }
it "is Enumerable" do
expect(headers).to be_an Enumerable
end
describe "#set" do
it "sets header value" do
headers.set "Accept", "application/json"
expect(headers["Accept"]).to eq "application/json"
end
it "normalizes header name" do
headers.set :content_type, "application/json"
expect(headers["Content-Type"]).to eq "application/json"
end
it "overwrites previous value" do
headers.set :set_cookie, "hoo=ray"
headers.set :set_cookie, "woo=hoo"
expect(headers["Set-Cookie"]).to eq "woo=hoo"
end
it "allows set multiple values" do
headers.set :set_cookie, "hoo=ray"
headers.set :set_cookie, %w(hoo=ray woo=hoo)
expect(headers["Set-Cookie"]).to eq %w(hoo=ray woo=hoo)
end
it "fails with empty header name" do
expect { headers.set "", "foo bar" }.
to raise_error HTTP::InvalidHeaderNameError
end
it "fails with invalid header name" do
expect { headers.set "foo bar", "baz" }.
to raise_error HTTP::InvalidHeaderNameError
end
end
describe "#[]=" do
it "sets header value" do
headers["Accept"] = "application/json"
expect(headers["Accept"]).to eq "application/json"
end
it "normalizes header name" do
headers[:content_type] = "application/json"
expect(headers["Content-Type"]).to eq "application/json"
end
it "overwrites previous value" do
headers[:set_cookie] = "hoo=ray"
headers[:set_cookie] = "woo=hoo"
expect(headers["Set-Cookie"]).to eq "woo=hoo"
end
it "allows set multiple values" do
headers[:set_cookie] = "hoo=ray"
headers[:set_cookie] = %w(hoo=ray woo=hoo)
expect(headers["Set-Cookie"]).to eq %w(hoo=ray woo=hoo)
end
end
describe "#delete" do
before { headers.set "Content-Type", "application/json" }
it "removes given header" do
headers.delete "Content-Type"
expect(headers["Content-Type"]).to be_nil
end
it "normalizes header name" do
headers.delete :content_type
expect(headers["Content-Type"]).to be_nil
end
it "fails with empty header name" do
expect { headers.delete "" }.
to raise_error HTTP::InvalidHeaderNameError
end
it "fails with invalid header name" do
expect { headers.delete "foo bar" }.
to raise_error HTTP::InvalidHeaderNameError
end
end
describe "#add" do
it "sets header value" do
headers.add "Accept", "application/json"
expect(headers["Accept"]).to eq "application/json"
end
it "normalizes header name" do
headers.add :content_type, "application/json"
expect(headers["Content-Type"]).to eq "application/json"
end
it "appends new value if header exists" do
headers.add :set_cookie, "hoo=ray"
headers.add :set_cookie, "woo=hoo"
expect(headers["Set-Cookie"]).to eq %w(hoo=ray woo=hoo)
end
it "allows append multiple values" do
headers.add :set_cookie, "hoo=ray"
headers.add :set_cookie, %w(woo=hoo yup=pie)
expect(headers["Set-Cookie"]).to eq %w(hoo=ray woo=hoo yup=pie)
end
it "fails with empty header name" do
expect { headers.add "", "foobar" }.
to raise_error HTTP::InvalidHeaderNameError
end
it "fails with invalid header name" do
expect { headers.add "foo bar", "baz" }.
to raise_error HTTP::InvalidHeaderNameError
end
end
describe "#get" do
before { headers.set "Content-Type", "application/json" }
it "returns array of associated values" do
expect(headers.get "Content-Type").to eq %w(application/json)
end
it "normalizes header name" do
expect(headers.get :content_type).to eq %w(application/json)
end
context "when header does not exists" do
it "returns empty array" do
expect(headers.get :accept).to eq []
end
end
it "fails with empty header name" do
expect { headers.get "" }.
to raise_error HTTP::InvalidHeaderNameError
end
it "fails with invalid header name" do
expect { headers.get "foo bar" }.
to raise_error HTTP::InvalidHeaderNameError
end
end
describe "#[]" do
context "when header does not exists" do
it "returns nil" do
expect(headers[:accept]).to be_nil
end
end
context "when header has a single value" do
before { headers.set "Content-Type", "application/json" }
it "normalizes header name" do
expect(headers[:content_type]).to_not be_nil
end
it "returns it returns a single value" do
expect(headers[:content_type]).to eq "application/json"
end
end
context "when header has a multiple values" do
before do
headers.add :set_cookie, "hoo=ray"
headers.add :set_cookie, "woo=hoo"
end
it "normalizes header name" do
expect(headers[:set_cookie]).to_not be_nil
end
it "returns array of associated values" do
expect(headers[:set_cookie]).to eq %w(hoo=ray woo=hoo)
end
end
end
describe "#to_h" do
before do
headers.add :content_type, "application/json"
headers.add :set_cookie, "hoo=ray"
headers.add :set_cookie, "woo=hoo"
end
it "returns a Hash" do
expect(headers.to_h).to be_a ::Hash
end
it "returns Hash with normalized keys" do
expect(headers.to_h.keys).to match_array %w(Content-Type Set-Cookie)
end
context "for a header with single value" do
it "provides a value as is" do
expect(headers.to_h["Content-Type"]).to eq "application/json"
end
end
context "for a header with multiple values" do
it "provides an array of values" do
expect(headers.to_h["Set-Cookie"]).to eq %w(hoo=ray woo=hoo)
end
end
end
describe "#to_a" do
before do
headers.add :content_type, "application/json"
headers.add :set_cookie, "hoo=ray"
headers.add :set_cookie, "woo=hoo"
end
it "returns an Array" do
expect(headers.to_a).to be_a Array
end
it "returns Array of key/value pairs with normalized keys" do
expect(headers.to_a).to eq [
%w(Content-Type application/json),
%w(Set-Cookie hoo=ray),
%w(Set-Cookie woo=hoo)
]
end
end
describe "#inspect" do
before { headers.set :set_cookie, %w(hoo=ray woo=hoo) }
subject { headers.inspect }
it { is_expected.to eq '#<HTTP::Headers {"Set-Cookie"=>["hoo=ray", "woo=hoo"]}>' }
end
describe "#keys" do
before do
headers.add :content_type, "application/json"
headers.add :set_cookie, "hoo=ray"
headers.add :set_cookie, "woo=hoo"
end
it "returns uniq keys only" do
expect(headers.keys.size).to eq 2
end
it "normalizes keys" do
expect(headers.keys).to include("Content-Type", "Set-Cookie")
end
end
describe "#each" do
before do
headers.add :set_cookie, "hoo=ray"
headers.add :content_type, "application/json"
headers.add :set_cookie, "woo=hoo"
end
it "yields each key/value pair separatedly" do
expect { |b| headers.each(&b) }.to yield_control.exactly(3).times
end
it "yields headers in the same order they were added" do
expect { |b| headers.each(&b) }.to yield_successive_args(
%w(Set-Cookie hoo=ray),
%w(Content-Type application/json),
%w(Set-Cookie woo=hoo)
)
end
it "returns self instance if block given" do
expect(headers.each { |*| }).to be headers
end
it "returns Enumerator if no block given" do
expect(headers.each).to be_a Enumerator
end
end
describe ".empty?" do
subject { headers.empty? }
context "initially" do
it { is_expected.to be true }
end
context "when header exists" do
before { headers.add :accept, "text/plain" }
it { is_expected.to be false }
end
context "when last header was removed" do
before do
headers.add :accept, "text/plain"
headers.delete :accept
end
it { is_expected.to be true }
end
end
describe "#hash" do
let(:left) { described_class.new }
let(:right) { described_class.new }
it "equals if two headers equals" do
left.add :accept, "text/plain"
right.add :accept, "text/plain"
expect(left.hash).to eq right.hash
end
end
describe "#==" do
let(:left) { described_class.new }
let(:right) { described_class.new }
it "compares header keys and values" do
left.add :accept, "text/plain"
right.add :accept, "text/plain"
expect(left).to eq right
end
it "allows comparison with Array of key/value pairs" do
left.add :accept, "text/plain"
expect(left).to eq [%w(Accept text/plain)]
end
it "sensitive to headers order" do
left.add :accept, "text/plain"
left.add :cookie, "woo=hoo"
right.add :cookie, "woo=hoo"
right.add :accept, "text/plain"
expect(left).to_not eq right
end
it "sensitive to header values order" do
left.add :cookie, "hoo=ray"
left.add :cookie, "woo=hoo"
right.add :cookie, "woo=hoo"
right.add :cookie, "hoo=ray"
expect(left).to_not eq right
end
end
describe "#dup" do
before { headers.set :content_type, "application/json" }
subject(:dupped) { headers.dup }
it { is_expected.to be_a described_class }
it { is_expected.not_to be headers }
it "has headers copied" do
expect(dupped[:content_type]).to eq "application/json"
end
context "modifying a copy" do
before { dupped.set :content_type, "text/plain" }
it "modifies dupped copy" do
expect(dupped[:content_type]).to eq "text/plain"
end
it "does not affects original headers" do
expect(headers[:content_type]).to eq "application/json"
end
end
end
describe "#merge!" do
before do
headers.set :host, "example.com"
headers.set :accept, "application/json"
headers.merge! :accept => "plain/text", :cookie => %w(hoo=ray woo=hoo)
end
it "leaves headers not presented in other as is" do
expect(headers[:host]).to eq "example.com"
end
it "overwrites existing values" do
expect(headers[:accept]).to eq "plain/text"
end
it "appends other headers, not presented in base" do
expect(headers[:cookie]).to eq %w(hoo=ray woo=hoo)
end
end
describe "#merge" do
before do
headers.set :host, "example.com"
headers.set :accept, "application/json"
end
subject(:merged) do
headers.merge :accept => "plain/text", :cookie => %w(hoo=ray woo=hoo)
end
it { is_expected.to be_a described_class }
it { is_expected.not_to be headers }
it "does not affects original headers" do
expect(merged.to_h).to_not eq headers.to_h
end
it "leaves headers not presented in other as is" do
expect(merged[:host]).to eq "example.com"
end
it "overwrites existing values" do
expect(merged[:accept]).to eq "plain/text"
end
it "appends other headers, not presented in base" do
expect(merged[:cookie]).to eq %w(hoo=ray woo=hoo)
end
end
describe ".coerce" do
let(:dummyClass) { Class.new { def respond_to?(*); end } }
it "accepts any object that respond to #to_hash" do
hashie = double :to_hash => {"accept" => "json"}
expect(described_class.coerce(hashie)["accept"]).to eq "json"
end
it "accepts any object that respond to #to_h" do
hashie = double :to_h => {"accept" => "json"}
expect(described_class.coerce(hashie)["accept"]).to eq "json"
end
it "accepts any object that respond to #to_a" do
hashie = double :to_a => [%w(accept json)]
expect(described_class.coerce(hashie)["accept"]).to eq "json"
end
it "fails if given object cannot be coerced" do
expect { described_class.coerce dummyClass.new }.to raise_error HTTP::Error
end
context "with duplicate header keys (mixed case)" do
let(:headers) { {"Set-Cookie" => "hoo=ray", "set-cookie" => "woo=hoo"} }
it "adds all headers" do
expect(described_class.coerce(headers).to_a).to match_array([
%w(Set-Cookie hoo=ray),
%w(Set-Cookie woo=hoo)
])
end
end
it "is aliased as .[]" do
expect(described_class.method :coerce).to eq described_class.method(:[])
end
end
end
|