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

Change opaque ID middleware to not throw OpaqueIDError on 5xx responses #293

Merged
merged 3 commits into from
Jan 24, 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/middleware/opaque_id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def call(env)

@app.call(env).on_complete do |renv|
response_uuid = renv[:response_headers][X_OPAQUE_ID]
if uuid != response_uuid
# Don't raise OpaqueIdError if the response is a 5xx
if !response_uuid.nil? && uuid != response_uuid && renv.status < 500
raise ::ElastomerClient::Client::OpaqueIdError,
"Conflicting 'X-Opaque-Id' headers: request #{uuid.inspect}, response #{response_uuid.inspect}"
end
Expand Down
49 changes: 42 additions & 7 deletions test/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -416,15 +416,50 @@
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 = "{}" }
describe "OpaqueIDError conditionals" do
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

it "throws OpaqueIdError on mismatched ID" do
client_params = $client_params.merge \
opaque_id: true
client = ElastomerClient::Client.new(**client_params)

test_url = "#{client.url}/"
stub_request(:get, test_url).and_return(status: 200, headers: { "Content-Type" => "application/json", "X-Opaque-Id" => "foo" })

assert_raises(ElastomerClient::Client::OpaqueIdError) { client.request :get, test_url, {} }
end

response = client.get("/")
it "throws OpaqueIdError on empty string ID" do
client_params = $client_params.merge \
opaque_id: true
client = ElastomerClient::Client.new(**client_params)

test_url = "#{client.url}/"
stub_request(:get, test_url).and_return(status: 200, headers: { "Content-Type" => "application/json", "X-Opaque-Id" => "" })

assert_raises(ElastomerClient::Client::OpaqueIdError) { client.request :get, test_url, {} }
end

assert_equal "yes", response.headers["Fake"]
it "throws ServerError and not OpaqueIdError on 5xx response and nil ID" do
client_params = $client_params.merge \
opaque_id: true
client = ElastomerClient::Client.new(**client_params)

test_url = "#{client.url}/"
stub_request(:get, test_url).and_return(status: 503, headers: { "Content-Type" => "application/json" })

assert_raises(ElastomerClient::Client::ServerError) { client.request :get, test_url, {} }
end
end
end