Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 45 additions & 3 deletions spec/lib/appsignal/integrations/railtie_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,29 @@

describe Appsignal::Integrations::Railtie do
include RailsHelper

before { Appsignal.clear! }
after { clear_rails_error_reporter! }

def expect_middleware_to_match(middleware, klass, args)
raise "expect_middleware_to_match: No middleware found!" unless middleware

expect(middleware.klass).to eq(klass)
expect(middleware.args).to match(args)
end

# Resolve the middleware proxy stack for the given app
# This needs to be done manually as the middleware stack on the
# MyApp::Application constant is frozen after the initial initialization. We need
# to test if the operations we do in the Railtie are added by resolving it.
def resolve_middleware(app)
middleware_stack = ActionDispatch::MiddlewareStack.new
# Add this middleware, because our Railtie relies on it being present
middleware_stack.use ActionDispatch::DebugExceptions
app.middleware.merge_into(middleware_stack)
middleware_stack
end

describe "on Rails app initialize!" do
it "starts AppSignal by calling its hooks" do
expect(Appsignal::Integrations::Railtie).to receive(:on_load).and_call_original
Expand Down Expand Up @@ -102,14 +117,14 @@ def initialize_railtie(event)
it "adds the middleware" do
initialize_railtie(event)

middlewares = MyApp::Application.middleware
middleware_stack = resolve_middleware(app)
expect_middleware_to_match(
middlewares.find { |m| m.klass == ::Rack::Events },
middleware_stack.find { |m| m.klass == ::Rack::Events },
::Rack::Events,
[[instance_of(Appsignal::Rack::EventHandler)]]
)
expect_middleware_to_match(
middlewares.find { |m| m.klass == Appsignal::Rack::RailsInstrumentation },
middleware_stack.find { |m| m.klass == Appsignal::Rack::RailsInstrumentation },
Appsignal::Rack::RailsInstrumentation,
[]
)
Expand Down Expand Up @@ -178,6 +193,16 @@ def subscribe(subscriber)
expect(Appsignal.config).to be_nil
end
end

it "doesn't add the middleware when AppSignal is not started" do
allow(Appsignal).to receive(:started?).and_return(false)
initialize_railtie(event)

middleware_stack = resolve_middleware(app)
expect(middleware_stack.find { |m| m.klass == ::Rack::Events }).to be_nil
expect(middleware_stack.find { |m| m.klass == Appsignal::Rack::RailsInstrumentation })
.to be_nil
end
end

describe ".after_initialize" do
Expand Down Expand Up @@ -205,6 +230,23 @@ def subscribe(subscriber)
expect(Appsignal.config).to be_nil
end
end

it "adds the middleware even when AppSignal is not started" do
allow(Appsignal).to receive(:started?).and_return(false)
initialize_railtie(event)

middleware_stack = resolve_middleware(app)
expect_middleware_to_match(
middleware_stack.find { |m| m.klass == ::Rack::Events },
::Rack::Events,
[[instance_of(Appsignal::Rack::EventHandler)]]
)
expect_middleware_to_match(
middleware_stack.find { |m| m.klass == Appsignal::Rack::RailsInstrumentation },
Appsignal::Rack::RailsInstrumentation,
[]
)
end
end
end

Expand Down
Loading