From 15eab6a8521b2b931bb490c9b7bcb8e5fc4c869d Mon Sep 17 00:00:00 2001 From: Giancarlo Salamanca Date: Sat, 9 Apr 2016 18:00:17 +1000 Subject: [PATCH 1/2] Don't duplicate the character around snippets --- lib/rollbar/js/middleware.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rollbar/js/middleware.rb b/lib/rollbar/js/middleware.rb index e022dc24..0ad802d8 100644 --- a/lib/rollbar/js/middleware.rb +++ b/lib/rollbar/js/middleware.rb @@ -77,7 +77,7 @@ def add_js(response) return nil unless head_open_end if head_open_end - body = body[0..head_open_end] << + body = body[0..(head_open_end - 1)] << config_js_tag << snippet_js_tag << body[head_open_end..-1] From bb50214e957c6cd4d51d5f8ef3a1020c25f27c70 Mon Sep 17 00:00:00 2001 From: Jon de Andres Date: Tue, 19 Apr 2016 16:39:20 +0200 Subject: [PATCH 2/2] Change logic to add Rollbar.js snippet and test it --- lib/rollbar/js/middleware.rb | 6 +++--- spec/rollbar/js/middleware_spec.rb | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/lib/rollbar/js/middleware.rb b/lib/rollbar/js/middleware.rb index 0ad802d8..c406a87f 100644 --- a/lib/rollbar/js/middleware.rb +++ b/lib/rollbar/js/middleware.rb @@ -77,10 +77,10 @@ def add_js(response) return nil unless head_open_end if head_open_end - body = body[0..(head_open_end - 1)] << + body = body[0..head_open_end] << config_js_tag << snippet_js_tag << - body[head_open_end..-1] + body[head_open_end + 1..-1] end body @@ -91,7 +91,7 @@ def add_js(response) def find_end_of_head_open(body) head_open = body.index(/', head_open) + 1 if head_open + body.index('>', head_open) if head_open end def join_body(response) diff --git a/spec/rollbar/js/middleware_spec.rb b/spec/rollbar/js/middleware_spec.rb index 134a5982..b7242e72 100644 --- a/spec/rollbar/js/middleware_spec.rb +++ b/spec/rollbar/js/middleware_spec.rb @@ -22,6 +22,11 @@

Testing the middleware

+END + end + let(:minified_html) do + <<-END +

Testing the middleware

END end let(:snippet) { 'THIS IS THE SNIPPET' } @@ -65,6 +70,26 @@ res_status, res_headers, response = subject.call(env) new_body = response.body.join + expect(new_body).to_not include('>>') + expect(new_body).to include(snippet) + expect(new_body).to include(config[:options].to_json) + expect(res_status).to be_eql(status) + expect(res_headers['Content-Type']).to be_eql(content_type) + end + end + + context 'having a html 200 response with minified body' do + let(:body) { [minified_html] } + let(:status) { 200 } + let(:headers) do + { 'Content-Type' => content_type } + end + + it 'adds the config and the snippet to the response' do + res_status, res_headers, response = subject.call(env) + new_body = response.body.join + + expect(new_body).to_not include('>>') expect(new_body).to include(snippet) expect(new_body).to include(config[:options].to_json) expect(res_status).to be_eql(status)