Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'railslove/master'
Browse files Browse the repository at this point in the history
* railslove/master:
  Use leftmost match to inject head to avoid one line html bug
  version up
  Use local variables in rack middleware to prevent instance state changes (railslove#151)
  Make middleware thread safe
  • Loading branch information
jclusso committed Apr 17, 2021
2 parents 26872b1 + 0a09c94 commit 6af876f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 1.12.1

* [ENHANCEMENT] Use local variables to prevent instance state #151 (thx @bumi)
* [ENHANCEMENT] Make middleware thread safe #150 (thx @kspe)

# 1.12.0

* [ENHANCEMENT] Add support for Heap #147 (thx @mohanzhang)
Expand Down
2 changes: 1 addition & 1 deletion lib/rack/tracker/google_tag_manager/google_tag_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def inject(response)
# Sub! is enough, in well formed html there's only one head or body tag.
# Block syntax need to be used, otherwise backslashes in input will mess the output.
# @see http://stackoverflow.com/a/4149087/518204 and https://github.com/railslove/rack-tracker/issues/50
response.sub! %r{<head.*>} do |m|
response.sub! %r{<head.*?>} do |m|
m.to_s << self.render_head
end
response.sub! %r{<body.*?>} do |m|
Expand Down
2 changes: 1 addition & 1 deletion lib/rack/tracker/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Rack
class Tracker
VERSION = '1.12.0'
VERSION = '1.12.1'
end
end
23 changes: 23 additions & 0 deletions spec/handler/google_tag_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,27 @@ def env
end
end

describe '#inject' do
subject { handler_object.inject(example_response) }
let(:handler_object) { described_class.new(env, container: 'somebody') }

before do
allow(handler_object).to receive(:render_head).and_return('<script>"HEAD"</script>')
allow(handler_object).to receive(:render_body).and_return('<script>"BODY"</script>')
end

context 'with one line html response' do
let(:example_response) { "<html><head></head><body></body></html>" }

it 'will have render_head content in head tag' do
expect(subject).to match(%r{<head>.*<script>"HEAD"</script>.*</head>})
end

it 'will have render_body content in body tag' do
expect(subject).to match(%r{<body>.*<script>"BODY"</script>.*</body>})
end

end
end

end

0 comments on commit 6af876f

Please sign in to comment.