|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Released under the MIT License. |
| 4 | +# Copyright, 2026, by Samuel Williams. |
| 5 | + |
| 6 | +require "async/http/client" |
| 7 | + |
| 8 | +class RetryClient < Async::HTTP::Client |
| 9 | + def initialize(failures) |
| 10 | + @pool = FakePool.new |
| 11 | + @protocol = nil |
| 12 | + @retries = 3 |
| 13 | + @scheme = "https" |
| 14 | + @authority = "example.com" |
| 15 | + @failures = failures |
| 16 | + @chunks = [] |
| 17 | + end |
| 18 | + |
| 19 | + attr :chunks |
| 20 | + |
| 21 | + class FakePool |
| 22 | + def acquire |
| 23 | + Object.new |
| 24 | + end |
| 25 | + |
| 26 | + def release(connection) |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + def make_response(request, connection, attempt) |
| 31 | + @chunks << request.body&.read |
| 32 | + |
| 33 | + if failure = @failures.shift |
| 34 | + raise failure |
| 35 | + end |
| 36 | + |
| 37 | + return Protocol::HTTP::Response[200, {}, ["OK"]] |
| 38 | + end |
| 39 | +end |
| 40 | + |
| 41 | +describe Async::HTTP::Client do |
| 42 | + with "retries" do |
| 43 | + it "rewinds idempotent request bodies after ambiguous failures" do |
| 44 | + client = RetryClient.new([EOFError.new]) |
| 45 | + request = Protocol::HTTP::Request["PUT", "/", {}, ["Hello"]] |
| 46 | + |
| 47 | + response = client.call(request) |
| 48 | + |
| 49 | + expect(response).to be(:success?) |
| 50 | + expect(client.chunks).to be == ["Hello", "Hello"] |
| 51 | + end |
| 52 | + |
| 53 | + it "rewinds non-idempotent request bodies after refused requests" do |
| 54 | + client = RetryClient.new([Protocol::HTTP::RefusedError.new("Request not processed.")]) |
| 55 | + request = Protocol::HTTP::Request["POST", "/", {}, ["Hello"]] |
| 56 | + |
| 57 | + response = client.call(request) |
| 58 | + |
| 59 | + expect(response).to be(:success?) |
| 60 | + expect(client.chunks).to be == ["Hello", "Hello"] |
| 61 | + end |
| 62 | + end |
| 63 | +end |
0 commit comments