|
1 | 1 | require 'spec_helper' |
2 | 2 |
|
3 | 3 | RSpec.describe Sentry::HTTPTransport do |
4 | | - let(:client) { Sentry::Client.new(Sentry.configuration) } |
5 | | - let(:event) { client.event_from_message("test") } |
6 | | - subject { described_class.new(Sentry.configuration) } |
7 | | - |
8 | | - describe "customizations" do |
9 | | - before do |
10 | | - Sentry.init do |c| |
11 | | - c.dsn = 'http://12345@sentry.localdomain/sentry/42' |
12 | | - end |
| 4 | + let(:configuration) do |
| 5 | + Sentry::Configuration.new.tap do |config| |
| 6 | + config.dsn = 'http://12345@sentry.localdomain/sentry/42' |
13 | 7 | end |
| 8 | + end |
| 9 | + let(:client) { Sentry::Client.new(configuration) } |
| 10 | + let(:event) { client.event_from_message("foobarbaz") } |
| 11 | + let(:data) do |
| 12 | + subject.encode(event.to_hash) |
| 13 | + end |
14 | 14 |
|
| 15 | + subject { described_class.new(configuration) } |
| 16 | + |
| 17 | + describe "customizations" do |
15 | 18 | it 'sets a custom User-Agent' do |
16 | 19 | expect(subject.conn.headers[:user_agent]).to eq("sentry-ruby/#{Sentry::VERSION}") |
17 | 20 | end |
18 | 21 |
|
19 | 22 | it 'allows to customise faraday' do |
20 | 23 | builder = spy('faraday_builder') |
21 | 24 | expect(Faraday).to receive(:new).and_yield(builder) |
22 | | - Sentry.configuration.transport.faraday_builder = proc { |b| b.request :instrumentation } |
| 25 | + configuration.transport.faraday_builder = proc { |b| b.request :instrumentation } |
23 | 26 |
|
24 | 27 | subject |
25 | 28 |
|
26 | 29 | expect(builder).to have_received(:request).with(:instrumentation) |
27 | 30 | end |
28 | 31 | end |
29 | 32 |
|
| 33 | + describe "request payload" do |
| 34 | + let(:compressed_stubs) do |
| 35 | + Faraday::Adapter::Test::Stubs.new do |stub| |
| 36 | + stub.post('sentry/api/42/envelope/') do |env| |
| 37 | + expect(env.request_headers["Content-Type"]).to eq("application/x-sentry-envelope") |
| 38 | + expect(env.request_headers["Content-Encoding"]).to eq("gzip") |
| 39 | + |
| 40 | + envelope = Zlib.gunzip(env.body) |
| 41 | + expect(envelope).to include(event.event_id) |
| 42 | + expect(envelope).to include("foobarbaz") |
| 43 | + end |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + let(:uncompressed_stubs) do |
| 48 | + Faraday::Adapter::Test::Stubs.new do |stub| |
| 49 | + stub.post('sentry/api/42/envelope/') do |env| |
| 50 | + expect(env.request_headers["Content-Type"]).to eq("application/x-sentry-envelope") |
| 51 | + expect(env.request_headers["Content-Encoding"]).to eq("") |
| 52 | + |
| 53 | + envelope = env.body |
| 54 | + expect(envelope).to include(event.event_id) |
| 55 | + expect(envelope).to include("foobarbaz") |
| 56 | + end |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + it "compresses data by default" do |
| 61 | + configuration.transport.http_adapter = [:test, compressed_stubs] |
| 62 | + |
| 63 | + subject.send_data(data) |
| 64 | + compressed_stubs.verify_stubbed_calls |
| 65 | + end |
| 66 | + |
| 67 | + it "doesn't compress small event" do |
| 68 | + configuration.transport.http_adapter = [:test, uncompressed_stubs] |
| 69 | + |
| 70 | + event.instance_variable_set(:@threads, nil) # shrink event |
| 71 | + |
| 72 | + subject.send_data(data) |
| 73 | + uncompressed_stubs.verify_stubbed_calls |
| 74 | + end |
| 75 | + |
| 76 | + it "doesn't compress data if the encoding is not gzip" do |
| 77 | + configuration.transport.http_adapter = [:test, uncompressed_stubs] |
| 78 | + configuration.transport.encoding = "json" |
| 79 | + |
| 80 | + subject.send_data(data) |
| 81 | + uncompressed_stubs.verify_stubbed_calls |
| 82 | + end |
| 83 | + end |
| 84 | + |
30 | 85 | describe "failed request handling" do |
31 | 86 | before do |
32 | | - Sentry.init do |c| |
33 | | - c.dsn = 'http://12345@sentry.localdomain/sentry/42' |
34 | | - c.transport.http_adapter = [:test, stubs] |
35 | | - c.transport.transport_class = described_class |
36 | | - end |
| 87 | + configuration.transport.http_adapter = [:test, stubs] |
37 | 88 | end |
| 89 | + |
38 | 90 | context "receive 4xx responses" do |
39 | 91 | let(:stubs) do |
40 | 92 | Faraday::Adapter::Test::Stubs.new do |stub| |
|
43 | 95 | end |
44 | 96 |
|
45 | 97 | it 'raises an error' do |
46 | | - expect { subject.send_data(event.to_hash) }.to raise_error(Sentry::Error, /the server responded with status 404/) |
| 98 | + expect { subject.send_data(data) }.to raise_error(Sentry::Error, /the server responded with status 404/) |
47 | 99 |
|
48 | 100 | stubs.verify_stubbed_calls |
49 | 101 | end |
|
57 | 109 | end |
58 | 110 |
|
59 | 111 | it 'raises an error' do |
60 | | - expect { subject.send_data(event.to_hash) }.to raise_error(Sentry::Error, /the server responded with status 500/) |
| 112 | + expect { subject.send_data(data) }.to raise_error(Sentry::Error, /the server responded with status 500/) |
61 | 113 |
|
62 | 114 | stubs.verify_stubbed_calls |
63 | 115 | end |
|
71 | 123 | end |
72 | 124 |
|
73 | 125 | it 'raises an error with header' do |
74 | | - expect { subject.send_data(event.to_hash) }.to raise_error(Sentry::Error, /error_in_header/) |
| 126 | + expect { subject.send_data(data) }.to raise_error(Sentry::Error, /error_in_header/) |
75 | 127 |
|
76 | 128 | stubs.verify_stubbed_calls |
77 | 129 | end |
|
0 commit comments