Skip to content

Commit

Permalink
Merge pull request #435 from rollbar/patch-js-middleware-and-test-it
Browse files Browse the repository at this point in the history
Patch js middleware and test it
  • Loading branch information
jondeandres committed Apr 19, 2016
2 parents 9d42d0e + bb50214 commit 2844e6a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/rollbar/js/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def add_js(response)
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
Expand All @@ -91,7 +91,7 @@ def add_js(response)

def find_end_of_head_open(body)
head_open = body.index(/<head\W/)
body.index('>', head_open) + 1 if head_open
body.index('>', head_open) if head_open
end

def join_body(response)
Expand Down
25 changes: 25 additions & 0 deletions spec/rollbar/js/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<h1>Testing the middleware</h1>
</body>
</html>
END
end
let(:minified_html) do
<<-END
<html><head><link rel="stylesheet" href="url" type="text/css" media="screen" /><script type="text/javascript" src="foo"></script></head><body><h1>Testing the middleware</h1></body></html>
END
end
let(:snippet) { 'THIS IS THE SNIPPET' }
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 2844e6a

Please sign in to comment.