/
configuration_spec.rb
144 lines (124 loc) · 5.83 KB
/
configuration_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
require "spec_helper"
require "unleash/configuration"
RSpec.describe Unleash do
describe 'Configuration' do
before do
Unleash.configuration = nil
end
it "should have the correct defaults" do
config = Unleash::Configuration.new
expect(config.app_name).to be_nil
expect(config.environment).to eq('default')
expect(config.url).to be_nil
expect(config.instance_id).to be_truthy
expect(config.custom_http_headers).to eq({})
expect(config.disable_metrics).to be_falsey
expect(config.refresh_interval).to eq(10)
expect(config.metrics_interval).to eq(30)
expect(config.timeout).to eq(30)
expect(config.retry_limit).to eq(1)
expect(config.backup_file).to_not be_nil
expect(config.project_name).to be_nil
expect{ config.validate! }.to raise_error(ArgumentError)
end
it "should by default be invalid" do
config = Unleash::Configuration.new
expect{ config.validate! }.to raise_error(ArgumentError)
end
it "should be valid with the mandatory arguments set" do
config = Unleash::Configuration.new(app_name: 'rspec_test', url: 'http://testurl/')
expect{ config.validate! }.not_to raise_error
end
it "should be lenient if disable_client is true" do
config = Unleash::Configuration.new(disable_client: true)
expect{ config.validate! }.not_to raise_error
end
it "support yield for setting the configuration" do
Unleash.configure do |config|
config.url = 'http://test-url/'
config.app_name = 'my-test-app'
end
expect{ Unleash.configuration.validate! }.not_to raise_error
expect(Unleash.configuration.url).to eq('http://test-url/')
expect(Unleash.configuration.app_name).to eq('my-test-app')
expect(Unleash.configuration.fetch_toggles_uri.to_s).to eq('http://test-url/client/features')
expect(Unleash.configuration.client_metrics_uri.to_s).to eq('http://test-url/client/metrics')
expect(Unleash.configuration.client_register_uri.to_s).to eq('http://test-url/client/register')
end
it "should build the correct unleash endpoints from the base url" do
config = Unleash::Configuration.new(url: 'https://testurl/api', app_name: 'test-app')
expect(config.url).to eq('https://testurl/api')
expect(config.fetch_toggles_uri.to_s).to eq('https://testurl/api/client/features')
expect(config.client_metrics_uri.to_s).to eq('https://testurl/api/client/metrics')
expect(config.client_register_uri.to_s).to eq('https://testurl/api/client/register')
end
it "should build the correct unleash endpoints from a base url ending with slash" do
config = Unleash::Configuration.new(url: 'https://testurl/api/', app_name: 'test-app')
expect(config.url).to eq('https://testurl/api/')
expect(config.fetch_toggles_uri.to_s).to eq('https://testurl/api/client/features')
expect(config.client_metrics_uri.to_s).to eq('https://testurl/api/client/metrics')
expect(config.client_register_uri.to_s).to eq('https://testurl/api/client/register')
end
it "should build the correct unleash endpoints from a base url ending with double slashes" do
config = Unleash::Configuration.new(url: 'https://testurl/api//', app_name: 'test-app')
expect(config.url).to eq('https://testurl/api//')
expect(config.fetch_toggles_uri.to_s).to eq('https://testurl/api//client/features')
expect(config.client_metrics_uri.to_s).to eq('https://testurl/api//client/metrics')
expect(config.client_register_uri.to_s).to eq('https://testurl/api//client/register')
end
it "should build the correct unleash features endpoint when project_name is used" do
config = Unleash::Configuration.new(url: 'https://testurl/api', app_name: 'test-app', project_name: 'test-project')
expect(config.fetch_toggles_uri.to_s).to eq('https://testurl/api/client/features?project=test-project')
end
it "should allow hashes for custom_http_headers via yield" do
Unleash.configure do |config|
config.url = 'http://test-url/'
config.app_name = 'my-test-app'
config.custom_http_headers = { 'X-API-KEY': '123' }
end
expect{ Unleash.configuration.validate! }.not_to raise_error
expect(Unleash.configuration.custom_http_headers).to eq({ 'X-API-KEY': '123' })
end
it "should allow hashes for custom_http_headers via new client" do
config = Unleash::Configuration.new(
url: 'https://testurl/api',
app_name: 'test-app',
custom_http_headers: { 'X-API-KEY': '123' }
)
expect{ config.validate! }.not_to raise_error
expect(config.custom_http_headers).to include({ 'X-API-KEY': '123' })
expect(config.http_headers).to include({ 'UNLEASH-APPNAME' => 'test-app' })
expect(config.http_headers).to include('UNLEASH-INSTANCEID')
end
it "should not accept invalid custom_http_headers via yield" do
expect do
Unleash.configure do |config|
config.url = 'http://test-url/'
config.app_name = 'my-test-app'
config.custom_http_headers = 123.456
end
end.to raise_error(ArgumentError)
end
it "should not accept invalid custom_http_headers via new client" do
WebMock \
.stub_request(:post, "http://test-url//client/register")
.with(
headers: {
'Accept' => '*/*',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Content-Type' => 'application/json',
'User-Agent' => 'Ruby'
}
)
.to_return(status: 200, body: "", headers: {})
expect do
Unleash::Client.new(
url: 'https://testurl/api',
app_name: 'test-app',
custom_http_headers: 123.0,
disable_metrics: true
)
end.to raise_error(ArgumentError)
end
end
end