Skip to content
Open
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
11 changes: 9 additions & 2 deletions lib/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def initialize(server_secret, options, backoff_mult = 10)
@backoff_multiplier = backoff_mult
@post_logs_retry_backoff = options.post_logs_retry_backoff
@post_logs_retry_limit = options.post_logs_retry_limit
@ssl_context = options.ssl_context
@session_id = SecureRandom.uuid
@connection_pool = ConnectionPool.new(size: 3) do
meta = Statsig.get_statsig_metadata
Expand Down Expand Up @@ -111,11 +112,17 @@ def request(method, url, body, retries = 0, backoff = 1, zipped = false, event_c
'STATSIG-EVENT-COUNT' => event_count == 0 ? nil : event_count.to_s
)

options = {}
if @ssl_context
options[:ssl_context] = @ssl_context
end

case method
when :GET
request.get(url)
request.get(url, **options)
when :POST
request.post(url, body: body)
options[:body] = body
request.post(url, **options)
end
end
rescue StandardError => e
Expand Down
7 changes: 6 additions & 1 deletion lib/statsig_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ class StatsigOptions
# default: false
attr_accessor :disable_evaluation_memoization

# set a custom SSL context for the network requests
attr_accessor :ssl_context

def initialize(
environment = nil,
download_config_specs_url: nil,
Expand All @@ -115,7 +118,8 @@ def initialize(
post_logs_retry_limit: 3,
post_logs_retry_backoff: nil,
user_persistent_storage: nil,
disable_evaluation_memoization: false
disable_evaluation_memoization: false,
ssl_context: nil
)
@environment = environment.is_a?(Hash) ? environment : nil

Expand Down Expand Up @@ -146,5 +150,6 @@ def initialize(
@post_logs_retry_backoff = post_logs_retry_backoff
@user_persistent_storage = user_persistent_storage
@disable_evaluation_memoization = disable_evaluation_memoization
@ssl_context = ssl_context
end
end
21 changes: 21 additions & 0 deletions test/test_network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@ def teardown
WebMock.disable!
end

def test_ssl_context
ssl_context = OpenSSL::SSL::SSLContext.new.tap { |ctx| ctx.verify_mode = OpenSSL::SSL::VERIFY_PEER }
options = StatsigOptions.new(ssl_context: ssl_context)

@net = Statsig::Network.new('secret-abc', options, 1)
spy = Spy.on_instance_method(HTTP::Client, :post).and_return(
HTTP::Response.new(
{
status: 200,
body: 'hello',
version: "1.1",
headers: {},
request: nil,
},
)
)

res, _ = @net.post('https://statsigapi.net/v1/log_event', '{}', 5, 1)
assert_equal(spy.calls.first.kwargs[:ssl_context], ssl_context)
end

def test_retries_succeed
stub_request(:post, 'https://statsigapi.net/v1/log_event').to_return(status: lambda { |req| status_lambda(req) }, body: 'hello')

Expand Down