Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle responses in the reverse order from the requests #766

Merged
merged 1 commit into from Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading