From 5fc8227af24a494d81089b77f727f48e0e6de0d0 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Thu, 3 Oct 2024 14:40:59 +1300 Subject: [PATCH] Add test for basic post request. --- lib/async/http/protocol/http1/finishable.rb | 8 +++- test/async/http/client/post.rb | 45 +++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 test/async/http/client/post.rb diff --git a/lib/async/http/protocol/http1/finishable.rb b/lib/async/http/protocol/http1/finishable.rb index 26aee3f..a541065 100644 --- a/lib/async/http/protocol/http1/finishable.rb +++ b/lib/async/http/protocol/http1/finishable.rb @@ -15,6 +15,8 @@ class Finishable < ::Protocol::HTTP::Body::Wrapper def initialize(body) super(body) + $stderr.puts "Finishable#initialize: #{body.inspect}" + @closed = Async::Variable.new @error = nil @@ -28,12 +30,16 @@ def reading? def read @reading = true - super + super.tap do |chunk| + $stderr.puts "Finishable#read: #{chunk.inspect}" + end end def close(error = nil) super + $stderr.puts "Finishable#close: #{error.inspect}" + unless @closed.resolved? @error = error @closed.value = true diff --git a/test/async/http/client/post.rb b/test/async/http/client/post.rb new file mode 100644 index 0000000..7bb957d --- /dev/null +++ b/test/async/http/client/post.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +# Released under the MIT License. +# Copyright, 2018-2024, by Samuel Williams. + +require "sus/fixtures/async/http" + +APostRequest = Sus::Shared("a post request") do + include Sus::Fixtures::Async::HTTP::ServerContext + let(:protocol) {subject} + + let(:app) do + ::Protocol::HTTP::Middleware.for do |request| + ::Protocol::HTTP::Response[200, {}, request.body] + end + end + + it "can post a fixed length body" do + $stderr.puts "Connecting to server: #{subject}" + body = Protocol::HTTP::Body::Buffered.wrap(["Hello, World!"]) + + begin + response = client.post("/", body: body) + + $stderr.puts "Got response: #{response.inspect}" + + expect(response).to be(:success?) + expect(response.read).to be == "Hello, World!" + ensure + response&.finish + end + end +end + +describe Async::HTTP::Protocol::HTTP10 do + it_behaves_like APostRequest +end + +describe Async::HTTP::Protocol::HTTP11 do + it_behaves_like APostRequest +end + +describe Async::HTTP::Protocol::HTTP2 do + it_behaves_like APostRequest +end