Skip to content

Commit 3c68d80

Browse files
authored
Fix net/http adapter and an issue with nil responses (#23)
* Update Gemfile for Rails 7 * Fix IoMonitor::NetHttpAdapterPatch to avoid collisions with other tools, e.g. Faraday * Add test to cover an issue with nil response * Fix an issue with nil responses * Restore Gemfile
1 parent 6bfdc38 commit 3c68d80

File tree

5 files changed

+21
-3
lines changed

5 files changed

+21
-3
lines changed

lib/io_monitor/controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def append_info_to_payload(payload)
3737
data[source] = aggregator.get(source)
3838
end
3939

40-
data[:response] = payload[:response].body.bytesize
40+
data[:response] = payload[:response]&.body&.bytesize || 0
4141
end
4242

4343
private

lib/io_monitor/patches/net_http_adapter_patch.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
module IoMonitor
44
module NetHttpAdapterPatch
55
def request(*args, &block)
6-
super do |response|
7-
if response.body && IoMonitor.aggregator.active?
6+
super.tap do |response|
7+
if response&.body && IoMonitor.aggregator.active?
88
IoMonitor.aggregator.increment(NetHttpAdapter.kind, response.body.bytesize)
99
end
1010
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
class FakeController < ApplicationController
4+
include IoMonitor::Controller
5+
6+
def fake
7+
raise ActionController::RoutingError.new("Fake")
8+
end
9+
end

spec/dummy/config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
22

33
Rails.application.routes.draw do
4+
get "/fake", to: "fake#fake"
45
end

spec/io_monitor/controller_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,11 @@ def show
133133
end
134134
end
135135
end
136+
137+
RSpec.describe "when response is nil", type: :request do
138+
it "does not fail" do
139+
get "/fake"
140+
141+
expect(response).to be_not_found
142+
end
143+
end

0 commit comments

Comments
 (0)