Skip to content

Commit

Permalink
Handle responses in the reverse order from the requests (#766)
Browse files Browse the repository at this point in the history
Co-authored-by: John Doe <[email protected]>
  • Loading branch information
souk4711 and John Doe authored Oct 14, 2023
1 parent 8b802bf commit 1ba37c2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/http/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def perform(req, options)
end
res = build_response(req, options)

res = options.features.inject(res) do |response, (_name, feature)|
res = options.features.values.reverse.inject(res) do |response, feature|
feature.wrap_response(response)
end

Expand Down
38 changes: 38 additions & 0 deletions spec/lib/http/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,44 @@ def on_error(request, error)
end.to raise_error(HTTP::ConnectTimeoutError)
expect(feature_instance.captured_error).to be_a(HTTP::ConnectTimeoutError)
end

it "handle responses in the reverse order from the requests" do
feature_class_order =
Class.new(HTTP::Feature) do
@order = []

class << self
attr_reader :order
end

def initialize(id:)
@id = id
end

def wrap_request(req)
self.class.order << "request.#{@id}"
req
end

def wrap_response(res)
self.class.order << "response.#{@id}"
res
end
end
feature_instance_a = feature_class_order.new(:id => "a")
feature_instance_b = feature_class_order.new(:id => "b")
feature_instance_c = feature_class_order.new(:id => "c")

client.use(
:test_feature_a => feature_instance_a,
:test_feature_b => feature_instance_b,
:test_feature_c => feature_instance_c
).request(:get, dummy.endpoint)

expect(feature_class_order.order).to eq(
["request.a", "request.b", "request.c", "response.c", "response.b", "response.a"]
)
end
end
end

Expand Down

0 comments on commit 1ba37c2

Please sign in to comment.