Skip to content

Commit

Permalink
Add test for capture_headers in context_builder
Browse files Browse the repository at this point in the history
  • Loading branch information
mheiligers-godaddy committed Mar 30, 2024
1 parent 5ead781 commit 098273e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/elastic_apm/context_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ def apply_to_request(context, rack_env:, for_type:)
request.body = should_capture_body?(for_type) ? get_body(req) : SKIPPED

headers, env = get_headers_and_env(rack_env)
request.headers = headers if config.capture_headers?
request.env = env if config.capture_env?

request.cookies = req.cookies.dup
unless request.cookies.empty?
request.headers['Cookie'] = SKIPPED if request.headers.has_key?('Cookie')

if config.capture_headers?
request.headers = headers
unless request.cookies.empty?
request.headers['Cookie'] = SKIPPED if request.headers.has_key?('Cookie')
end
end

context
Expand Down
15 changes: 15 additions & 0 deletions spec/elastic_apm/context_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@ module ElasticAPM
expect(result.request.body.valid_encoding?).to be true
end
end

context 'with capture_headers false' do
let(:config) { Config.new capture_headers: false }

it 'does not break on the cookies' do
env = Rack::MockRequest.env_for(
'/somewhere/in/there?q=yes',
method: 'POST',
'HTTP_COOKIE' => 'things=1'
)
env['HTTP_CONTENT_TYPE'] = 'application/json'

expect { subject.build(rack_env: env, for_type: :transaction) }.not_to raise_error
end
end
end
end
end

0 comments on commit 098273e

Please sign in to comment.