Skip to content

Commit

Permalink
Updated API tests to verify they are instrumented.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesiarmes committed Jul 3, 2024
1 parent c828e50 commit 92aed62
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 13 deletions.
10 changes: 9 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

require 'factory_bot'
require 'rack'
require 'rack/test'

# Configure code coverage reporting.
if ENV.fetch('COVERAGE', false)
Expand All @@ -14,9 +16,15 @@
end
end

# We need to build a Rack app for testing. This ensures that we're including the
# appropriate middleware and that the app is configured correctly.
ENV['RACK_ENV'] = 'test'
RSPEC_APP = Rack::Builder.parse_file('config.ru')

RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
end

# Include factories.
# Include shared examples and factories.
require_relative 'support/examples'
require_relative 'support/factories'
3 changes: 3 additions & 0 deletions spec/support/examples.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true

require_relative 'examples/instrumented'
18 changes: 18 additions & 0 deletions spec/support/examples/instrumented.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

require 'statsd-instrument'

# Verify endpoint instrumentation.
RSpec.shared_examples 'instrumented' do |method, path, stat|
include StatsD::Instrument::Matchers

it 'increments the endpoint counter' do
expect { send(method, path, defined?(params) ? params : {}) }.to \
trigger_statsd_increment("endpoint.#{stat}.requests")
end

it 'measures the endpoint duration' do
expect { send(method, path) }.to \
trigger_statsd_measure("endpoint.#{stat}.duration")
end
end
11 changes: 3 additions & 8 deletions spec/unit/document_transfer/api/health_spec.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
# frozen_string_literal: true

require 'rack/test'

require_relative '../../../../lib/api/api'
require_relative '../../../../lib/api/health'

describe DocumentTransfer::API::Health do
include Rack::Test::Methods
include StatsD::Instrument::Matchers

def app
DocumentTransfer::API::API
RSPEC_APP
end

describe 'GET /health' do
include_examples 'instrumented', :get, '/health', :health

it 'returns 200' do
get '/health'
expect(last_response).to be_ok
Expand All @@ -23,9 +22,5 @@ def app
get '/health'
expect(last_response.body).to eq({ status: 'ok' }.to_json)
end

it 'increments the health check counter' do
expect { get '/health' }.to trigger_statsd_increment('health_check')
end
end
end
7 changes: 3 additions & 4 deletions spec/unit/document_transfer/api/transfer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
# frozen_string_literal: true

require 'rack/test'

require_relative '../../../../lib/api/api'
require_relative '../../../../lib/api/transfer'

describe DocumentTransfer::API::Transfer do
include Rack::Test::Methods
include StatsD::Instrument::Matchers

def app
DocumentTransfer::API::API
RSPEC_APP
end

describe 'POST /transfer' do
Expand All @@ -35,6 +32,8 @@ def app
allow(destination).to receive(:transfer).and_return({ path: 'rspec-folder/rspec.pdf' })
end

include_examples 'instrumented', :post, '/transfer', :transfer

it 'succeeds' do
post '/transfer', params

Expand Down

0 comments on commit 92aed62

Please sign in to comment.