Skip to content

Commit

Permalink
Introduce public url var that's shared with browser client (#113)
Browse files Browse the repository at this point in the history
* Introduce public url env var that's shared with browser client

* Bump version to 4.0.2
  • Loading branch information
aburgel authored Mar 24, 2021
1 parent 6297469 commit 998c394
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Set up ENV vars in every environment:

* `MIXPANEL_TOKEN` - By default, TestTrack reports to Mixpanel. If you're using a [custom analytics provider](#analytics) you can omit this.
* `TEST_TRACK_API_URL` - Set this to the URL of your TestTrack instance with your app credentials, e.g. `http://[myapp]:[your new app password]@[your-app-domain]/`
* `TEST_TRACK_PUBLIC_API_URL` - (optional) If public traffic to TestTrack should use a different host name, set this variable. By default this will use `TEST_TRACK_API_URL` without any credentials

[your-app-domain] can be
* `testtrack.test`
Expand Down
2 changes: 1 addition & 1 deletion app/models/test_track/web_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def visitor_dsl

def state_hash
{
url: TestTrack.url,
url: TestTrack.public_url,
cookieDomain: cookie_domain,
cookieName: visitor_cookie_name,
splits: current_visitor.split_registry.to_hash,
Expand Down
14 changes: 9 additions & 5 deletions lib/test_track.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,15 @@ def update_config
yield(ConfigUpdater.new)
end

def url
def public_url
ENV['TEST_TRACK_PUBLIC_API_URL'] || _uncredentialed_private_url
end

def private_url
ENV['TEST_TRACK_API_URL']
end

def _uncredentialed_private_url
return nil unless private_url

full_uri = URI.parse(private_url)
Expand All @@ -124,10 +132,6 @@ def url
full_uri.to_s
end

def private_url
ENV['TEST_TRACK_API_URL']
end

def _build_timestamp
File.read(BUILD_TIMESTAMP_FILE_PATH).chomp.presence if File.exist?(BUILD_TIMESTAMP_FILE_PATH)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/test_track_rails_client/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module TestTrackRailsClient
VERSION = "4.0.1" # rubocop:disable Style/MutableConstant
VERSION = "4.0.2" # rubocop:disable Style/MutableConstant
end
14 changes: 12 additions & 2 deletions spec/models/test_track/web_session_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -540,8 +540,18 @@ def current_clown; end
allow(split_registry).to receive(:experience_sampling_weight).and_return(sampling_weight)
end

it "includes the test track URL" do
expect(subject.state_hash[:url]).to eq "http://testtrack.dev"
context 'when no public url is provided' do
it "defaults to the private test track URL" do
expect(subject.state_hash[:url]).to eq "http://testtrack.dev"
end
end

context 'when a public url is provided' do
it "uses the public test track URL" do
with_env TEST_TRACK_PUBLIC_API_URL: 'http://public.url.com' do
expect(subject.state_hash[:url]).to eq "http://public.url.com"
end
end
end

it "includes the cookie_domain" do
Expand Down
18 changes: 18 additions & 0 deletions spec/test_track_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,22 @@
end
end
end

describe '.public_url' do
context 'when TEST_TRACK_PUBLIC_API_URL is set' do
it 'returns it' do
with_env TEST_TRACK_PUBLIC_API_URL: 'http://public.url.com' do
expect(TestTrack.public_url).to eq 'http://public.url.com'
end
end
end

context 'when TEST_TRACK_PUBLIC_API_URL is not set' do
it 'returns private url with credentials removed' do
with_env TEST_TRACK_API_URL: 'http://user:pass@private.url.com' do
expect(TestTrack.public_url).to eq 'http://private.url.com'
end
end
end
end
end

0 comments on commit 998c394

Please sign in to comment.