Skip to content

Commit 8c14502

Browse files
refact: avoid real http calls in unit tests (#249)
* refact: avoid real http calls * fix: header Co-authored-by: Tom Zurkan <[email protected]>
1 parent 3d6b45c commit 8c14502

File tree

1 file changed

+46
-18
lines changed

1 file changed

+46
-18
lines changed

spec/project_spec.rb

+46-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
#
4-
# Copyright 2016-2019, Optimizely and contributors
4+
# Copyright 2016-2020, Optimizely and contributors
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
@@ -716,8 +716,18 @@ def callback(_args); end
716716
end
717717

718718
describe '.Optimizely with config manager' do
719+
before(:example) do
720+
stub_request(:post, impression_log_url)
721+
stub_request(:get, 'https://cdn.optimizely.com/datafiles/valid_sdk_key.json')
722+
.with(
723+
headers: {
724+
'Content-Type' => 'application/json'
725+
}
726+
)
727+
.to_return(status: 200, body: config_body_JSON, headers: {})
728+
end
729+
719730
it 'should update config, send update notification when url is provided' do
720-
WebMock.allow_net_connect!
721731
notification_center = Optimizely::NotificationCenter.new(spy_logger, error_handler)
722732

723733
expect(notification_center).to receive(:send_notifications).with(
@@ -728,23 +738,22 @@ def callback(_args); end
728738

729739
expect(notification_center).to receive(:send_notifications).ordered
730740
http_project_config_manager = Optimizely::HTTPProjectConfigManager.new(
731-
url: 'https://cdn.optimizely.com/datafiles/QBw9gFM8oTn7ogY9ANCC1z.json',
741+
url: 'https://cdn.optimizely.com/datafiles/valid_sdk_key.json',
732742
notification_center: notification_center
733743
)
734744

735745
project_instance = Optimizely::Project.new(
736-
config_body_JSON, nil, spy_logger, error_handler,
746+
nil, nil, spy_logger, error_handler,
737747
false, nil, nil, http_project_config_manager, notification_center
738748
)
739749

740750
until http_project_config_manager.ready?; end
741751

742752
expect(http_project_config_manager.config).not_to eq(nil)
743-
expect(project_instance.activate('checkout_flow_experiment', 'test_user')).not_to eq(nil)
753+
expect(project_instance.activate('test_experiment', 'test_user')).not_to eq(nil)
744754
end
745755

746756
it 'should update config, send update notification when sdk key is provided' do
747-
WebMock.allow_net_connect!
748757
notification_center = Optimizely::NotificationCenter.new(spy_logger, error_handler)
749758

750759
expect(notification_center).to receive(:send_notifications).with(
@@ -755,25 +764,34 @@ def callback(_args); end
755764
expect(notification_center).to receive(:send_notifications).ordered
756765

757766
http_project_config_manager = Optimizely::HTTPProjectConfigManager.new(
758-
sdk_key: 'QBw9gFM8oTn7ogY9ANCC1z',
767+
sdk_key: 'valid_sdk_key',
759768
notification_center: notification_center
760769
)
761770

762771
project_instance = Optimizely::Project.new(
763-
config_body_JSON, nil, spy_logger, error_handler,
772+
nil, nil, spy_logger, error_handler,
764773
false, nil, nil, http_project_config_manager, notification_center
765774
)
766775

767776
until http_project_config_manager.ready?; end
768777

769778
expect(http_project_config_manager.config).not_to eq(nil)
770-
expect(project_instance.activate('checkout_flow_experiment', 'test_user')).not_to eq(nil)
779+
expect(project_instance.activate('test_experiment', 'test_user')).not_to eq(nil)
771780
end
772781
end
773782

774783
describe '.Optimizely with sdk key' do
784+
before(:example) do
785+
stub_request(:post, impression_log_url)
786+
stub_request(:get, 'https://cdn.optimizely.com/datafiles/valid_sdk_key.json')
787+
.with(
788+
headers: {
789+
'Content-Type' => 'application/json'
790+
}
791+
)
792+
.to_return(status: 200, body: config_body_JSON, headers: {})
793+
end
775794
it 'should update config, send update notification when sdk key is provided' do
776-
WebMock.allow_net_connect!
777795
notification_center = Optimizely::NotificationCenter.new(spy_logger, error_handler)
778796

779797
expect(notification_center).to receive(:send_notifications).with(
@@ -785,13 +803,13 @@ def callback(_args); end
785803

786804
project_instance = Optimizely::Project.new(
787805
nil, nil, spy_logger, error_handler,
788-
false, nil, 'QBw9gFM8oTn7ogY9ANCC1z', nil, notification_center
806+
false, nil, 'valid_sdk_key', nil, notification_center
789807
)
790808

791809
until project_instance.config_manager.ready?; end
792810

793811
expect(project_instance.is_valid).to be true
794-
expect(project_instance.activate('checkout_flow_experiment', 'test_user')).not_to eq(nil)
812+
expect(project_instance.activate('test_experiment', 'test_user')).not_to eq(nil)
795813
end
796814
end
797815
end
@@ -2801,9 +2819,20 @@ def callback(_args); end
28012819
end
28022820

28032821
describe '.close' do
2822+
before(:example) do
2823+
stub_request(:post, impression_log_url)
2824+
stub_request(:get, 'https://cdn.optimizely.com/datafiles/valid_sdk_key.json')
2825+
.with(
2826+
headers: {
2827+
'Content-Type' => 'application/json'
2828+
}
2829+
)
2830+
.to_return(status: 200, body: config_body_JSON, headers: {})
2831+
end
2832+
28042833
it 'should stop config manager and event processor when optimizely close is called' do
28052834
config_manager = Optimizely::HTTPProjectConfigManager.new(
2806-
sdk_key: 'QBw9gFM8oTn7ogY9ANCC1z',
2835+
sdk_key: 'valid_sdk_key',
28072836
start_by_default: true
28082837
)
28092838

@@ -2827,7 +2856,7 @@ def callback(_args); end
28272856

28282857
it 'should stop invalid object' do
28292858
http_project_config_manager = Optimizely::HTTPProjectConfigManager.new(
2830-
sdk_key: 'QBw9gFM8oTn7ogY9ANCC1z'
2859+
sdk_key: 'valid_sdk_key'
28312860
)
28322861

28332862
project_instance = Optimizely::Project.new(
@@ -2840,9 +2869,8 @@ def callback(_args); end
28402869
end
28412870

28422871
it 'shoud return optimizely as invalid for an API when close is called' do
2843-
WebMock.allow_net_connect!
28442872
http_project_config_manager = Optimizely::HTTPProjectConfigManager.new(
2845-
sdk_key: 'QBw9gFM8oTn7ogY9ANCC1z'
2873+
sdk_key: 'valid_sdk_key'
28462874
)
28472875

28482876
project_instance = Optimizely::Project.new(
@@ -2852,13 +2880,13 @@ def callback(_args); end
28522880

28532881
until http_project_config_manager.ready?; end
28542882

2855-
expect(project_instance.activate('checkout_flow_experiment', 'test_user')).not_to eq(nil)
2883+
expect(project_instance.activate('test_experiment', 'test_user')).not_to eq(nil)
28562884
expect(project_instance.is_valid).to be true
28572885

28582886
project_instance.close
28592887

28602888
expect(project_instance.is_valid).to be false
2861-
expect(project_instance.activate('checkout_flow_experiment', 'test_user')).to eq(nil)
2889+
expect(project_instance.activate('test_experiment', 'test_user')).to eq(nil)
28622890
end
28632891

28642892
it 'should not raise exception for static config manager' do

0 commit comments

Comments
 (0)