Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions gems/instruments-ruby4.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ gem 'moped'
gem 'actionpack'
gem 'actionview'
gem 'httpx'
gem 'webmock'
gem 'grape'
1 change: 1 addition & 0 deletions gems/instruments.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ gem 'moped'
gem 'actionpack'
gem 'actionview'
gem 'httpx'
gem 'webmock'
gem 'grape'
# json 3.0 (released 2026-09) dropped the `quirks_mode` keyword that Grape 3's
# ActiveSupport JSON formatter still passes, so any Grape 3 JSON request raises
Expand Down
1 change: 1 addition & 0 deletions gems/typhoeus.gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
eval_gemfile("../Gemfile")

gem 'typhoeus'
gem 'webmock'
41 changes: 36 additions & 5 deletions test/unit/instruments/httpx_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,34 @@
require 'scout_apm/instruments/httpx'

require 'httpx'
require 'webmock'

class HTTPXTest < Minitest::Test
include WebMock::API

def setup
super # clears Thread.current[:scout_request] so a stale request from a
# prior test can't swallow this test's layers
WebMock.enable!
WebMock.disable_net_connect!

@context = ScoutApm::AgentContext.new
@recorder = FakeRecorder.new
ScoutApm::Agent.instance.context.recorder = @recorder
ScoutApm::Instruments::HTTPX.new(@context).install(prepend: false)
end

def teardown
WebMock.reset!
WebMock.allow_net_connect!
WebMock.disable!
super
end

def test_httpx
stub_request(:get, /news\.ycombinator\.com/).to_return(status: 200, body: "")
stub_request(:get, /google\.com/).to_return(status: 200, body: "")

responses = HTTPX.get(
"https://news.ycombinator.com/news",
"https://news.ycombinator.com/news?p=2",
Expand All @@ -27,11 +44,18 @@ def test_httpx
end

def test_httpx_post_request
stub_request(:post, /httpbin\.org\/post/).to_return(status: 200, body: "")

HTTPX.post("https://httpbin.org/post", json: { test: "data" })
assert_recorded(@recorder, "HTTP", "POST", "httpbin.org/post")
end

def test_instruments_httpx_error_handling
# An error response still flows through Session#request, so the layer is
# started and stopped, and the request is recorded exactly once.
stub_request(:get, /thisshouldnotexistatall12345\.com/)
.to_return(status: 500, body: "")

begin
HTTPX.get("https://thisshouldnotexistatall12345.com")
rescue
Expand All @@ -41,6 +65,10 @@ def test_instruments_httpx_error_handling
end

def test_httpx_request_retry
# A timed-out request is still dispatched through Session#request, so the
# layer is recorded even though the response errors.
stub_request(:get, /httpbin\.org\/delay/).to_timeout

begin
HTTPX.with(timeout: { connect_timeout: 0.25, request_timeout: 0.25 })
.get("https://httpbin.org/delay/5")
Expand All @@ -50,14 +78,17 @@ def test_httpx_request_retry
end

def test_multiple_plugins
stub_request(:get, /news\.ycombinator\.com/).to_return(status: 200, body: "")
stub_request(:get, "http://httpbin.org/redirect/2")
.to_return(status: 302, headers: { "Location" => "http://httpbin.org/redirect/1" })
stub_request(:get, "http://httpbin.org/redirect/1")
.to_return(status: 302, headers: { "Location" => "http://httpbin.org/get" })
stub_request(:get, "http://httpbin.org/get").to_return(status: 200, body: "")

session = HTTPX.plugin(:persistent).plugin(:follow_redirects)

session.get("https://news.ycombinator.com/news")
begin
session.get("http://httpbin.org/redirect/2")
rescue
skip "httpbin.org not available"
end
session.get("http://httpbin.org/redirect/2")

assert_equal 2, @recorder.requests.length, "Expected 2 requests to be recorded"
end
Expand Down
18 changes: 17 additions & 1 deletion test/unit/instruments/typhoeus_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,31 @@
require 'scout_apm/instruments/typhoeus'

require 'typhoeus'
require 'webmock'

class TyphoeusTest < Minitest::Test
include WebMock::API

def setup
super # clears Thread.current[:scout_request] so a stale request from a
# prior test can't swallow this test's layers
WebMock.enable!
WebMock.disable_net_connect!
stub_request(:any, /example\.com/).to_return(status: 200, body: "")

@context = ScoutApm::AgentContext.new
@recorder = FakeRecorder.new
ScoutApm::Agent.instance.context.recorder = @recorder
ScoutApm::Instruments::Typhoeus.new(@context).install(prepend: false)
end

def teardown
WebMock.reset!
WebMock.allow_net_connect!
WebMock.disable!
super
end

def test_instruments_typhoeus_hydra
hydra = Typhoeus::Hydra.new
2.times.map{ hydra.queue(Typhoeus::Request.new("example.com", followlocation: true)) }
Expand All @@ -39,4 +55,4 @@ def assert_recorded(recorder, type, name, desc = nil)
assert_equal desc, req.root_layer.desc if !desc.nil?
end
end
end
end
Loading