Skip to content

Commit 3e1606b

Browse files
committed
Working (?) tests
1 parent ec9e139 commit 3e1606b

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@
1212

1313
# Created when initializing Rails app
1414
/spec/log
15+
/spec/tmp
16+
/log

spec/scan_suppressing_logger/middleware_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,37 @@
1515
expect(wrappers).to include(ScanSuppressingLogger::RollbarWrapper)
1616
end
1717
end
18+
19+
describe 'Replacing Rails::Rack::Logger' do
20+
it 'replaces Rails::Rack::Logger with ScanSuppressingLogger::Middleware' do
21+
expect(Rails.application.middleware).to include(ScanSuppressingLogger::Middleware)
22+
expect(Rails.application.middleware).not_to include(Rails::Rack::Logger)
23+
end
24+
25+
context 'from a suppressed address' do
26+
it 'does not log the request' do
27+
# Expect the middleware to be called
28+
expect_any_instance_of(ScanSuppressingLogger::Middleware).to receive(:call).and_call_original
29+
# Expect the logger to report it was suppressed
30+
expect(Rails.logger).to receive(:info).with('Suppressed for 127.0.0.1')
31+
# Do not expect the original Rails::Rack::Logger to be called
32+
expect_any_instance_of(Rails::Rack::Logger).not_to receive(:call)
33+
34+
# Make a simulated request to the app
35+
env = Rack::MockRequest.env_for('http://example.org/', 'REMOTE_ADDR' => '127.0.0.1')
36+
status, headers, body = Rails.application.call(env)
37+
end
38+
end
39+
40+
context 'from a non-suppressed address' do
41+
it 'logs the request' do
42+
# Expect the original Rails::Rack::Logger to be called
43+
expect_any_instance_of(Rails::Rack::Logger).to receive(:call).and_call_original
44+
45+
# Make a simulated request to the app
46+
env = Rack::MockRequest.env_for('/', 'REMOTE_ADDR' => '192.0.2.1')
47+
status, headers, body = Rails.application.call(env)
48+
end
49+
end
50+
end
1851
end

spec/spec_helper.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
require "bundler/setup"
22
require "rails"
3+
require "action_controller/railtie"
34
require "scan_suppressing_logger"
45

5-
# Set up a dummy Rails app for testing
6+
# Setup a dummy Rails app
67
module Dummy
78
class Application < Rails::Application
89
config.root = File.dirname(__FILE__)
9-
config.eager_load = false
1010
end
1111
end
1212

13-
Dummy::Application.initialize!
13+
# Impossible to do this in a test; so you only get one shot setting it up.
14+
Rails.application.configure do
15+
config.middleware.swap Rails::Rack::Logger, ScanSuppressingLogger::Middleware, {
16+
networks: ['127.0.0.1']
17+
}
18+
19+
config.eager_load = false
20+
config.hosts.clear
21+
end
22+
23+
Rails.application.initialize!
1424

1525
RSpec.configure do |config|
1626
# Enable flags like --only-failures and --next-failure

0 commit comments

Comments
 (0)