diff --git a/lib/datadog/tracing/contrib/rack/middlewares.rb b/lib/datadog/tracing/contrib/rack/middlewares.rb index 0caa839848..550b5f8828 100644 --- a/lib/datadog/tracing/contrib/rack/middlewares.rb +++ b/lib/datadog/tracing/contrib/rack/middlewares.rb @@ -268,10 +268,12 @@ def parse_url(env, original_env) query_string.empty? ? path : "#{path}?#{query_string}" else - request_uri + # normally REQUEST_URI starts at the path, but it + # might contain the full URL in some cases (e.g WEBrick) + request_uri.sub(/^#{base_url}/, '') end - ::URI.join(base_url, fullpath).to_s + base_url + fullpath end def parse_user_agent_header(headers) diff --git a/spec/datadog/tracing/contrib/rack/middlewares_spec.rb b/spec/datadog/tracing/contrib/rack/middlewares_spec.rb index caf3284ef1..ea5b427615 100644 --- a/spec/datadog/tracing/contrib/rack/middlewares_spec.rb +++ b/spec/datadog/tracing/contrib/rack/middlewares_spec.rb @@ -44,4 +44,27 @@ end end end + + # Non-ASCII URLs cannot be tested with `rack-test` as of v2.0.2. + # It would be ideal if that was possible, as we could create integration tests + # for such cases. + # + # As an alternative, we test the parsing method directly. + describe '#parse_url' do + subject(:parse_url) { middleware.send(:parse_url, env, original_env) } + let(:env) { { 'REQUEST_URI' => uri, 'HTTP_HOST' => 'localhost:443', 'rack.url_scheme' => 'https' } } + let(:original_env) { {} } + + context 'with Unicode characters' do + let(:uri) { 'https://localhost/success/?繋がってて' } + + it { is_expected.to eq(uri) } + end + + context 'with unencoded ASCII characters' do + let(:uri) { 'https://localhost/success/|' } + + it { is_expected.to eq(uri) } + end + end end