Skip to content

Commit

Permalink
Add test for url resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyCTHsu committed Aug 7, 2024
1 parent 50e645e commit 297f952
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
3 changes: 1 addition & 2 deletions lib/datadog/core/crashtracking/agent_base_url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
module Datadog
module Core
module Crashtracking
# This module provides a method to resolve the base URL of the agent
module AgentBaseUrl
module_function

Expand All @@ -14,8 +15,6 @@ def resolve(agent_settings)
"#{agent_settings.ssl ? 'https' : 'http'}://#{agent_settings.hostname}:#{agent_settings.port}/"
when Datadog::Core::Configuration::Ext::Agent::UnixSocket::ADAPTER
"unix://#{agent_settings.uds_path}"
else
nil
end
end
end
Expand Down
64 changes: 64 additions & 0 deletions spec/datadog/core/crashtracking/agent_base_url_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# frozen_string_literal: true

require 'spec_helper'
require 'datadog/core/crashtracking/agent_base_url'

RSpec.describe Datadog::Core::Crashtracking::AgentBaseUrl do
describe '.resolve' do
context 'when using HTTP adapter' do
context 'when SSL is enabled' do
let(:agent_settings) do
double(
'agent_settings',
adapter: Datadog::Core::Configuration::Ext::Agent::HTTP::ADAPTER,
ssl: true,
hostname: 'example.com',
port: 8080
)
end

it 'returns the correct base URL' do
expect(described_class.resolve(agent_settings)).to eq('https://example.com:8080/')
end
end

context 'when SSL is disabled' do
let(:agent_settings) do
double(
'agent_settings',
adapter: Datadog::Core::Configuration::Ext::Agent::HTTP::ADAPTER,
ssl: false,
hostname: 'example.com',
port: 8080
)
end

it 'returns the correct base URL' do
expect(described_class.resolve(agent_settings)).to eq('http://example.com:8080/')
end
end
end

context 'when using UnixSocket adapter' do
let(:agent_settings) do
double(
'agent_settings',
adapter: Datadog::Core::Configuration::Ext::Agent::UnixSocket::ADAPTER,
uds_path: '/var/run/datadog.sock'
)
end

it 'returns the correct base URL' do
expect(described_class.resolve(agent_settings)).to eq('unix:///var/run/datadog.sock')
end
end

context 'when using unknownm adapter' do
let(:agent_settings) { double('agent_settings', adapter: 'unknown') }

it 'returns nil' do
expect(described_class.resolve(agent_settings)).to be_nil
end
end
end
end

0 comments on commit 297f952

Please sign in to comment.