Skip to content

Commit 8257b49

Browse files
committed
Use sentry request stubbing when needed
1 parent 5d0411d commit 8257b49

File tree

6 files changed

+54
-24
lines changed

6 files changed

+54
-24
lines changed

sentry-ruby/spec/contexts/with_request_mock.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def setsockopt(*args); end
1212
allow(TCPSocket).to receive(:open).and_return(FakeSocket.new)
1313
end
1414

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

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

3838
def stub_normal_response(code: "200", &block)
39-
stub_request(build_fake_response(code), &block)
39+
sentry_stub_request(build_fake_response(code), &block)
4040
end
4141
end

sentry-ruby/spec/sentry/breadcrumb/http_logger_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
RSpec.describe :http_logger do
77
include_context "with request mock"
88

9+
around do |example|
10+
WebMock.disable!
11+
example.run
12+
WebMock.enable!
13+
end
14+
915
let(:string_io) { StringIO.new }
1016
let(:logger) do
1117
::Logger.new(string_io)

sentry-ruby/spec/sentry/net/http_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
RSpec.describe Sentry::Net::HTTP do
77
include_context "with request mock"
88

9+
around do |example|
10+
WebMock.disable!
11+
example.run
12+
WebMock.enable!
13+
end
14+
915
let(:string_io) { StringIO.new }
1016
let(:logger) do
1117
::Logger.new(string_io)

sentry-ruby/spec/sentry/transport/http_transport_rate_limiting_spec.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
RSpec.describe "rate limiting" do
77
include_context "with request mock"
88

9+
around do |example|
10+
WebMock.disable!
11+
example.run
12+
WebMock.enable!
13+
end
14+
915
before do
1016
perform_basic_setup do |config|
1117
config.logger = Logger.new(string_io)
@@ -107,7 +113,7 @@
107113

108114
describe "rate limit header processing" do
109115
before do
110-
stub_request(fake_response)
116+
sentry_stub_request(fake_response)
111117
end
112118

113119
shared_examples "rate limiting headers handling" do

sentry-ruby/spec/sentry/transport/http_transport_spec.rb

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
RSpec.describe Sentry::HTTPTransport do
77
include_context "with request mock"
88

9+
around do |example|
10+
WebMock.disable!
11+
example.run
12+
WebMock.enable!
13+
end
14+
915
let(:configuration) do
1016
Sentry::Configuration.new.tap do |config|
1117
config.dsn = Sentry::TestHelper::DUMMY_DSN
@@ -22,7 +28,7 @@
2228
subject { client.transport }
2329

2430
it "logs a debug message only during initialization" do
25-
stub_request(build_fake_response("200"))
31+
sentry_stub_request(build_fake_response("200"))
2632
string_io = StringIO.new
2733
configuration.logger = Logger.new(string_io)
2834

@@ -39,7 +45,7 @@
3945
end
4046

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

@@ -87,7 +93,7 @@
8793
let(:fake_response) { build_fake_response("200") }
8894

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

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

100-
stub_request(fake_response) do |_, http_obj|
106+
sentry_stub_request(fake_response) do |_, http_obj|
101107
expect(http_obj.proxy_address).to eq("example.com")
102108
expect(http_obj.proxy_user).to eq("stan")
103109
expect(http_obj.proxy_pass).to eq("foobar")
@@ -109,7 +115,7 @@
109115
it "accepts a custom proxy string" do
110116
configuration.transport.proxy = "https://stan:[email protected]:8080"
111117

112-
stub_request(fake_response) do |_, http_obj|
118+
sentry_stub_request(fake_response) do |_, http_obj|
113119
expect(http_obj.proxy_address).to eq("example.com")
114120
expect(http_obj.proxy_user).to eq("stan")
115121
expect(http_obj.proxy_pass).to eq("foobar")
@@ -122,7 +128,7 @@
122128
it "accepts a custom proxy URI" do
123129
configuration.transport.proxy = URI("https://stan:[email protected]:8080")
124130

125-
stub_request(fake_response) do |_, http_obj|
131+
sentry_stub_request(fake_response) do |_, http_obj|
126132
expect(http_obj.proxy_address).to eq("example.com")
127133
expect(http_obj.proxy_user).to eq("stan")
128134
expect(http_obj.proxy_pass).to eq("foobar")
@@ -136,7 +142,7 @@
136142
begin
137143
ENV["http_proxy"] = "https://stan:[email protected]:8080"
138144

139-
stub_request(fake_response) do |_, http_obj|
145+
sentry_stub_request(fake_response) do |_, http_obj|
140146
expect(http_obj.proxy_address).to eq("example.com")
141147
expect(http_obj.proxy_port).to eq(8080)
142148

@@ -155,7 +161,7 @@
155161
it "accepts custom timeout" do
156162
configuration.transport.timeout = 10
157163

158-
stub_request(fake_response) do |_, http_obj|
164+
sentry_stub_request(fake_response) do |_, http_obj|
159165
expect(http_obj.read_timeout).to eq(10)
160166

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

172-
stub_request(fake_response) do |_, http_obj|
178+
sentry_stub_request(fake_response) do |_, http_obj|
173179
expect(http_obj.open_timeout).to eq(10)
174180
end
175181

@@ -178,7 +184,7 @@
178184

179185
describe "ssl configurations" do
180186
it "has the corrent default" do
181-
stub_request(fake_response) do |_, http_obj|
187+
sentry_stub_request(fake_response) do |_, http_obj|
182188
expect(http_obj.verify_mode).to eq(1)
183189
expect(http_obj.ca_file).to eq(nil)
184190
end
@@ -189,7 +195,7 @@
189195
it "accepts custom ssl_verification configuration" do
190196
configuration.transport.ssl_verification = false
191197

192-
stub_request(fake_response) do |_, http_obj|
198+
sentry_stub_request(fake_response) do |_, http_obj|
193199
expect(http_obj.verify_mode).to eq(0)
194200
expect(http_obj.ca_file).to eq(nil)
195201
end
@@ -200,7 +206,7 @@
200206
it "accepts custom ssl_ca_file configuration" do
201207
configuration.transport.ssl_ca_file = "/tmp/foo"
202208

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

214-
stub_request(fake_response) do |_, http_obj|
220+
sentry_stub_request(fake_response) do |_, http_obj|
215221
expect(http_obj.verify_mode).to eq(0)
216222
expect(http_obj.ca_file).to eq("/tmp/foo")
217223
end
@@ -225,7 +231,7 @@
225231
let(:fake_response) { build_fake_response("200") }
226232

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

@@ -238,7 +244,7 @@
238244
end
239245

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

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

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

@@ -296,7 +302,7 @@
296302
let(:fake_response) { build_fake_response("404") }
297303

298304
it "raises an error" do
299-
stub_request(fake_response)
305+
sentry_stub_request(fake_response)
300306

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

308314
it "raises an error" do
309-
stub_request(fake_response)
315+
sentry_stub_request(fake_response)
310316

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

320326
it "raises an error with header" do
321-
stub_request(error_response)
327+
sentry_stub_request(error_response)
322328

323329
expect { subject.send_data(data) }.to raise_error(Sentry::ExternalError, /error_in_header/)
324330
end

sentry-ruby/spec/sentry_spec.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,14 @@
183183
context "with spotlight" do
184184
before { perform_basic_setup { |c| c.spotlight = true } }
185185

186+
around do |example|
187+
WebMock.disable!
188+
example.run
189+
WebMock.enable!
190+
end
191+
186192
it "sends the event to spotlight too" do
187-
stub_request(build_fake_response("200")) do |request, http_obj|
193+
sentry_stub_request(build_fake_response("200")) do |request, http_obj|
188194
expect(request["Content-Type"]).to eq("application/x-sentry-envelope")
189195
expect(request["Content-Encoding"]).to eq("gzip")
190196
expect(http_obj.address).to eq("localhost")

0 commit comments

Comments
 (0)