Skip to content

Commit

Permalink
request normalization context
Browse files Browse the repository at this point in the history
  • Loading branch information
dpep committed Nov 27, 2023
1 parent 39df27e commit 93d3442
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
8 changes: 6 additions & 2 deletions lib/network_resiliency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def mode=(mode)
@mode = mode
end

def normalize_request(adapter, request = nil, &block)
def normalize_request(adapter, request = nil, **context, &block)
unless ADAPTERS.include?(adapter)
raise ArgumentError, "invalid adapter: #{adapter}"
end
Expand All @@ -113,13 +113,17 @@ def normalize_request(adapter, request = nil, &block)
raise ArgumentError, "specify request or block, but not both"
end

if request.nil? && !context.empty?
raise ArgumentError, "can not speficy context without request"
end

@normalize_request ||= {}
@normalize_request[adapter] ||= []
@normalize_request[adapter] << block if block_given?

if request
@normalize_request[adapter].reduce(request) do |req, block|
block.call(req)
block.call(req, **context)
end
else
@normalize_request[adapter]
Expand Down
2 changes: 1 addition & 1 deletion lib/network_resiliency/adapter/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def patched?(instance = nil)

refine Net::HTTP do
def normalize_path(path)
NetworkResiliency.normalize_request(:http, path).gsub(
NetworkResiliency.normalize_request(:http, path, host: address).gsub(
Regexp.union(
NetworkResiliency::Adapter::HTTP::ID_REGEX,
NetworkResiliency::Adapter::HTTP::UUID_REGEX,
Expand Down
8 changes: 6 additions & 2 deletions spec/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -321,15 +321,19 @@
context "when NetworkResiliency configured with custom normalization" do
before do
NetworkResiliency.configure do |c|
c.normalize_request(:http) do |path|
c.normalize_request(:http) do |path, host:|
if host.include?("example")
path += "/example"
end

path.sub "foo", "bar"
end
end
end

let(:path) { "/foo/123" }

it { is_expected.to eq "/bar/x" }
it { is_expected.to eq "/bar/x/example" }
end
end
end
29 changes: 27 additions & 2 deletions spec/network_resiliency_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -660,18 +660,43 @@ def expect_enabled
expect(described_class.normalize_request(:http, path)).to eq path
end

context "when request context is utilized" do
let(:host) { "example.com" }

before do
described_class.normalize_request(:http) do |path, host:|
if host == "example.com"
path = "/example"
end
end
end

it "takes the context into account" do
res = described_class.normalize_request(:http, path, host: host)
expect(res).to eq "/example"
end

context "when context is specified without a request" do
it "fails" do
expect {
described_class.normalize_request(:http, host: host)
}.to raise_error(ArgumentError)
end
end
end

context "when an invalid adapter is specified" do
it "fails" do
expect {
described_class.normalize_request(:foo, path)
described_class.normalize_request(:foo)
}.to raise_error(ArgumentError)
end
end

context "when both a request and block are specified" do
it "fails" do
expect {
described_class.normalize_request(:http, path) { |p| p }
described_class.normalize_request(:http, path) { nil }
}.to raise_error(ArgumentError)
end
end
Expand Down

0 comments on commit 93d3442

Please sign in to comment.