|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +describe Bugsnag::Rack do |
| 4 | + it "calls the upstream rack app with the environment" do |
| 5 | + rack_env = {"key" => "value"} |
| 6 | + app = lambda { |env| ['response', {}, env] } |
| 7 | + rack_stack = Bugsnag::Rack.new(app) |
| 8 | + |
| 9 | + response = rack_stack.call(rack_env) |
| 10 | + |
| 11 | + expect(response).to eq(['response', {}, rack_env]) |
| 12 | + end |
| 13 | + |
| 14 | + context "when an exception is raised in rack middleware" do |
| 15 | + # Build a fake crashing rack app |
| 16 | + exception = BugsnagTestException.new("It crashed") |
| 17 | + rack_env = {"key" => "value"} |
| 18 | + app = lambda { |env| raise exception } |
| 19 | + rack_stack = Bugsnag::Rack.new(app) |
| 20 | + |
| 21 | + it "re-raises the exception" do |
| 22 | + expect { rack_stack.call(rack_env) }.to raise_error(BugsnagTestException) |
| 23 | + end |
| 24 | + |
| 25 | + it "delivers an exception if auto_notify is enabled" do |
| 26 | + rack_stack.call(rack_env) rescue nil |
| 27 | + |
| 28 | + expect(Bugsnag).to have_sent_notification{ |payload, headers| |
| 29 | + exception_class = payload["events"].first["exceptions"].first["errorClass"] |
| 30 | + expect(exception_class).to eq(exception.class.to_s) |
| 31 | + } |
| 32 | + |
| 33 | + end |
| 34 | + |
| 35 | + it "applies the correct severity reason" do |
| 36 | + rack_stack.call(rack_env) rescue nil |
| 37 | + |
| 38 | + expect(Bugsnag).to have_sent_notification{ |payload, headers| |
| 39 | + event = get_event_from_payload(payload) |
| 40 | + expect(event["unhandled"]).to be true |
| 41 | + expect(event["severityReason"]).to eq({ |
| 42 | + "type" => "unhandledExceptionMiddleware", |
| 43 | + "attributes" => { |
| 44 | + "framework" => "Rack" |
| 45 | + } |
| 46 | + }) |
| 47 | + } |
| 48 | + end |
| 49 | + |
| 50 | + it "does not deliver an exception if auto_notify is disabled" do |
| 51 | + Bugsnag.configure do |config| |
| 52 | + config.auto_notify = false |
| 53 | + end |
| 54 | + |
| 55 | + rack_stack.call(rack_env) rescue nil |
| 56 | + |
| 57 | + expect(Bugsnag).not_to have_sent_notification |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + context "when running against the middleware" do |
| 62 | + before do |
| 63 | + unless defined?(::Rack) |
| 64 | + @mocked_rack = true |
| 65 | + class ::Rack |
| 66 | + class ::Request |
| 67 | + end |
| 68 | + end |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + it "correctly extracts data from rack middleware" do |
| 73 | + callback = double |
| 74 | + rack_env = { |
| 75 | + :env => true, |
| 76 | + :HTTP_test_key => "test_key", |
| 77 | + "rack.session" => { |
| 78 | + :session => true |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + rack_request = double |
| 83 | + rack_params = { |
| 84 | + :param => 'test' |
| 85 | + } |
| 86 | + allow(rack_request).to receive_messages( |
| 87 | + :params => rack_params, |
| 88 | + :ip => "rack_ip", |
| 89 | + :request_method => "TEST", |
| 90 | + :path => "/TEST_PATH", |
| 91 | + :scheme => "http", |
| 92 | + :host => "test_host", |
| 93 | + :port => 80, |
| 94 | + :referer => "referer", |
| 95 | + :fullpath => "/TEST_PATH" |
| 96 | + ) |
| 97 | + expect(::Rack::Request).to receive(:new).with(rack_env).and_return(rack_request) |
| 98 | + |
| 99 | + report = double("Bugsnag::Report") |
| 100 | + allow(report).to receive(:request_data).and_return({ |
| 101 | + :rack_env => rack_env |
| 102 | + }) |
| 103 | + expect(report).to receive(:context=).with("TEST /TEST_PATH") |
| 104 | + expect(report).to receive(:user).and_return({}) |
| 105 | + |
| 106 | + config = double |
| 107 | + allow(config).to receive(:send_environment).and_return(true) |
| 108 | + allow(config).to receive(:meta_data_filters).and_return(nil) |
| 109 | + allow(report).to receive(:configuration).and_return(config) |
| 110 | + expect(report).to receive(:add_tab).once.with(:environment, rack_env) |
| 111 | + expect(report).to receive(:add_tab).once.with(:request, { |
| 112 | + :url => "http://test_host/TEST_PATH", |
| 113 | + :httpMethod => "TEST", |
| 114 | + :params => rack_params, |
| 115 | + :referer => "referer", |
| 116 | + :clientIp => "rack_ip", |
| 117 | + :headers => { |
| 118 | + "Test-Key" => "test_key" |
| 119 | + } |
| 120 | + }) |
| 121 | + expect(report).to receive(:add_tab).once.with(:session, { |
| 122 | + :session => true |
| 123 | + }) |
| 124 | + |
| 125 | + expect(callback).to receive(:call).with(report) |
| 126 | + |
| 127 | + middleware = Bugsnag::Middleware::RackRequest.new(callback) |
| 128 | + middleware.call(report) |
| 129 | + end |
| 130 | + |
| 131 | + after do |
| 132 | + Object.send(:remove_const, :Rack) if @mocked_rack |
| 133 | + end |
| 134 | + |
| 135 | + end |
| 136 | + |
| 137 | + it "don't mess with middlewares list on each req" do |
| 138 | + stub_const('Rack', nil) |
| 139 | + app = lambda { |env| ['200', {}, ['']] } |
| 140 | + |
| 141 | + Bugsnag::Rack.new(app) |
| 142 | + |
| 143 | + expect { 2.times { Bugsnag::Rack.new(app) } }.not_to change { |
| 144 | + Bugsnag.configuration.middleware.instance_variable_get(:@middlewares) |
| 145 | + } |
| 146 | + end |
| 147 | +end |
0 commit comments