|
8 | 8 |
|
9 | 9 | require "protocol/http1/connection"
|
10 | 10 | require "protocol/http/body/buffered"
|
| 11 | +require "protocol/http/body/writable" |
11 | 12 |
|
12 | 13 | require "connection_context"
|
13 | 14 |
|
|
547 | 548 | end.to raise_exception(Protocol::HTTP1::BadHeader)
|
548 | 549 | end
|
549 | 550 | end
|
| 551 | + |
| 552 | + it "enters half-closed (local) state after writing response body" do |
| 553 | + expect(client).to be(:idle?) |
| 554 | + client.write_request("localhost", "GET", "/", "HTTP/1.1", {}) |
| 555 | + expect(client).to be(:open?) |
| 556 | + body = Protocol::HTTP::Body::Buffered.new(["Hello World"]) |
| 557 | + client.write_body("HTTP/1.1", body) |
| 558 | + expect(client).to be(:half_closed_local?) |
| 559 | + |
| 560 | + expect(server).to be(:idle?) |
| 561 | + request = server.read_request |
| 562 | + server.write_response("HTTP/1.1", 200, {}, nil) |
| 563 | + server.write_body("HTTP/1.1", nil) |
| 564 | + expect(server).to be(:half_closed_local?) |
| 565 | + end |
| 566 | + |
| 567 | + it "returns back to idle state" do |
| 568 | + expect(client).to be(:idle?) |
| 569 | + client.write_request("localhost", "GET", "/", "HTTP/1.1", {}) |
| 570 | + expect(client).to be(:open?) |
| 571 | + client.write_body("HTTP/1.1", nil) |
| 572 | + expect(client).to be(:half_closed_local?) |
| 573 | + |
| 574 | + expect(server).to be(:idle?) |
| 575 | + request = server.read_request |
| 576 | + expect(request).to be == ["localhost", "GET", "/", "HTTP/1.1", {}, nil] |
| 577 | + expect(server).to be(:half_closed_remote?) |
| 578 | + |
| 579 | + server.write_response("HTTP/1.1", 200, {}, []) |
| 580 | + server.write_body("HTTP/1.1", nil) |
| 581 | + expect(server).to be(:idle?) |
| 582 | + |
| 583 | + response = client.read_response("GET") |
| 584 | + expect(client).to be(:idle?) |
| 585 | + end |
| 586 | + |
| 587 | + it "transitions to the closed state when using connection: close response body" do |
| 588 | + expect(client).to be(:idle?) |
| 589 | + client.write_request("localhost", "GET", "/", "HTTP/1.0", {}) |
| 590 | + expect(client).to be(:open?) |
| 591 | + |
| 592 | + client.write_body("HTTP/1.0", nil) |
| 593 | + expect(client).to be(:half_closed_local?) |
| 594 | + |
| 595 | + expect(server).to be(:idle?) |
| 596 | + request = server.read_request |
| 597 | + expect(server).to be(:half_closed_remote?) |
| 598 | + |
| 599 | + server.write_response("HTTP/1.0", 200, {}, []) |
| 600 | + |
| 601 | + # Length is unknown, and HTTP/1.0 does not support chunked encoding, so this will close the connection: |
| 602 | + body = Protocol::HTTP::Body::Writable.new |
| 603 | + body.write "Hello World" |
| 604 | + body.close_write |
| 605 | + |
| 606 | + server.write_body("HTTP/1.0", body) |
| 607 | + expect(server).not.to be(:persistent) |
| 608 | + expect(server).to be(:closed?) |
| 609 | + |
| 610 | + response = client.read_response("GET") |
| 611 | + body = response.last |
| 612 | + expect(body.join).to be == "Hello World" |
| 613 | + expect(client).to be(:closed?) |
| 614 | + end |
| 615 | + |
| 616 | + it "can't write a request in the closed state" do |
| 617 | + client.state = :closed |
| 618 | + |
| 619 | + expect do |
| 620 | + client.write_request("localhost", "GET", "/", "HTTP/1.0", {}) |
| 621 | + end.to raise_exception(Protocol::HTTP1::ProtocolError) |
| 622 | + end |
| 623 | + |
| 624 | + it "can't read a response in the closed state" do |
| 625 | + client.state = :closed |
| 626 | + |
| 627 | + expect do |
| 628 | + client.read_response("GET") |
| 629 | + end.to raise_exception(Protocol::HTTP1::ProtocolError) |
| 630 | + end |
| 631 | + |
| 632 | + it "can't write a response in the closed state" do |
| 633 | + server.state = :closed |
| 634 | + |
| 635 | + expect do |
| 636 | + server.write_response("HTTP/1.0", 200, {}, nil) |
| 637 | + end.to raise_exception(Protocol::HTTP1::ProtocolError) |
| 638 | + end |
| 639 | + |
| 640 | + it "can't read a request in the closed state" do |
| 641 | + server.state = :closed |
| 642 | + |
| 643 | + expect do |
| 644 | + server.read_request |
| 645 | + end.to raise_exception(Protocol::HTTP1::ProtocolError) |
| 646 | + end |
| 647 | + |
| 648 | + it "can't enter the closed state from the idle state" do |
| 649 | + expect do |
| 650 | + client.closed! |
| 651 | + end.to raise_exception(Protocol::HTTP1::ProtocolError) |
| 652 | + end |
| 653 | + |
| 654 | + it "can't write response body without writing response" do |
| 655 | + expect do |
| 656 | + server.write_body("HTTP/1.0", nil) |
| 657 | + end.to raise_exception(Protocol::HTTP1::ProtocolError) |
| 658 | + end |
| 659 | + |
| 660 | + it "can't write request body without writing request" do |
| 661 | + expect do |
| 662 | + client.write_body("HTTP/1.0", nil) |
| 663 | + end.to raise_exception(Protocol::HTTP1::ProtocolError) |
| 664 | + end |
| 665 | + |
| 666 | + it "can't read request body without reading request" do |
| 667 | + # Fake empty chunked encoded body: |
| 668 | + client.stream.write("0\r\n\r\n") |
| 669 | + |
| 670 | + body = server.read_request_body("POST", {"transfer-encoding" => ["chunked"]}) |
| 671 | + |
| 672 | + expect(body).to be_a(Protocol::HTTP1::Body::Chunked) |
| 673 | + |
| 674 | + expect do |
| 675 | + body.join |
| 676 | + end.to raise_exception(Protocol::HTTP1::ProtocolError) |
| 677 | + end |
| 678 | + |
| 679 | + it "can't write interim response in the closed state" do |
| 680 | + server.state = :closed |
| 681 | + |
| 682 | + expect do |
| 683 | + server.write_interim_response("HTTP/1.0", 100, {}) |
| 684 | + end.to raise_exception(Protocol::HTTP1::ProtocolError) |
| 685 | + end |
550 | 686 | end
|
0 commit comments