From 1d3aaebff5bf35837cb2d29556bb10bb4de591b6 Mon Sep 17 00:00:00 2001 From: Tom de Bruijn Date: Tue, 2 Jul 2024 21:25:04 +0200 Subject: [PATCH] Test webmachine custom action names Use a Webmachine resource to test the instrumentation. Use that to set a custom action name and test that it doesn't get overwritten by the instrumentation. --- gemfiles/webmachine1.gemfile | 9 ++-- .../appsignal/integrations/webmachine_spec.rb | 47 +++++++++++++++---- 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/gemfiles/webmachine1.gemfile b/gemfiles/webmachine1.gemfile index 67464de8b..6f12e6e89 100644 --- a/gemfiles/webmachine1.gemfile +++ b/gemfiles/webmachine1.gemfile @@ -1,6 +1,7 @@ -source 'https://rubygems.org' +source "https://rubygems.org" -gem 'webmachine', '~> 1.6' -gem 'webrick' +gem "i18n", "~> 0.0" # Lock to pre 1.x version as it's not compatible +gem "webmachine", "~> 1.6" +gem "webrick" -gemspec :path => '../' +gemspec :path => "../" diff --git a/spec/lib/appsignal/integrations/webmachine_spec.rb b/spec/lib/appsignal/integrations/webmachine_spec.rb index 5585af204..901f0110e 100644 --- a/spec/lib/appsignal/integrations/webmachine_spec.rb +++ b/spec/lib/appsignal/integrations/webmachine_spec.rb @@ -22,9 +22,26 @@ def headers nil ) end - let(:resource) { double(:trace? => false, :handle_exception => true, :"code=" => nil) } - let(:response) { Response.new } - let(:fsm) { Webmachine::Decision::FSM.new(resource, request, response) } + let(:app) do + proc do + def to_html + "Some HTML" + end + end + end + let(:resource) do + app_block = app + Class.new(Webmachine::Resource) do + class_eval(&app_block) if app_block + + def self.name + "MyResource" + end + end + end + let(:resource_instance) { resource.new(request, response) } + let(:response) { Webmachine::Response.new } + let(:fsm) { Webmachine::Decision::FSM.new(resource_instance, request, response) } before { start_agent } around { |example| keep_transactions { example.run } } @@ -35,7 +52,23 @@ def headers it "sets the action" do fsm.run - expect(last_transaction).to have_action("RSpec::Mocks::Double#GET") + expect(last_transaction).to have_action("MyResource#GET") + end + + context "with action already set" do + let(:app) do + proc do + def to_html + Appsignal.set_action("Custom Action") + "Some HTML" + end + end + end + + it "doesn't overwrite the action" do + fsm.run + expect(last_transaction).to have_action("Custom Action") + end end it "records an instrumentation event" do @@ -53,12 +86,6 @@ def headers expect(last_transaction).to be_completed expect(current_transaction?).to be_falsy end - - it "sets a response code" do - expect(fsm.response.code).to be_nil - fsm.run - expect(fsm.response.code).not_to be_nil - end end describe "#handle_exceptions" do