From 5322fda44f3c11762c1b4cfde917b9764021f47c Mon Sep 17 00:00:00 2001 From: Matheus Santana Date: Mon, 24 Mar 2025 16:17:40 -0300 Subject: [PATCH] Accept URLs with square brackets Closes #75 --- lib/rack_reverse_proxy/rule.rb | 6 ++--- spec/rack_reverse_proxy/rule_spec.rb | 39 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 spec/rack_reverse_proxy/rule_spec.rb diff --git a/lib/rack_reverse_proxy/rule.rb b/lib/rack_reverse_proxy/rule.rb index 0635482..4916909 100644 --- a/lib/rack_reverse_proxy/rule.rb +++ b/lib/rack_reverse_proxy/rule.rb @@ -93,11 +93,11 @@ def raw_uri end def just_uri - URI.parse(url) + Addressable::URI.parse(url) end def uri_with_path - URI.join(url, path) + Addressable::URI.join(url, path) end def evaluate(url) @@ -115,7 +115,7 @@ def with_substitutions? end def substitute_matches - URI(matches.substitute(url)) + Addressable::URI.parse(matches.substitute(url)) end end diff --git a/spec/rack_reverse_proxy/rule_spec.rb b/spec/rack_reverse_proxy/rule_spec.rb new file mode 100644 index 0000000..1e458cb --- /dev/null +++ b/spec/rack_reverse_proxy/rule_spec.rb @@ -0,0 +1,39 @@ +require "spec_helper" + +module RackReverseProxy + RSpec.describe Rule do + describe '#get_uri' do + context 'with path' do + it 'accepts URLs with square brackets' do + rule = RackReverseProxy::Rule.new('/test', 'http://example.com') + + uri = rule.get_uri('/test/[id]', {}, nil, nil) + + expect(uri.to_s).to eq 'http://example.com/test/[id]' + end + end + + context 'with substitutions' do + it 'accepts URLs with square brackets' do + rule = RackReverseProxy::Rule.new(%r{^/test/(.*)}, 'http://example.com/test/$1') + + uri = rule.get_uri('/test/[id]', {}, nil, nil) + + expect(uri.to_s).to eq 'http://example.com/test/[id]' + end + end + + context 'with custom url' do + it 'accepts URLs with square brackets' do + rule = RackReverseProxy::Rule.new(%r{^/test/(.*)}) + + expect_any_instance_of(RackReverseProxy::Rule::Matches).to receive(:custom_url).and_return('/test/[id]') + + uri = rule.get_uri('/test/[id]', {}, nil, nil) + + expect(uri.to_s).to eq '/test/[id]' + end + end + end + end +end