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

Move opaque id to the end of the middleware chain #291

Merged
merged 4 commits into from
Jan 9, 2024
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
3 changes: 2 additions & 1 deletion lib/elastomer_client/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ def connection
# Request compressed responses from ES and decompress them
conn.use(:gzip)
conn.request(:encode_json)
conn.request(:opaque_id) if @opaque_id
conn.request(:limit_size, max_request_size:) if max_request_size
conn.request(:elastomer_compress, compression:) if compress_body

Expand All @@ -149,6 +148,8 @@ def connection

@connection_block&.call(conn)

conn.request(:opaque_id) if @opaque_id
misalcedo marked this conversation as resolved.
Show resolved Hide resolved

if @adapter.is_a?(Array)
conn.adapter(*@adapter)
else
Expand Down
13 changes: 13 additions & 0 deletions test/client_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require File.expand_path("../test_helper", __FILE__)
require File.expand_path("../mock_response", __FILE__)
require "elastomer_client/notifications"

describe ElastomerClient::Client do
Expand Down Expand Up @@ -414,4 +415,16 @@
refute_same $client.connection, client.connection
end
end

it "does not throw OpaqueIdError for mocked response with empty opaque id" do
opts = $client_params.merge \
opaque_id: true
client = ElastomerClient::Client.new(**opts) do |connection|
connection.request(:mock_response) { |env| env.body = "{}" }
end

response = client.get("/")

assert_equal "yes", response.headers["Fake"]
end
end
30 changes: 30 additions & 0 deletions test/mock_response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module ElastomerClient
module Middleware
class MockResponse < Faraday::Middleware
def initialize(app, &block)
super(app)
@response_block = block
end

def call(env)
env.clear_body if env.needs_body?

env.status = 200
env.response_headers = ::Faraday::Utils::Headers.new
env.response_headers["Fake"] = "yes"
env.response = ::Faraday::Response.new

@response_block&.call(env)

env.response.finish(env) unless env.parallel?

env.response
end
end
end
end

Faraday::Request.register_middleware \
mock_response: ElastomerClient::Middleware::MockResponse