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 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
|
# frozen_string_literal: true
require "helper"
RSpec.describe HTTP2::Stream do
include FrameHelpers
let(:f) { Framer.new }
let(:client) { Client.new }
let(:stream) { client.new_stream }
context "stream states" do
it "should initiliaze all streams to IDLE" do
expect(stream.state).to eq :idle
end
it "should set custom stream priority" do
stream = client.new_stream(weight: 3, dependency: 2, exclusive: true)
expect(stream.weight).to eq 3
end
context "idle" do
it "should transition to open on sent HEADERS" do
stream.send headers_frame
expect(stream.state).to eq :open
end
it "should transition to open on received HEADERS" do
stream.receive headers_frame
expect(stream.state).to eq :open
end
it "should transition to reserved (local) on sent PUSH_PROMISE" do
stream.send push_promise_frame
expect(stream.state).to eq :reserved_local
end
it "should transition to reserved (remote) on received PUSH_PROMISE" do
stream.receive push_promise_frame
expect(stream.state).to eq :reserved_remote
end
it "should reprioritize stream on sent PRIORITY" do
expect { stream.send priority_frame }.to_not raise_error
expect(stream.weight).to eq 20
end
it "should reprioritize stream on received PRIORITY" do
expect { stream.send priority_frame }.to_not raise_error
expect(stream.weight).to eq 20
end
end
context "reserved (local)" do
before { stream.send push_promise_frame }
it "should transition on sent PUSH_PROMISE" do
expect(stream.state).to eq :reserved_local
end
it "should allow HEADERS to be sent" do
expect { stream.send headers_frame }.to_not raise_error
end
it "should raise error if sending invalid frames" do
frame_types.reject { |frame| %i[headers rst_stream priority].include?(frame[:type]) }.each do |type|
expect { stream.dup.send type }.to raise_error InternalError
end
end
it "should raise error on receipt of invalid frames" do
what_types = frame_types.reject { |frame| %i[priority window_update rst_stream].include?(frame[:type]) }
what_types.each do |type|
expect { stream.dup.receive type }.to raise_error InternalError
end
end
it "should transition to half closed (remote) on sent HEADERS" do
stream.send headers_frame
expect(stream.state).to eq :half_closed_remote
end
it "should transition to closed on sent RST_STREAM" do
stream.close
expect(stream.state).to eq :closed
end
it "should transition to closed on received RST_STREAM" do
stream.receive rst_stream_frame
expect(stream.state).to eq :closed
end
it "should reprioritize stream on PRIORITY" do
expect { stream.receive priority_frame }.to_not raise_error
expect(stream.weight).to eq 20
end
it "should increment remote_window on received WINDOW_UPDATE" do
expect { stream.receive window_update_frame }.to_not raise_error
expect(stream.remote_window).to eq DEFAULT_FLOW_WINDOW + window_update_frame[:increment]
end
it "should not increment connection remote_window on received WINDOW_UPDATE" do
expect { stream.receive window_update_frame }.to_not raise_error
expect(client.remote_window).to eq DEFAULT_FLOW_WINDOW
end
end
context "reserved (remote)" do
before { stream.receive push_promise_frame }
it "should transition on received PUSH_PROMISE" do
expect(stream.state).to eq :reserved_remote
end
it "should raise error if sending invalid frames" do
frame_types.reject { |frame| %i[priority rst_stream window_update].include?(frame[:type]) }.each do |type|
expect { stream.dup.send type }.to raise_error InternalError
end
end
it "should raise error on receipt of invalid frames" do
frame_types.reject { |frame| %i[headers rst_stream priority].include?(frame[:type]) }.each do |type|
expect { stream.dup.receive type }.to raise_error InternalError
end
end
it "should transition to half closed (local) on received HEADERS" do
stream.receive headers_frame
expect(stream.state).to eq :half_closed_local
end
it "should transition to closed on sent RST_STREAM" do
stream.close
expect(stream.state).to eq :closed
end
it "should transition to closed on received RST_STREAM" do
stream.receive rst_stream_frame
expect(stream.state).to eq :closed
end
it "should reprioritize stream on PRIORITY" do
expect { stream.send priority_frame }.to_not raise_error
expect(stream.weight).to eq 20
end
it "should increment local_window on sent WINDOW_UPDATE" do
expect { stream.send window_update_frame }.to_not raise_error
expect(stream.local_window).to eq DEFAULT_FLOW_WINDOW + window_update_frame[:increment]
end
end
context "open" do
before { stream.receive headers_frame }
it "should allow any valid frames types to be sent" do
(frame_types - [ping_frame, goaway_frame, settings_frame]).each do |type|
expect { stream.dup.send type }.to_not raise_error
end
end
it "should allow frames of any type to be received" do
frame_types.each do |type|
expect { stream.dup.receive type }.to_not raise_error
end
end
it "should transition to half closed (local) if sending END_STREAM" do
[data_frame, headers_frame].each do |frame|
s = stream.dup
s.send frame.merge(flags: [:end_stream])
expect(s.state).to eq :half_closed_local
end
end
it "should transition to half closed (remote) if receiving END_STREAM" do
[data_frame, headers_frame].each do |frame|
s = stream.dup
f = frame.dup
f[:flags] = [:end_stream]
s.receive f
expect(s.state).to eq :half_closed_remote
end
end
it "should transition to half closed if remote opened with END_STREAM" do
s = client.new_stream
hclose = headers_frame
hclose[:flags] = [:end_stream]
s.receive hclose
expect(s.state).to eq :half_closed_remote
end
it "should transition to half closed if local opened with END_STREAM" do
s = client.new_stream
hclose = headers_frame
hclose[:flags] = [:end_stream]
s.send hclose
expect(s.state).to eq :half_closed_local
end
it "should transition to closed if sending RST_STREAM" do
stream.close
expect(stream.state).to eq :closed
expect(stream).to be_closed
end
it "should transition to closed if receiving RST_STREAM" do
stream.receive rst_stream_frame
expect(stream.state).to eq :closed
end
it "should emit :active on open transition" do
openp = false
openr = false
sp = client.new_stream
sr = client.new_stream
sp.on(:active) { openp = true }
sr.on(:active) { openr = true }
sp.receive headers_frame
sr.send headers_frame
expect(openp).to be_truthy
expect(openr).to be_truthy
end
it "should not emit :active on transition from open" do
order = []
stream = client.new_stream
stream.on(:active) { order << :active }
stream.on(:half_close) { order << :half_close }
stream.on(:close) { order << :close }
req = headers_frame
req[:flags] = [:end_headers]
stream.send req
stream.send data_frame
expect(order).to eq %i[active half_close]
end
it "should emit :close on close transition" do
closep = false
closer = false
sp = stream.dup
sr = stream.dup
sp.on(:close) { closep = true }
sr.on(:close) { closer = true }
sp.receive rst_stream_frame
sr.close
expect(closep).to be_truthy
expect(closer).to be_truthy
end
it "should emit :close after frame is processed" do
order = []
stream = client.new_stream
stream.on(:active) { order << :active }
stream.on(:data) { order << :data }
stream.on(:half_close) { order << :half_close }
stream.on(:close) { order << :close }
req = headers_frame
req[:flags] = %i[end_stream end_headers]
stream.send req
stream.receive headers_frame
stream.receive data_frame
expect(order).to eq %i[active half_close data close]
end
it "should emit :close with reason" do
reason = nil
stream.on(:close) { |r| reason = r }
stream.receive rst_stream_frame
expect(reason).not_to be_nil
end
it "should reprioritize stream on sent PRIORITY" do
expect { stream.send priority_frame }.to_not raise_error
expect(stream.weight).to eq 20
end
it "should reprioritize stream on received PRIORITY" do
expect { stream.receive priority_frame }.to_not raise_error
expect(stream.weight).to eq 20
end
end
context "half closed (local)" do
before { stream.send headers_frame.merge(flags: %i[end_headers end_stream]) }
it "should raise error on attempt to send invalid frames" do
frame_types.reject { |frame| %i[priority rst_stream window_update].include?(frame[:type]) }.each do |frame|
expect { stream.dup.send frame }.to raise_error InternalError
end
end
it "should transition to closed on receipt of END_STREAM flag" do
[data_frame, headers_frame, continuation_frame].each do |frame|
s = stream.dup
f = frame.dup
f[:flags] = [:end_stream]
s.receive f
expect(s.state).to eq :closed
end
end
it "should transition to closed on receipt of RST_STREAM frame" do
stream.receive rst_stream_frame
expect(stream.state).to eq :closed
end
it "should transition to closed if RST_STREAM frame is sent" do
stream.send rst_stream_frame
expect(stream.state).to eq :closed
end
it "should ignore received WINDOW_UPDATE frames" do
expect { stream.receive window_update_frame }.to_not raise_error
expect(stream.state).to eq :half_closed_local
end
it "should ignore received PRIORITY frames" do
expect { stream.receive priority_frame }.to_not raise_error
expect(stream.state).to eq :half_closed_local
end
it "should reprioritize stream on sent PRIORITY" do
expect { stream.send priority_frame }.to_not raise_error
expect(stream.weight).to eq 20
end
it "should reprioritize stream (and decendants) on received PRIORITY" do
expect { stream.receive priority_frame }.to_not raise_error
expect(stream.weight).to eq 20
end
it "should increment local_window on sent WINDOW_UPDATE" do
expect { stream.send window_update_frame }.to_not raise_error
expect(stream.local_window).to eq DEFAULT_FLOW_WINDOW + window_update_frame[:increment]
end
it "should emit :half_close event on transition" do
order = []
stream = client.new_stream
stream.on(:active) { order << :active }
stream.on(:half_close) { order << :half_close }
req = headers_frame
req[:flags] = %i[end_stream end_headers]
stream.send req
expect(order).to eq %i[active half_close]
end
it "should emit :close event on transition to closed" do
closed = false
stream.on(:close) { closed = true }
stream.receive rst_stream_frame
expect(stream.state).to eq :closed
expect(closed).to be_truthy
end
end
context "half closed (remote)" do
before { stream.receive headers_frame.merge(flags: %i[end_headers end_stream]) }
it "should raise STREAM_CLOSED error on reciept of frames" do
(frame_types - [priority_frame, rst_stream_frame, window_update_frame]).each do |frame|
expect do
stream.dup.receive frame
end.to raise_error(StreamClosed)
end
end
it "should transition to closed if END_STREAM flag is sent" do
[data_frame, headers_frame].each do |frame|
s = stream.dup
s.on(:close) { expect(s.state).to eq :closed }
s.send frame.merge(flags: [:end_stream])
expect(s.state).to eq :closed
end
end
it "should not transition to closed if END_STREAM flag is sent when overflowing window" do
stream.on(:close) { raise "should not have closed" }
data = { type: :data, flags: [], stream: stream.id }
4.times do
data = data.merge(flags: [:end_stream]) if stream.remote_window < 16_384
stream.send data.merge(payload: "x" * 16_384)
end
end
it "should transition to closed when send buffer is emptied" do
o = Object.new
expect(o).to receive(:tap).once
stream.on(:close) do
expect(stream.buffered_amount).to eq 0
o.tap
end
data = { type: :data, flags: [], stream: stream.id }
4.times do
data = data.merge(flags: [:end_stream]) if stream.remote_window < 16_384
stream.send data.merge(payload: "x" * 16_384)
end
client << f.generate(settings_frame)
client << Framer.new.generate(type: :window_update, stream: stream.id, increment: 16_384)
end
it "should transition to closed if RST_STREAM is sent" do
stream.close
expect(stream.state).to eq :closed
end
it "should transition to closed on reciept of RST_STREAM frame" do
stream.receive rst_stream_frame
expect(stream.state).to eq :closed
end
it "should ignore sent WINDOW_UPDATE frames" do
expect { stream.send window_update_frame }.to_not raise_error
expect(stream.state).to eq :half_closed_remote
end
it "should increment remote_window on received WINDOW_UPDATE" do
expect { stream.receive window_update_frame }.to_not raise_error
expect(stream.remote_window).to eq DEFAULT_FLOW_WINDOW + window_update_frame[:increment]
end
it "should reprioritize stream on sent PRIORITY" do
expect { stream.send priority_frame }.to_not raise_error
expect(stream.weight).to eq 20
end
it "should reprioritize stream on received PRIORITY" do
expect { stream.receive priority_frame }.to_not raise_error
expect(stream.weight).to eq 20
end
it "should emit :half_close event on transition" do
order = []
stream = client.new_stream
stream.on(:active) { order << :active }
stream.on(:half_close) { order << :half_close }
req = headers_frame
req[:flags] = %i[end_stream end_headers]
stream.receive req
expect(order).to eq %i[active half_close]
end
it "should emit :close event on close transition" do
closed = false
stream.on(:close) { closed = true }
stream.close
expect(stream.state).to eq :closed
expect(closed).to be_truthy
end
end
context "closed" do
context "remote closed stream" do
before do
stream.send headers_frame.merge(flags: %i[end_headers end_stream]) # half closed local
stream.receive headers_frame.merge(flags: %i[end_headers end_stream]) # closed by remote
end
it "should raise STREAM_CLOSED on attempt to send frames" do
(frame_types - [priority_frame, rst_stream_frame]).each do |frame|
expect do
stream.dup.send frame
end.to raise_error(StreamClosed)
end
end
it "should raise STREAM_CLOSED on receipt of frame" do
(frame_types - [priority_frame, rst_stream_frame, window_update_frame]).each do |frame|
expect do
stream.dup.receive frame
end.to raise_error(StreamClosed)
end
end
it "should allow PRIORITY, RST_STREAM to be sent" do
expect { stream.send priority_frame }.to_not raise_error
expect { stream.send rst_stream_frame }.to_not raise_error
end
it "should allow PRIORITY, RST_STREAM to be received" do
expect { stream.receive priority_frame }.to_not raise_error
expect { stream.receive rst_stream_frame }.to_not raise_error
end
it "should reprioritize stream on sent PRIORITY" do
expect { stream.send priority_frame }.to_not raise_error
expect(stream.weight).to eq 20
end
it "should reprioritize stream on received PRIORITY" do
expect { stream.receive priority_frame }.to_not raise_error
expect(stream.weight).to eq 20
end
it "should ignore received WINDOW_UPDATE frames" do
expect { stream.receive window_update_frame }.to_not raise_error
expect(stream.state).to eq :closed
end
end
context "local closed via RST_STREAM frame" do
before do
stream.send headers_frame # open
stream.send rst_stream_frame # closed by local
end
it "should ignore received frames" do
control_frames.each do |frame|
expect do
cb = []
stream.on(:data) { cb << :data }
stream.on(:headers) { cb << :headers }
stream.dup.receive frame
expect(cb).to be_empty
end.to_not raise_error
end
end
# it "should transition to reserved remote on PUSH_PROMISE" do
# An endpoint might receive a PUSH_PROMISE frame after it sends
# RST_STREAM. PUSH_PROMISE causes a stream to become "reserved".
# ...
# We're auto RST'ing PUSH streams in connection class, hence
# skipping this transition for now.
# end
end
# FIXME: Isn't this test same as "half closed (local)"?
# context "local closed via END_STREAM flag" do
# before(:each) do
# stream.send headers_frame # open
# stream.send data_frame # contains end_stream flag
# end
# it "should ignore received frames" do
# frame_types.each do |frame|
# expect { stream.dup.receive frame }.to_not raise_error
# end
# end
# end
end
end
# TODO: add test cases to ensure on(:priority) emitted after close
context "flow control" do
it "should initialize to default flow control window" do
expect(stream.remote_window).to eq DEFAULT_FLOW_WINDOW
end
it "should update window size on DATA frames only" do
stream.send headers_frame # go to open
expect(stream.remote_window).to eq DEFAULT_FLOW_WINDOW
(frame_types - [data_frame, ping_frame, goaway_frame, settings_frame]).each do |frame|
s = stream.dup
s.send frame
expect(s.remote_window).to eq DEFAULT_FLOW_WINDOW
end
stream.send data_frame
expect(stream.remote_window).to eq DEFAULT_FLOW_WINDOW - data_frame[:payload].bytesize
end
it "should update window size on receipt of WINDOW_UPDATE" do
stream.send headers_frame
stream.send data_frame
stream.receive window_update_frame
expect(stream.remote_window).to eq(
DEFAULT_FLOW_WINDOW - data_frame[:payload].bytesize + window_update_frame[:increment]
)
end
it "should observe session flow control" do
settings = settings_frame
data = data_frame
settings[:payload] = [[:settings_initial_window_size, 1000]]
settings[:stream] = 0
framer = Framer.new
client << framer.generate(settings)
s1 = client.new_stream
s1.send headers_frame
s1.send data.merge(payload: "x" * 900, flags: [])
expect(s1.remote_window).to eq 100
s1.send data.merge(payload: "x" * 200)
expect(s1.remote_window).to eq 0
expect(s1.buffered_amount).to eq 100
client << framer.generate(window_update_frame.merge(stream: s1.id, increment: 1000))
expect(s1.buffered_amount).to eq 0
expect(s1.remote_window).to eq 900
end
it "should not update window when data received is less than half of maximum local window size" do
data = data_frame
datalen = data[:payload].bytesize
expect(stream).not_to receive(:send) do |frame|
expect(frame[:type]).to eq :window_update
expect(frame[:increment]).to eq datalen
end
stream.receive headers_frame
stream.receive data
end
it "should update window when data received is over half of the maximum local window size" do
data1 = data_frame.merge(payload: "a" * 16_384, flags: [])
data2 = data_frame.merge(payload: "a" * 16_384)
datalen = 16_384 * 2
expect(stream).to receive(:send) do |frame|
expect(frame[:type]).to eq :window_update
expect(frame[:increment]).to eq datalen
end
stream.receive headers_frame
stream.receive data1
stream.receive data2
end
it "should send a DATA frame with size 0 when there is no window, as long as end_stream flag is set" do
settings = settings_frame
data = data_frame
settings[:payload] = [[:settings_initial_window_size, 1000]]
settings[:stream] = 0
framer = Framer.new
client << framer.generate(settings)
s1 = client.new_stream
s1.send headers_frame
s1.send data.merge(payload: "x" * 1000, flags: [])
# check if window is exhausted
expect(s1.remote_window).to be(0)
expect(s1.send_buffer).to be_empty
s1.send data.merge(payload: "", flags: [:end_stream])
expect(s1.remote_window).to be(0)
expect(s1.send_buffer).to be_empty
end
end
context "client API" do
it ".reprioritize should emit PRIORITY frame" do
expect(stream).to receive(:send) do |frame|
expect(frame[:type]).to eq :priority
expect(frame[:weight]).to eq 30
end
stream.reprioritize weight: 30
end
it ".reprioritize should raise error if invoked by server" do
srv = Server.new
stream = srv.new_stream
expect { stream.reprioritize(weight: 10) }.to raise_error(InternalError)
end
it ".headers should emit HEADERS frames" do
payload = {
":method" => "GET",
":scheme" => "http",
":host" => "www.example.org",
":path" => "/resource",
"custom" => "value"
}
expect(stream).to receive(:send) do |frame|
expect(frame[:type]).to eq :headers
expect(frame[:payload]).to eq payload
expect(frame[:flags]).to eq [:end_headers]
end
stream.headers(payload, end_stream: false, end_headers: true)
end
it ".data should emit DATA frames" do
expect(stream).to receive(:send) do |frame|
expect(frame[:type]).to eq :data
expect(frame[:payload]).to eq "text"
expect(frame[:flags]).to be_empty
end
stream.data("text", end_stream: false)
expect(stream).to receive(:send) do |frame|
expect(frame[:flags]).to eq [:end_stream]
end
stream.data("text")
end
it ".data should split large DATA frames" do
data = "x" * 16_384 * 2
want = [
{ type: :data, flags: [], length: 16_384 },
{ type: :data, flags: [], length: 16_384 },
{ type: :data, flags: [:end_stream], length: 1 }
]
want.each do |w|
expect(stream).to receive(:send) do |frame|
expect(frame[:type]).to eq w[:type]
expect(frame[:flags]).to eq w[:flags]
expect(frame[:payload].bytesize).to eq w[:length]
end
end
stream.data("#{data}x")
end
it ".data should split large multibyte DATA frames" do
data = "🐼" * 16_384
want = [
{ type: :data, flags: [], length: 16_384 },
{ type: :data, flags: [], length: 16_384 },
{ type: :data, flags: [], length: 16_384 },
{ type: :data, flags: [], length: 16_384 },
{ type: :data, flags: [:end_stream], length: 1 }
]
want.each do |w|
expect(stream).to receive(:send) do |frame|
expect(frame[:type]).to eq w[:type]
expect(frame[:flags]).to eq w[:flags]
expect(frame[:payload].bytesize).to eq w[:length]
end
end
stream.data("#{data}x")
end
it ".cancel should reset stream with cancel error code" do
expect(stream).to receive(:send) do |frame|
expect(frame[:type]).to eq :rst_stream
expect(frame[:error]).to eq :cancel
end
stream.cancel
end
it ".refuse should reset stream with refused stream error code" do
expect(stream).to receive(:send) do |frame|
expect(frame[:type]).to eq :rst_stream
expect(frame[:error]).to eq :refused_stream
end
stream.refuse
end
it ".window_update should emit WINDOW_UPDATE frames" do
expect(stream).to receive(:send) do |frame|
expect(frame[:type]).to eq :window_update
expect(frame[:increment]).to eq 20
end
stream.window_update(20)
end
end
context "server API" do
let(:srv) { Server.new }
let(:frm) { Framer.new }
let(:client_stream) { client.new_stream }
before do
client.on(:frame) { |bytes| srv << bytes }
end
it "should emit received headers via on(:headers)" do
headers = REQUEST_HEADERS
recv = nil
srv.on(:stream) do |stream|
stream.on(:headers) { |h| recv = h }
end
client_stream.headers(headers)
expect(recv).to eq headers
end
it "should emit received payload via on(:data)" do
payload = "some-payload"
srv.on(:stream) do |stream|
stream.on(:data) do |recv|
expect(recv).to eq payload
end
end
client_stream.headers(REQUEST_HEADERS)
client_stream.data(payload)
end
it "should emit received priority parameters via on(:priority)" do
new_weight = 15
new_dependency = client_stream.id + 2
callback_called = false
srv.on(:stream) do |stream|
stream.on(:priority) do |pri|
callback_called = true
expect(pri.is_a?(Hash)).to be
expect(pri[:weight]).to eq new_weight
expect(pri[:dependency]).to eq new_dependency
end
end
client_stream.headers(REQUEST_HEADERS)
client_stream.reprioritize(weight: new_weight, dependency: new_dependency)
expect(callback_called).to be
end
context "push" do
before(:each) do
srv.on(:frame) { |bytes| client << bytes }
srv.on(:stream) do |stream|
@server_stream = stream
end
client_stream.headers(REQUEST_HEADERS)
end
it ".promise should emit server initiated stream" do
push = nil
@server_stream.promise(REQUEST_HEADERS) { |pstream| push = pstream }
expect(push.id).to eq 2
end
it ".promise push stream should have parent stream" do
push = nil
@server_stream.promise(REQUEST_HEADERS) { |pstream| push = pstream }
expect(push.state).to eq :reserved_local
expect(push.parent.id).to eq @server_stream.id
end
context "stream states" do
it "server: active > half close > close" do
order = []
@server_stream.promise(REQUEST_HEADERS) do |push|
stream = push
expect(push.state).to eq :reserved_local
order << :reserved
push.on(:active) { order << :active }
push.on(:half_close) { order << :half_close }
push.on(:close) { order << :close }
push.headers(RESPONSE_HEADERS)
push.send data_frame.merge(stream: stream.id)
end
expect(order).to eq %i[reserved active half_close close]
end
it "client: promise_headers > active > headers > .. > data > close" do
order = []
headers = []
promise_headers = []
client.on(:promise) do |push|
order << :reserved
push.on(:active) { order << :active }
push.on(:data) { order << :data }
push.on(:half_close) { order << :half_close }
push.on(:close) { order << :close }
push.on(:promise_headers) do |h|
order << :promise_headers
promise_headers += h
end
push.on(:headers) do |h|
order << :headers
headers += h
end
expect(push.id).to be_even
end
@server_stream.promise(REQUEST_HEADERS) do |push|
push.headers(RESPONSE_HEADERS)
push.data("somedata")
end
expect(promise_headers).to eq(REQUEST_HEADERS)
expect(headers).to eq(RESPONSE_HEADERS)
expect(order).to eq %i[
reserved
promise_headers
active
headers
half_close
data
close
]
end
end
end
end
end
|