/
client_spec.rb
139 lines (118 loc) · 4.63 KB
/
client_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
require "spec_helper"
RSpec.describe Unleash::Client do
it "Uses custom http headers when initializing client" do
WebMock.stub_request(:post, "http://test-url//client/register")
.with(
headers: {
'Accept'=>'*/*',
'Content-Type'=>'application/json',
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'User-Agent'=>'Ruby',
'X-Api-Key'=>'123'
})
.to_return(status: 200, body: "", headers: {})
WebMock.stub_request(:post, "http://test-url//client/metrics").
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: {})
Unleash.configure do |config|
config.url = 'http://test-url/'
config.app_name = 'my-test-app'
config.instance_id = 'rspec/test'
config.custom_http_headers = {'X-API-KEY' => '123'}
end
unleash_client = Unleash::Client.new(
url: 'http://test-url/',
app_name: 'my-test-app',
instance_id: 'rspec/test',
custom_http_headers: {'X-API-KEY' => '123'}
)
expect(
a_request(:post, "http://test-url//client/register")
.with( headers: {'Content-Type': 'application/json'})
.with( headers: {'X-API-KEY': '123', 'Content-Type': 'application/json'})
.with( headers: {'UNLEASH-APPNAME': 'my-test-app'})
.with( headers: {'UNLEASH-INSTANCEID': 'rspec/test'})
).to have_been_made.once
expect(
a_request(:get, "http://test-url//client/features")
.with( headers: {'X-API-KEY': '123'})
.with( headers: {'UNLEASH-APPNAME': 'my-test-app'})
.with( headers: {'UNLEASH-INSTANCEID': 'rspec/test'})
).to have_been_made.once
# Test now sending of metrics
Unleash.reporter.send
expect(
a_request(:post, "http://test-url//client/metrics")
.with( headers: {'Content-Type': 'application/json'})
.with( headers: {'X-API-KEY': '123', 'Content-Type': 'application/json'})
.with( headers: {'UNLEASH-APPNAME': 'my-test-app'})
.with( headers: {'UNLEASH-INSTANCEID': 'rspec/test'})
).to have_been_made.once
end
it "should not fail if we are provided no toggles from the unleash server" do
WebMock.stub_request(:post, "http://test-url//client/register")
.with(
headers: {
'Accept'=>'*/*',
'Content-Type'=>'application/json',
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'User-Agent'=>'Ruby',
'X-Api-Key'=>'123'
})
.to_return(status: 200, body: "", headers: {})
Unleash.configure do |config|
config.url = 'http://test-url/'
config.app_name = 'my-test-app'
config.instance_id = 'rspec/test'
config.disable_metrics = true
config.custom_http_headers = {'X-API-KEY' => '123'}
end
unleash_client = Unleash::Client.new(
url: 'http://test-url/',
app_name: 'my-test-app',
instance_id: 'rspec/test',
custom_http_headers: {'X-API-KEY' => '123'}
)
expect(
unleash_client.is_enabled?('any_feature', {}, true)
).to eq(true)
expect(WebMock).not_to have_requested(:get, 'http://test-url/')
expect(WebMock).to have_requested(:get, 'http://test-url//client/features')
expect(WebMock).to have_requested(:post, 'http://test-url//client/register')
expect(WebMock).not_to have_requested(:post, 'http://test-url//client/metrics')
end
it "should return default results if running with disable_client" do
Unleash.configure do |config|
config.disable_client = true
end
unleash_client = Unleash::Client.new
expect(
unleash_client.is_enabled?('any_feature', {}, true)
).to eq(true)
expect(
unleash_client.is_enabled?('any_feature2', {}, false)
).to eq(false)
end
it "should not connect anywhere if running with disable_client" do
Unleash.configure do |config|
config.disable_client = true
config.url = 'http://test-url/'
config.custom_http_headers = 'invalid_string'
end
unleash_client = Unleash::Client.new
expect(
unleash_client.is_enabled?('any_feature', {}, true)
).to eq(true)
expect(WebMock).not_to have_requested(:get, 'http://test-url/')
expect(WebMock).not_to have_requested(:get, 'http://test-url//client/features')
expect(WebMock).not_to have_requested(:post, 'http://test-url//client/features')
expect(WebMock).not_to have_requested(:post, 'http://test-url//client/register')
expect(WebMock).not_to have_requested(:post, 'http://test-url//client/metrics')
end
end