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
|
# frozen_string_literal: true
# Released under the MIT License.
# Copyright, 2019-2025, by Samuel Williams.
# Copyright, 2023, by Marco Concetto Rudilosso.
require "protocol/http2/connection_context"
describe Protocol::HTTP2::Connection do
let(:stream) {StringIO.new}
let(:framer) {Protocol::HTTP2::Framer.new(stream)}
let(:connection) {subject.new(framer, 1)}
with "#maximum_concurrent_streams" do
it "is the remote peer's maximum concurrent streams" do
connection.remote_settings.maximum_concurrent_streams = 10
connection.local_settings.current.maximum_concurrent_streams = 5
expect(connection.maximum_concurrent_streams).to be == 10
end
end
with "#receive_headers" do
it "fails with protocol error if exceeding the maximum concurrent connections" do
connection.local_settings.current.maximum_concurrent_streams = 1
# Create a stream to
connection.streams[1] = Protocol::HTTP2::Stream.new(connection, 1)
frame = Protocol::HTTP2::HeadersFrame.new(3)
expect do
connection.receive_headers(frame)
end.to raise_exception(Protocol::HTTP2::ProtocolError, message: be =~ /Exceeded maximum concurrent streams/)
end
end
it "reports the connection id 0 is not closed" do
expect(connection).not.to be(:closed_stream_id?, 0)
end
it "does not report any stream_id as being remote" do
expect(connection).not.to be(:valid_remote_stream_id?, 1)
end
it "rejects a push promise" do
frame = Protocol::HTTP2::PushPromiseFrame.new
expect do
connection.receive_push_promise(frame)
end.to raise_exception(Protocol::HTTP2::ProtocolError, message: be =~ /Unable to receive push promise/)
end
it "rejects a stream reset to stream id 0" do
frame = Protocol::HTTP2::ResetStreamFrame.new(0)
expect do
connection.receive_reset_stream(frame)
end.to raise_exception(Protocol::HTTP2::ProtocolError, message: be =~ /Cannot reset connection/)
end
it "rejects headers to stream id 0" do
frame = Protocol::HTTP2::HeadersFrame.new(0)
expect do
connection.receive_headers(frame)
end.to raise_exception(Protocol::HTTP2::ProtocolError, message: be =~ /Cannot receive headers for stream 0/)
end
it "rejects continuation" do
frame = Protocol::HTTP2::ContinuationFrame.new(1)
expect do
connection.receive_continuation(frame)
end.to raise_exception(Protocol::HTTP2::ProtocolError, message: be =~ /Received unexpected continuation/)
end
it "rejects incorrectly encoded headers" do
expect(connection).to receive(:valid_remote_stream_id?).and_return(true)
invalid_headers_frame = Protocol::HTTP2::HeadersFrame.new(1)
invalid_headers_frame.pack("\xFF")
invalid_headers_frame.write(stream)
stream.rewind
expect(connection).to receive(:send_goaway)
expect do
connection.read_frame
end.to raise_exception(Protocol::HPACK::Error)
end
it "can't write frames to a closed connection" do
connection.close
expect do
connection.write_frames do |framer|
end
end.to raise_exception(EOFError, message: be =~ /Connection closed/)
end
end
with "client and server" do
include_context Protocol::HTTP2::ConnectionContext
it "can negotiate connection" do
first_server_frame = nil
first_client_frame = client.send_connection_preface do
first_server_frame = server.read_connection_preface([])
end
expect(first_client_frame).to be_a Protocol::HTTP2::SettingsFrame
expect(first_client_frame).not.to be(:acknowledgement?)
expect(first_server_frame).to be_a Protocol::HTTP2::SettingsFrame
expect(first_server_frame).not.to be(:acknowledgement?)
frame = client.read_frame
expect(frame).to be_a Protocol::HTTP2::SettingsFrame
expect(frame).to be(:acknowledgement?)
frame = server.read_frame
expect(frame).to be_a Protocol::HTTP2::SettingsFrame
expect(frame).to be(:acknowledgement?)
expect(client.state).to be == :open
expect(server.state).to be == :open
end
describe Protocol::HTTP2::PingFrame do
def before
client.open!
server.open!
super
end
it "can send ping and receive pong" do
expect(server).to receive(:receive_ping)
client.send_ping("12345678")
server.read_frame
expect(client).to receive(:receive_ping)
client.read_frame
end
end
describe Protocol::HTTP2::Stream do
def before
client.open!
server.open!
super
end
let(:request_data) {"Hello World!"}
let(:stream) {client.create_stream}
let(:request_headers) {[[":method", "GET"], [":path", "/"], [":authority", "localhost"]]}
let(:response_headers) {[[":status", "200"]]}
it "can determine who initiated stream" do
expect(client).to be(:client_stream_id?, stream.id)
expect(client).not.to be(:server_stream_id?, stream.id)
end
it "can determine closed streams" do
expect(client).not.to be(:idle_stream_id?, stream.id)
expect(server).to be(:idle_stream_id?, stream.id)
stream.send_headers(request_headers)
server.read_frame
# `closed_stream_id?` is true even if the stream is still open. It implies that if the stream is not otherwise open, it is closed.
expect(server[stream.id]).not.to be_nil
expect(server).to be(:closed_stream_id?, stream.id)
end
with "server created stream" do
let(:stream) {server.create_stream}
it "can determine who initiated stream" do
expect(client).to be(:idle_stream_id?, stream.id)
expect(server).not.to be(:idle_stream_id?, stream.id)
end
end
it "can create new stream and send response" do
# First argument is deprecated priority.
stream.send_headers(request_headers)
expect(stream.id).to be == 1
expect(server).to receive(:receive_headers) do |frame|
headers = super(frame)
expect(headers).to be == request_headers
end
server.read_frame
expect(server.streams).not.to be(:empty?)
expect(server.streams[1].state).to be == :open
stream.send_data(request_data, Protocol::HTTP2::END_STREAM)
expect(stream.state).to be == :half_closed_local
expect(server).to receive(:receive_data)
data_frame = server.read_frame
expect(data_frame.unpack).to be == request_data
expect(server.streams[1].state).to be == :half_closed_remote
server.streams[1].send_headers(response_headers, Protocol::HTTP2::END_STREAM)
expect(stream).to receive(:process_headers) do |frame|
headers = super(frame)
expect(headers).to be == response_headers
end
client.read_frame
expect(stream.state).to be == :closed
expect(stream.remote_window.used).to be == data_frame.length
end
it "can update settings while sending response" do
stream.send_headers(request_headers)
server.read_frame
client.send_settings([
[Protocol::HTTP2::Settings::INITIAL_WINDOW_SIZE, 100]
])
frame = server.read_frame
expect(frame).to be_a(Protocol::HTTP2::SettingsFrame)
frame = client.read_frame
expect(frame).to be_a(Protocol::HTTP2::SettingsFrame)
expect(frame).to be(:acknowledgement?)
end
it "can update concurrent streams" do
initial_maximum_concurrent_streams = server.maximum_concurrent_streams
inform "Initial maximum concurrent streams: #{initial_maximum_concurrent_streams}"
server.send_settings([
[Protocol::HTTP2::Settings::MAXIMUM_CONCURRENT_STREAMS, 1]
])
# It's not acknowledged until the client sends an acknowledgement:
expect(server).to have_attributes(
maximum_concurrent_streams: be == initial_maximum_concurrent_streams
)
expect(client).to have_attributes(
maximum_concurrent_streams: be == initial_maximum_concurrent_streams
)
frame = client.read_frame
expect(frame).to be_a(Protocol::HTTP2::SettingsFrame)
expect(client).to have_attributes(
maximum_concurrent_streams: be == 1
)
frame = server.read_frame
expect(frame).to be_a(Protocol::HTTP2::SettingsFrame)
expect(frame).to be(:acknowledgement?)
expect(server.local_settings).to have_attributes(
maximum_concurrent_streams: be == 1
)
# The client maximum concurrent streams was not changed because the client did not `send_settings`:
expect(server).to have_attributes(
maximum_concurrent_streams: be == initial_maximum_concurrent_streams
)
end
it "doesn't accept headers for an existing stream" do
stream.send_headers(request_headers)
expect(stream.id).to be == 1
server.read_frame
expect(server.streams).not.to be(:empty?)
expect(server.streams[1].state).to be == :open
stream.send_headers(request_headers)
# Simulate a closed stream:
server.streams.clear
expect do
server.read_frame
end.to raise_exception(Protocol::HTTP2::ProtocolError, message: be =~ /Invalid stream id/)
end
it "client can handle graceful shutdown" do
stream.send_headers(request_headers, Protocol::HTTP2::END_STREAM)
# Establish request stream on server:
server.read_frame
# Graceful shutdown
server.send_goaway(0)
another_stream = client.create_stream
another_stream.send_headers(request_headers, Protocol::HTTP2::END_STREAM)
expect(client.read_frame).to be_a Protocol::HTTP2::GoawayFrame
expect(client.remote_stream_id).to be == 1
expect(client).to be(:closed?)
# The server will ignore this frame as it was sent after the graceful shutdown:
server.read_frame
expect(server.streams[another_stream.id]).to be_nil
# The pre-existing stream is still functional:
expect(server.streams[1].state).to be == :half_closed_remote
server.streams[1].send_headers(response_headers, Protocol::HTTP2::END_STREAM)
client.read_frame
expect(stream.state).to be == :closed
end
it "client can handle non-graceful shutdown" do
stream.send_headers(request_headers, Protocol::HTTP2::END_STREAM)
# Establish request stream on server:
server.read_frame
# Send connection error to client:
server.send_goaway(1, "Bugger off!")
expect(stream).to receive(:close)
expect do
client.read_frame
end.to raise_exception(Protocol::HTTP2::GoawayError)
client.close
expect(client.remote_stream_id).to be == 1
expect(client).to be(:closed?)
end
it "can stream data" do
stream.send_headers(request_headers)
stream.send_data("A")
stream.send_data("B")
stream.send_data("C")
stream.send_data("", Protocol::HTTP2::END_STREAM)
frame = server.read_frame
expect(frame).to be_a(Protocol::HTTP2::HeadersFrame)
expect(stream.available_frame_size).to be >= 3
frame = server.read_frame
expect(frame).to be_a(Protocol::HTTP2::DataFrame)
expect(frame.unpack).to be == "A"
frame = server.read_frame
expect(frame).to be_a(Protocol::HTTP2::DataFrame)
expect(frame.unpack).to be == "B"
frame = server.read_frame
expect(frame).to be_a(Protocol::HTTP2::DataFrame)
expect(frame.unpack).to be == "C"
frame = server.read_frame
expect(frame).to be_a(Protocol::HTTP2::DataFrame)
expect(frame.unpack).to be == ""
expect(frame).to be(:end_stream?)
end
end
it "allows client to create new stream and send headers when client maximum concurrent streams is 0" do
client.local_settings.current.maximum_concurrent_streams = 0
client_stream = client.create_stream
request_headers = [[":method", "GET"], [":path", "/"], [":authority", "localhost"]]
client_stream.send_headers(request_headers)
expect(server).to receive(:receive_headers) do |frame|
headers = super(frame)
expect(headers).to be == request_headers
end
server.read_frame
end
it "does not allow client to create new stream and send headers when server maximum concurrent streams is 0" do
server.local_settings.current.maximum_concurrent_streams = 0
client_stream = client.create_stream
request_headers = [[":method", "GET"], [":path", "/"], [":authority", "localhost"]]
client_stream.send_headers(request_headers)
expect {server.read_frame}.to raise_exception(Protocol::HTTP2::ProtocolError)
end
it "allows server to create new stream and send headers when server maximum concurrent streams is 0" do
server.local_settings.current.maximum_concurrent_streams = 0
server_stream = server.create_stream
request_headers = [[":method", "GET"], [":path", "/"], [":authority", "localhost"]]
server_stream.send_headers(request_headers)
expect(client).to receive(:receive_headers) do |frame|
headers = super(frame)
expect(headers).to be == request_headers
end
client.read_frame
end
it "does not allow server to create new stream send headers when client maximum concurrent streams is 0" do
client.local_settings.current.maximum_concurrent_streams = 0
server_stream = server.create_stream
request_headers = [[":method", "GET"], [":path", "/"], [":authority", "localhost"]]
server_stream.send_headers(request_headers)
expect {client.read_frame}.to raise_exception(Protocol::HTTP2::ProtocolError)
end
with "closed client" do
def before
client.close!
super
end
it "cannot receive settings" do
expect do
settings_frame = Protocol::HTTP2::SettingsFrame.new
client.receive_settings(settings_frame)
end.to raise_exception(Protocol::HTTP2::ProtocolError, message: be =~ /Cannot receive settings/)
end
it "cannot send ping" do
expect do
client.send_ping("Hello World!")
end.to raise_exception(Protocol::HTTP2::ProtocolError, message: be =~ /Cannot send ping/)
end
it "cannot receive ping" do
expect do
ping_frame = Protocol::HTTP2::PingFrame.new
client.receive_ping(ping_frame)
end.to raise_exception(Protocol::HTTP2::ProtocolError, message: be =~ /Cannot receive ping/)
end
end
end
|