|
632 | 632 | expect(body).to receive(:empty?).and_return(false) |
633 | 633 | expect(body).to receive(:length).and_return(nil) |
634 | 634 |
|
| 635 | + # Only a response body may be delimited by closing the connection: |
635 | 636 | expect(server).to receive(:write_body_and_close) |
636 | 637 | server.write_body("HTTP/1.0", body) |
637 | 638 | end |
| 639 | + |
| 640 | + it "refuses to write closed body for a request" do |
| 641 | + expect(body).to receive(:empty?).and_return(false) |
| 642 | + expect(body).to receive(:length).and_return(nil) |
| 643 | + expect(body).to receive(:close) |
| 644 | + |
| 645 | + # A request body cannot be delimited by closing the connection, so nothing should be written: |
| 646 | + expect do |
| 647 | + server.write_body("HTTP/1.0", body, false, nil, true) |
| 648 | + end.to raise_exception(Protocol::HTTP1::ProtocolError, message: be =~ /unknown length/) |
| 649 | + |
| 650 | + expect(server).to be(:open?) |
| 651 | + server.close |
| 652 | + expect(client.stream.read).to be == "" |
| 653 | + end |
| 654 | + |
| 655 | + it "can write chunked body for a request" do |
| 656 | + expect(body).to receive(:empty?).and_return(false) |
| 657 | + expect(body).to receive(:length).and_return(nil) |
| 658 | + |
| 659 | + expect(server).to receive(:write_chunked_body) |
| 660 | + server.write_body("HTTP/1.1", body, false, nil, true) |
| 661 | + end |
638 | 662 | end |
639 | 663 |
|
640 | 664 | with "bad requests" do |
|
739 | 763 | expect(client).to be(:closed?) |
740 | 764 | end |
741 | 765 |
|
| 766 | + with "HTTP/1.0 request body" do |
| 767 | + it "can write a request body of known length" do |
| 768 | + client.write_request("localhost", "POST", "/", "HTTP/1.0", {"connection" => "keep-alive"}) |
| 769 | + |
| 770 | + body = Protocol::HTTP::Body::Buffered.new(["Hello World"]) |
| 771 | + client.write_body("HTTP/1.0", body, false, nil, true) |
| 772 | + expect(client).to be(:half_closed_local?) |
| 773 | + |
| 774 | + authority, method, path, version, headers, body = server.read_request |
| 775 | + expect(method).to be == "POST" |
| 776 | + expect(body).to be_a(Protocol::HTTP1::Body::Fixed) |
| 777 | + expect(body.join).to be == "Hello World" |
| 778 | + expect(server).to be(:persistent) |
| 779 | + end |
| 780 | + |
| 781 | + it "can write an empty request body of unknown length" do |
| 782 | + client.write_request("localhost", "POST", "/", "HTTP/1.0", {}) |
| 783 | + |
| 784 | + body = Protocol::HTTP::Body::Writable.new |
| 785 | + body.close_write |
| 786 | + |
| 787 | + client.write_body("HTTP/1.0", body, false, nil, true) |
| 788 | + expect(client).to be(:half_closed_local?) |
| 789 | + |
| 790 | + authority, method, path, version, headers, body = server.read_request |
| 791 | + expect(body).to be_nil |
| 792 | + expect(server).to be(:persistent) |
| 793 | + end |
| 794 | + |
| 795 | + it "refuses to write a request body of unknown length" do |
| 796 | + client.write_request("localhost", "POST", "/", "HTTP/1.0", {"connection" => "keep-alive"}) |
| 797 | + |
| 798 | + # Without a content length, the server would treat these bytes as the start of the next request: |
| 799 | + body = Protocol::HTTP::Body::Writable.new |
| 800 | + body.write "GET /other HTTP/1.1\r\nhost: localhost\r\n\r\n" |
| 801 | + body.close_write |
| 802 | + |
| 803 | + expect do |
| 804 | + client.write_body("HTTP/1.0", body, false, nil, true) |
| 805 | + end.to raise_exception(Protocol::HTTP1::ProtocolError, message: be =~ /unknown length/) |
| 806 | + |
| 807 | + expect(body).to be(:closed?) |
| 808 | + |
| 809 | + # The connection is still open, so the caller is responsible for closing it: |
| 810 | + expect(client).to be(:open?) |
| 811 | + client.close |
| 812 | + |
| 813 | + # Only the request head was written; none of the body bytes reached the server: |
| 814 | + expect(server.stream.read).to be == "POST / HTTP/1.0\r\nhost: localhost\r\nconnection: keep-alive\r\n" |
| 815 | + end |
| 816 | + end |
| 817 | + |
742 | 818 | it "raises RefusedError when the stream is broken" do |
743 | 819 | client.stream.close |
744 | 820 |
|
|
0 commit comments