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 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
|
# frozen_string_literal: true
# Released under the MIT License.
# Copyright, 2018-2025, by Samuel Williams.
require "protocol/http/headers"
require "protocol/http/cookie"
describe Protocol::HTTP::Headers do
let(:fields) do
[
["Content-Type", "text/html"],
["connection", "Keep-Alive"],
["Set-Cookie", "hello=world"],
["Accept", "*/*"],
["set-cookie", "foo=bar"],
]
end
let(:headers) {subject[fields]}
with ".new" do
it "can construct headers with trailers" do
headers = subject.new(fields, 4)
expect(headers).to be(:trailer?)
expect(headers.trailer.to_a).to be == [
["set-cookie", "foo=bar"],
]
end
end
with ".[]" do
it "can be constructed from frozen array" do
self.fields.freeze
expect(headers.fields).not.to be(:frozen?)
end
end
with "#keys" do
it "should return keys" do
expect(headers.keys).to be == ["content-type", "connection", "set-cookie", "accept"]
end
end
with "#trailer?" do
it "should not be a trailer" do
expect(headers).not.to be(:trailer?)
expect(headers.tail).to be_nil
end
end
with "#merge" do
it "should merge headers" do
other = subject[[
# This will be appended:
["Set-Cookie", "goodbye=world"],
]]
merged = headers.merge(other)
expect(merged.to_h).to be == {
"content-type" => "text/html",
"set-cookie" => ["hello=world", "foo=bar", "goodbye=world"],
"accept" => ["*/*"],
"connection" => ["keep-alive"]
}
end
it "can't merge singleton headers" do
other = subject[[
["content-type", "text/plain"],
]]
# This doesn't fail as we haven't built an internal index yet:
merged = headers.merge(other)
expect do
# Once we build the index, it will fail:
merged.to_h
end.to raise_exception(Protocol::HTTP::DuplicateHeaderError)
end
end
with "#extract" do
it "can extract named fields" do
# Force the headers to be indexed:
headers.to_h
expect(headers.extract(["content-type", "set-cookie"])).to be == [
["Content-Type", "text/html"],
["Set-Cookie", "hello=world"],
["set-cookie", "foo=bar"],
]
end
end
with "#clear" do
it "should clear headers" do
headers.clear
expect(headers.fields).to be(:empty?)
end
end
with "#freeze" do
it "can't modify frozen headers" do
headers.freeze
expect(headers.fields).to be == fields
expect(headers.fields).to be(:frozen?)
expect(headers.to_h).to be(:frozen?)
end
it "returns duplicated headers if they are frozen" do
headers.freeze
expect(subject[headers]).not.to be(:frozen?)
end
end
with "#dup" do
it "should not modify source object" do
headers = self.headers.dup
headers["field"] = "value"
expect(self.headers).not.to be(:include?, "field")
end
end
with "#empty?" do
it "shouldn't be empty" do
expect(headers).not.to be(:empty?)
end
end
with "#include?" do
it "should include? named fields" do
expect(headers).to be(:include?, "set-cookie")
end
end
with "#key?" do
it "should key? named fields" do
expect(headers).to be(:key?, "set-cookie")
end
end
with "#fields" do
it "should add fields in order" do
expect(headers.fields).to be == fields
end
it "can enumerate fields" do
headers.each.with_index do |field, index|
expect(field).to be == fields[index]
end
end
end
with "#to_a" do
it "should return the fields array" do
expect(headers.to_a).to be == fields
end
it "should return the same object as fields" do
expect(headers.to_a).to be_equal(headers.fields)
end
it "should return an array" do
expect(headers.to_a).to be_a(Array)
end
end
with "#to_h" do
it "should generate array values for duplicate keys" do
expect(headers.to_h["set-cookie"]).to be == ["hello=world", "foo=bar"]
end
end
with "#inspect" do
it "should generate a string representation" do
expect(headers.inspect).to be == "#<Protocol::HTTP::Headers #{fields.inspect}>"
end
end
with "#[]" do
it "can lookup fields" do
expect(headers["content-type"]).to be == "text/html"
end
end
with "#[]=" do
it "can add field with a String value" do
headers["Content-Length"] = "1"
expect(headers.fields.last).to be == ["Content-Length", "1"]
expect(headers["content-length"]).to be == "1"
end
it "can add field with an Integer value" do
headers["Content-Length"] = 1
expect(headers.fields.last).to be == ["Content-Length", "1"]
expect(headers["content-length"]).to be == "1"
end
it "can add field with indexed hash" do
expect(headers.to_h).not.to be(:empty?)
headers["Content-Length"] = "1"
expect(headers["content-length"]).to be == "1"
end
end
with "#add" do
it "can add field" do
headers.add("Content-Length", 1)
expect(headers.fields.last).to be == ["Content-Length", "1"]
expect(headers["content-length"]).to be == "1"
end
end
with "#set" do
it "can replace an existing field" do
headers.add("accept-encoding", "gzip,deflate")
headers.set("accept-encoding", "gzip")
expect(headers["accept-encoding"]).to be == ["gzip"]
end
end
with "#extract" do
it "can extract key's that don't exist" do
expect(headers.extract("foo")).to be(:empty?)
end
it "can extract single key" do
expect(headers.extract("content-type")).to be == [["Content-Type", "text/html"]]
end
end
with "#==" do
it "can compare with array" do
expect(headers).to be == fields
end
it "can compare with itself" do
expect(headers).to be == headers
end
it "can compare with hash" do
expect(headers).not.to be == {}
end
end
with "#delete" do
it "can delete case insensitive fields" do
expect(headers.delete("content-type")).to be == "text/html"
expect(headers.fields).to be == fields[1..-1]
end
it "can delete non-existant fields" do
expect(headers.delete("transfer-encoding")).to be_nil
end
end
with "#merge" do
it "can merge content-length" do
headers.merge!("content-length" => 2)
expect(headers["content-length"]).to be == "2"
end
end
with "#trailer!" do
it "can add trailer" do
headers.add("trailer", "etag")
count = headers.fields.size
trailer = headers.trailer!
expect(headers.tail).to be == count
headers.add("etag", "abcd")
expect(trailer.to_h).to be == {"etag" => "abcd"}
end
it "can add trailer without explicit header" do
trailer = headers.trailer!
headers.add("etag", "abcd")
expect(trailer.to_h).to be == {"etag" => "abcd"}
end
with "forbidden trailers" do
let(:headers) {subject.new}
forbidden_trailers = %w[
accept
accept-charset
accept-encoding
accept-language
authorization
proxy-authorization
www-authenticate
proxy-authenticate
connection
content-length
transfer-encoding
te
upgrade
trailer
host
expect
range
content-type
content-encoding
content-range
cookie
set-cookie
x-foo-bar
]
forbidden_trailers.each do |key|
it "can't add a #{key.inspect} header in the trailer", unique: key do
trailer = headers.trailer!
headers.add(key, "example")
expect(headers).not.to be(:include?, key)
end
end
end
with "permitted trailers" do
let(:headers) {subject.new}
permitted_trailers = [
"date",
"digest",
"etag",
"server-timing",
]
permitted_trailers.each do |key|
it "can add a #{key.inspect} header in the trailer", unique: key do
trailer = headers.trailer!
headers.add(key, "example")
expect(headers).to be(:include?, key)
end
end
end
end
with "#trailer" do
it "can enumerate trailer" do
headers.add("trailer", "etag")
headers.trailer!
headers.add("etag", "abcd")
expect(headers.trailer.to_h).to be == {"etag" => "abcd"}
end
end
with "custom policy" do
let(:headers) {subject.new}
# Create a custom header class that allows trailers
let(:grpc_status_class) do
Class.new(String) do
def self.trailer?
true
end
end
end
it "can set custom policy to allow additional trailer headers" do
# Create custom policy that allows grpc-status as trailer
custom_policy = Protocol::HTTP::Headers::POLICY.dup
custom_policy["grpc-status"] = grpc_status_class
# Set the custom policy
headers.policy = custom_policy
# Enable trailers
headers.trailer!
# Add grpc-status header (should be allowed with custom policy)
headers.add("grpc-status", "0")
# Verify it appears in trailers
expect(headers).to be(:include?, "grpc-status")
trailer_headers = {}
headers.trailer do |key, value|
trailer_headers[key] = value
end
expect(trailer_headers["grpc-status"]).to be == "0"
end
it "policy= clears indexed cache" do
# Add some headers first
headers.add("content-type", "text/html")
# Force indexing
hash1 = headers.to_h
expect(hash1).to be(:include?, "content-type")
# Change policy
new_policy = {}
headers.policy = new_policy
# Add another header
headers.add("x-custom", "value")
# Verify cache was cleared and rebuilt
hash2 = headers.to_h
expect(hash2).to be(:include?, "content-type")
expect(hash2).to be(:include?, "x-custom")
end
it "can read policy attribute" do
original_policy = headers.policy
expect(original_policy).to be == Protocol::HTTP::Headers::POLICY
# Set new policy
custom_policy = {"custom" => String}
headers.policy = custom_policy
# Verify policy was changed
expect(headers.policy).to be == custom_policy
expect(headers.policy).not.to be == original_policy
end
end
with "#flatten!" do
it "can flatten trailer" do
headers.add("trailer", "etag")
trailer = headers.trailer!
headers.add("etag", "abcd")
headers.flatten!
expect(headers).not.to have_keys("trailer")
expect(headers).to have_keys("etag")
end
end
with "#flatten" do
it "can flatten trailer" do
headers.add("trailer", "etag")
trailer = headers.trailer!
headers.add("etag", "abcd")
copy = headers.flatten
expect(headers).to have_keys("trailer")
expect(headers).to have_keys("etag")
expect(copy).not.to have_keys("trailer")
expect(copy).to have_keys("etag")
end
end
with "set-cookie" do
it "can extract parsed cookies" do
expect(headers["set-cookie"]).to be_a(Protocol::HTTP::Header::Cookie)
end
end
with "connection" do
it "can extract connection options" do
expect(headers["connection"]).to be_a(Protocol::HTTP::Header::Connection)
end
it "should normalize to lower case" do
expect(headers["connection"]).to be == ["keep-alive"]
end
end
end
describe Protocol::HTTP::Headers::Merged do
let(:merged) do
Protocol::HTTP::Headers::Merged.new(
Protocol::HTTP::Headers["content-type" => "text/html"],
Protocol::HTTP::Headers["content-encoding" => "gzip"]
)
end
with "#flatten" do
let(:flattened) {merged.flatten}
it "can combine all headers" do
expect(flattened).to be_a Protocol::HTTP::Headers
expect(flattened.fields).to be == [
["content-type", "text/html"],
["content-encoding", "gzip"]
]
end
end
with "#clear" do
it "can clear all headers" do
merged.clear
expect(merged.flatten).to be(:empty?)
end
end
with "#each" do
it "can iterate over all headers" do
expect(merged.each.to_a).to be == [
["content-type", "text/html"],
["content-encoding", "gzip"]
]
end
end
with "non-normalized case" do
let(:merged) do
Protocol::HTTP::Headers::Merged.new(
Protocol::HTTP::Headers["Content-Type" => "text/html"],
Protocol::HTTP::Headers["Content-Encoding" => "gzip"]
)
end
it "can normalize case" do
expect(merged.each.to_a).to be == [
["content-type", "text/html"],
["content-encoding", "gzip"]
]
end
end
end
|