Skip to content

Commit 098273e

Browse files
Add test for capture_headers in context_builder
1 parent 5ead781 commit 098273e

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

lib/elastic_apm/context_builder.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,14 @@ def apply_to_request(context, rack_env:, for_type:)
5151
request.body = should_capture_body?(for_type) ? get_body(req) : SKIPPED
5252

5353
headers, env = get_headers_and_env(rack_env)
54-
request.headers = headers if config.capture_headers?
5554
request.env = env if config.capture_env?
56-
5755
request.cookies = req.cookies.dup
58-
unless request.cookies.empty?
59-
request.headers['Cookie'] = SKIPPED if request.headers.has_key?('Cookie')
56+
57+
if config.capture_headers?
58+
request.headers = headers
59+
unless request.cookies.empty?
60+
request.headers['Cookie'] = SKIPPED if request.headers.has_key?('Cookie')
61+
end
6062
end
6163

6264
context

spec/elastic_apm/context_builder_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,21 @@ module ElasticAPM
115115
expect(result.request.body.valid_encoding?).to be true
116116
end
117117
end
118+
119+
context 'with capture_headers false' do
120+
let(:config) { Config.new capture_headers: false }
121+
122+
it 'does not break on the cookies' do
123+
env = Rack::MockRequest.env_for(
124+
'/somewhere/in/there?q=yes',
125+
method: 'POST',
126+
'HTTP_COOKIE' => 'things=1'
127+
)
128+
env['HTTP_CONTENT_TYPE'] = 'application/json'
129+
130+
expect { subject.build(rack_env: env, for_type: :transaction) }.not_to raise_error
131+
end
132+
end
118133
end
119134
end
120135
end

0 commit comments

Comments
 (0)