From 6f3a12f2cceb709455d6a044d0d3ea2cb9f774ea Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Sat, 31 Aug 2024 08:52:58 +1200 Subject: [PATCH] Add tests. --- guides/design-overview/readme.md | 10 +++++++-- test/protocol/http/request.rb | 35 ++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/guides/design-overview/readme.md b/guides/design-overview/readme.md index 2c477e1..e4be3a1 100644 --- a/guides/design-overview/readme.md +++ b/guides/design-overview/readme.md @@ -195,9 +195,15 @@ The value of this uni-directional flow is that it is natural for the stream to b Interim responses are responses that are sent before the final response. They are used for things like `103 Early Hints` and `100 Continue`. These responses are sent before the final response, and are used to signal to the client that the server is still processing the request. ```ruby +body = Body::Writable.new + interim_response_callback = proc do |status, headers| - puts "Interim Response: #{status}: #{headers}" + if status == 100 + # Continue sending the request body. + body.write("Hello World") + body.close + end end -response = client.call(request, interim_response: interim_response_callback) +response = client.post("/upload", {'expect' => '100-continue'}, body, interim_response: interim_response_callback) ``` diff --git a/test/protocol/http/request.rb b/test/protocol/http/request.rb index 3877520..9a61b49 100644 --- a/test/protocol/http/request.rb +++ b/test/protocol/http/request.rb @@ -70,4 +70,39 @@ request.call(connection) end end + + with "interim response" do + let(:request) {subject.new("http", "localhost", "GET", "/index.html", "HTTP/1.0", headers, body)} + + it "should call block" do + request.on_interim_response do |status, headers| + expect(status).to be == 100 + expect(headers).to be == {} + end + + request.send_interim_response(100, {}) + end + + it "calls multiple blocks" do + sequence = [] + + request.on_interim_response do |status, headers| + sequence << 1 + + expect(status).to be == 100 + expect(headers).to be == {} + end + + request.on_interim_response do |status, headers| + sequence << 2 + + expect(status).to be == 100 + expect(headers).to be == {} + end + + request.send_interim_response(100, {}) + + expect(sequence).to be == [2, 1] + end + end end