Skip to content

Commit

Permalink
Use sentry request stubbing when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
solnic committed Feb 17, 2025
1 parent fa20ea2 commit 3e7aebd
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 24 deletions.
6 changes: 3 additions & 3 deletions sentry-ruby/spec/contexts/with_request_mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def setsockopt(*args); end
allow(TCPSocket).to receive(:open).and_return(FakeSocket.new)
end

def stub_request(fake_response, &block)
def sentry_stub_request(fake_response, &block)
allow_any_instance_of(Net::HTTP).to receive(:connect)
allow_any_instance_of(Net::HTTP).to receive(:transport_request) do |http_obj, request|
block.call(request, http_obj) if block
Expand All @@ -32,10 +32,10 @@ def build_fake_response(status, body: {}, headers: {})

def stub_sentry_response
# use bad request as an example is easier for verifying with error messages
stub_request(build_fake_response("400", body: { data: "bad sentry DSN public key" }))
sentry_stub_request(build_fake_response("400", body: { data: "bad sentry DSN public key" }))
end

def stub_normal_response(code: "200", &block)
stub_request(build_fake_response(code), &block)
sentry_stub_request(build_fake_response(code), &block)
end
end
6 changes: 6 additions & 0 deletions sentry-ruby/spec/sentry/breadcrumb/http_logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
RSpec.describe :http_logger do
include_context "with request mock"

around do |example|
WebMock.disable!
example.run
WebMock.enable!
end

let(:string_io) { StringIO.new }
let(:logger) do
::Logger.new(string_io)
Expand Down
6 changes: 6 additions & 0 deletions sentry-ruby/spec/sentry/net/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
RSpec.describe Sentry::Net::HTTP do
include_context "with request mock"

around do |example|
WebMock.disable!
example.run
WebMock.enable!
end

let(:string_io) { StringIO.new }
let(:logger) do
::Logger.new(string_io)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
RSpec.describe "rate limiting" do
include_context "with request mock"

around do |example|
WebMock.disable!
example.run
WebMock.enable!
end

before do
perform_basic_setup do |config|
config.logger = Logger.new(string_io)
Expand Down Expand Up @@ -107,7 +113,7 @@

describe "rate limit header processing" do
before do
stub_request(fake_response)
sentry_stub_request(fake_response)
end

shared_examples "rate limiting headers handling" do
Expand Down
44 changes: 25 additions & 19 deletions sentry-ruby/spec/sentry/transport/http_transport_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
RSpec.describe Sentry::HTTPTransport do
include_context "with request mock"

around do |example|
WebMock.disable!
example.run
WebMock.enable!
end

let(:configuration) do
Sentry::Configuration.new.tap do |config|
config.dsn = Sentry::TestHelper::DUMMY_DSN
Expand All @@ -22,7 +28,7 @@
subject { client.transport }

it "logs a debug message only during initialization" do
stub_request(build_fake_response("200"))
sentry_stub_request(build_fake_response("200"))
string_io = StringIO.new
configuration.logger = Logger.new(string_io)

Expand All @@ -39,7 +45,7 @@
end

it "initializes new Net::HTTP instance for every request" do
stub_request(build_fake_response("200")) do |request|
sentry_stub_request(build_fake_response("200")) do |request|
expect(request["User-Agent"]).to eq("sentry-ruby/#{Sentry::VERSION}")
end

Expand Down Expand Up @@ -87,7 +93,7 @@
let(:fake_response) { build_fake_response("200") }

it 'sets default User-Agent' do
stub_request(fake_response) do |request|
sentry_stub_request(fake_response) do |request|
expect(request["User-Agent"]).to eq("sentry-ruby/#{Sentry::VERSION}")
end

Expand All @@ -97,7 +103,7 @@
it "accepts custom proxy" do
configuration.transport.proxy = { uri: URI("https://example.com"), user: "stan", password: "foobar" }

stub_request(fake_response) do |_, http_obj|
sentry_stub_request(fake_response) do |_, http_obj|
expect(http_obj.proxy_address).to eq("example.com")
expect(http_obj.proxy_user).to eq("stan")
expect(http_obj.proxy_pass).to eq("foobar")
Expand All @@ -109,7 +115,7 @@
it "accepts a custom proxy string" do
configuration.transport.proxy = "https://stan:foobar@example.com:8080"

stub_request(fake_response) do |_, http_obj|
sentry_stub_request(fake_response) do |_, http_obj|
expect(http_obj.proxy_address).to eq("example.com")
expect(http_obj.proxy_user).to eq("stan")
expect(http_obj.proxy_pass).to eq("foobar")
Expand All @@ -122,7 +128,7 @@
it "accepts a custom proxy URI" do
configuration.transport.proxy = URI("https://stan:foobar@example.com:8080")

stub_request(fake_response) do |_, http_obj|
sentry_stub_request(fake_response) do |_, http_obj|
expect(http_obj.proxy_address).to eq("example.com")
expect(http_obj.proxy_user).to eq("stan")
expect(http_obj.proxy_pass).to eq("foobar")
Expand All @@ -136,7 +142,7 @@
begin
ENV["http_proxy"] = "https://stan:foobar@example.com:8080"

stub_request(fake_response) do |_, http_obj|
sentry_stub_request(fake_response) do |_, http_obj|
expect(http_obj.proxy_address).to eq("example.com")
expect(http_obj.proxy_port).to eq(8080)

Expand All @@ -155,7 +161,7 @@
it "accepts custom timeout" do
configuration.transport.timeout = 10

stub_request(fake_response) do |_, http_obj|
sentry_stub_request(fake_response) do |_, http_obj|
expect(http_obj.read_timeout).to eq(10)

if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.6")
Expand All @@ -169,7 +175,7 @@
it "accepts custom open_timeout" do
configuration.transport.open_timeout = 10

stub_request(fake_response) do |_, http_obj|
sentry_stub_request(fake_response) do |_, http_obj|
expect(http_obj.open_timeout).to eq(10)
end

Expand All @@ -178,7 +184,7 @@

describe "ssl configurations" do
it "has the corrent default" do
stub_request(fake_response) do |_, http_obj|
sentry_stub_request(fake_response) do |_, http_obj|
expect(http_obj.verify_mode).to eq(1)
expect(http_obj.ca_file).to eq(nil)
end
Expand All @@ -189,7 +195,7 @@
it "accepts custom ssl_verification configuration" do
configuration.transport.ssl_verification = false

stub_request(fake_response) do |_, http_obj|
sentry_stub_request(fake_response) do |_, http_obj|
expect(http_obj.verify_mode).to eq(0)
expect(http_obj.ca_file).to eq(nil)
end
Expand All @@ -200,7 +206,7 @@
it "accepts custom ssl_ca_file configuration" do
configuration.transport.ssl_ca_file = "/tmp/foo"

stub_request(fake_response) do |_, http_obj|
sentry_stub_request(fake_response) do |_, http_obj|
expect(http_obj.verify_mode).to eq(1)
expect(http_obj.ca_file).to eq("/tmp/foo")
end
Expand All @@ -211,7 +217,7 @@
it "accepts custom ssl configuration" do
configuration.transport.ssl = { verify: false, ca_file: "/tmp/foo" }

stub_request(fake_response) do |_, http_obj|
sentry_stub_request(fake_response) do |_, http_obj|
expect(http_obj.verify_mode).to eq(0)
expect(http_obj.ca_file).to eq("/tmp/foo")
end
Expand All @@ -225,7 +231,7 @@
let(:fake_response) { build_fake_response("200") }

it "compresses data by default" do
stub_request(fake_response) do |request|
sentry_stub_request(fake_response) do |request|
expect(request["Content-Type"]).to eq("application/x-sentry-envelope")
expect(request["Content-Encoding"]).to eq("gzip")

Expand All @@ -238,7 +244,7 @@
end

it "doesn't compress small event" do
stub_request(fake_response) do |request|
sentry_stub_request(fake_response) do |request|
expect(request["Content-Type"]).to eq("application/x-sentry-envelope")
expect(request["Content-Encoding"]).to eq("")

Expand All @@ -255,7 +261,7 @@
it "doesn't compress data if the encoding is not gzip" do
configuration.transport.encoding = "json"

stub_request(fake_response) do |request|
sentry_stub_request(fake_response) do |request|
expect(request["Content-Type"]).to eq("application/x-sentry-envelope")
expect(request["Content-Encoding"]).to eq("")

Expand Down Expand Up @@ -296,7 +302,7 @@
let(:fake_response) { build_fake_response("404") }

it "raises an error" do
stub_request(fake_response)
sentry_stub_request(fake_response)

expect { subject.send_data(data) }.to raise_error(Sentry::ExternalError, /the server responded with status 404/)
end
Expand All @@ -306,7 +312,7 @@
let(:fake_response) { build_fake_response("500") }

it "raises an error" do
stub_request(fake_response)
sentry_stub_request(fake_response)

expect { subject.send_data(data) }.to raise_error(Sentry::ExternalError, /the server responded with status 500/)
end
Expand All @@ -318,7 +324,7 @@
end

it "raises an error with header" do
stub_request(error_response)
sentry_stub_request(error_response)

expect { subject.send_data(data) }.to raise_error(Sentry::ExternalError, /error_in_header/)
end
Expand Down
8 changes: 7 additions & 1 deletion sentry-ruby/spec/sentry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,14 @@
context "with spotlight" do
before { perform_basic_setup { |c| c.spotlight = true } }

around do |example|
WebMock.disable!
example.run
WebMock.enable!
end

it "sends the event to spotlight too" do
stub_request(build_fake_response("200")) do |request, http_obj|
sentry_stub_request(build_fake_response("200")) do |request, http_obj|
expect(request["Content-Type"]).to eq("application/x-sentry-envelope")
expect(request["Content-Encoding"]).to eq("gzip")
expect(http_obj.address).to eq("localhost")
Expand Down

0 comments on commit 3e7aebd

Please sign in to comment.