require 'spec_helper'
require 'rollbar/middleware/js'
describe Rollbar::Middleware::Js do
subject { described_class.new(app, config) }
let(:env) { {} }
let(:config) { {} }
let(:app) do
proc do |_|
[status, headers, body]
end
end
let(:html) do
<<-END
Testing the middleware
END
end
let(:minified_html) do
<<-END
Testing the middleware
END
end
let(:meta_charset_html) do
<<-END
Testing the middleware
END
end
let(:meta_content_html) do
<<-END
Testing the middleware
END
end
let(:snippet) { 'THIS IS THE SNIPPET' }
let(:content_type) { 'text/html' }
before do
reconfigure_notifier
allow(subject).to receive(:js_snippet).and_return(snippet)
end
shared_examples "doesn't add the snippet or config", :add_js => false do
it "doesn't add the snippet or config" do
res_status, res_headers, response = subject.call(env)
new_body = response.join
expect(new_body).not_to include(snippet)
expect(new_body).not_to include(config[:options].to_json)
expect(new_body).to be_eql(body.join)
expect(res_status).to be_eql(status)
expect(res_headers['Content-Type']).to be_eql(content_type)
end
end
describe '#call' do
context 'with enabled config' do
let(:config) do
{
:enabled => true,
:options => { :foo => :bar }
}
end
context 'having a html 200 response' do
let(:body) { [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)
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)
expect(res_headers['Content-Type']).to be_eql(content_type)
end
end
context 'having a html 200 resposne with meta charset tag' do
let(:body) { [meta_charset_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)
expect(res_headers['Content-Type']).to be_eql(content_type)
meta_tag = ''
expect(new_body.index(snippet)).to be > new_body.index(meta_tag)
end
end
context 'having a html 200 resposne with meta content-type tag' do
let(:body) { [meta_content_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)
expect(res_headers['Content-Type']).to be_eql(content_type)
meta_tag = ''
expect(new_body.index(snippet)).to be > new_body.index(meta_tag)
end
end
context 'having a html 200 response and SecureHeaders >= 3.0.0 defined' do
let(:body) { [html] }
let(:status) { 200 }
let(:headers) do
{ 'Content-Type' => content_type }
end
before do
Object.const_set('SecureHeaders', Module.new)
SecureHeaders.const_set('VERSION', '3.0.0')
SecureHeaders.const_set('Configuration', Module.new {
def self.get
end
})
allow(SecureHeaders).to receive(:content_security_policy_script_nonce) { 'lorem-ipsum-nonce' }
end
after do
Object.send(:remove_const, 'SecureHeaders')
end
it 'renders the snippet and config in the response with nonce in script tag when SecureHeaders installed' do
secure_headers_config = double(:configuration, :current_csp => {}, :csp => double(:opt_out? => false))
allow(SecureHeaders::Configuration).to receive(:get).and_return(secure_headers_config)
res_status, res_headers, response = subject.call(env)
new_body = response.body.join
expect(new_body).to include('