Skip to content

Commit 0b0dcb7

Browse files
authored
Merge pull request #87 from launchdarkly/proxy
Add http proxy support
2 parents 8a2969a + 202d136 commit 0b0dcb7

File tree

5 files changed

+68
-2
lines changed

5 files changed

+68
-2
lines changed

ldclient-rb.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Gem::Specification.new do |spec|
3131
spec.add_runtime_dependency "net-http-persistent", "~> 2.9"
3232
spec.add_runtime_dependency "concurrent-ruby", "~> 1.0.4"
3333
spec.add_runtime_dependency "hashdiff", "~> 0.2"
34-
spec.add_runtime_dependency "ld-celluloid-eventsource", "~> 0.9.0"
34+
spec.add_runtime_dependency "ld-celluloid-eventsource", "~> 0.10.0"
3535
spec.add_runtime_dependency "celluloid", "~> 0.18.0.pre" # transitive dep; specified here for more control
3636

3737
if RUBY_VERSION >= '2.2.2'

lib/ldclient-rb/config.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def initialize(opts = {})
5656
@stream = opts.has_key?(:stream) ? opts[:stream] : Config.default_stream
5757
@offline = opts.has_key?(:offline) ? opts[:offline] : Config.default_offline
5858
@poll_interval = opts.has_key?(:poll_interval) && opts[:poll_interval] > 1 ? opts[:poll_interval] : Config.default_poll_interval
59+
@proxy = opts[:proxy] || Config.default_proxy
5960
end
6061

6162
#
@@ -143,6 +144,11 @@ def offline?
143144
#
144145
attr_reader :feature_store
145146

147+
148+
# The proxy configuration string
149+
#
150+
attr_reader :proxy
151+
146152
#
147153
# The default LaunchDarkly client configuration. This configuration sets
148154
# reasonable defaults for most users.
@@ -184,6 +190,10 @@ def self.default_connect_timeout
184190
2
185191
end
186192

193+
def self.default_proxy
194+
nil
195+
end
196+
187197
def self.default_logger
188198
if defined?(Rails) && Rails.respond_to?(:logger)
189199
Rails.logger

lib/ldclient-rb/requestor.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ def make_request(path)
3030
req.headers["User-Agent"] = "RubyClient/" + LaunchDarkly::VERSION
3131
req.options.timeout = @config.read_timeout
3232
req.options.open_timeout = @config.connect_timeout
33+
if @config.proxy
34+
req.options.proxy = Faraday::ProxyOptions.from @config.proxy
35+
end
3336
end
3437

3538
@config.logger.debug("[LDClient] Got response from uri: #{uri}\n\tstatus code: #{res.status}\n\theaders: #{res.headers}\n\tbody: #{res.body}")

lib/ldclient-rb/stream.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def start
3333
'Authorization' => @sdk_key,
3434
'User-Agent' => 'RubyClient/' + LaunchDarkly::VERSION
3535
}
36-
opts = {:headers => headers, :with_credentials => true}
36+
opts = {:headers => headers, :with_credentials => true, :proxy => @config.proxy}
3737
@es = Celluloid::EventSource.new(@config.stream_uri + "/flags", opts) do |conn|
3838
conn.on(PUT) { |message| process_message(message, PUT) }
3939
conn.on(PATCH) { |message| process_message(message, PATCH) }

spec/requestor_spec.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
require "spec_helper"
2+
require "faraday"
3+
4+
describe LaunchDarkly::Requestor do
5+
describe ".request_all_flags" do
6+
describe "with a proxy" do
7+
let(:requestor) {
8+
LaunchDarkly::Requestor.new(
9+
"key",
10+
LaunchDarkly::Config.new({
11+
:proxy => "http://proxy.com",
12+
:base_uri => "http://ld.com"
13+
})
14+
)
15+
}
16+
it "converts the proxy option" do
17+
faraday = Faraday.new
18+
requestor.instance_variable_set(:@client, faraday)
19+
allow(faraday).to receive(:get) do |*args, &block|
20+
req = double(Faraday::Request, :headers => {}, :options => Faraday::RequestOptions.new)
21+
block.call(req)
22+
expect(args).to eq ['http://ld.com/sdk/latest-flags']
23+
expect(req.options.proxy[:uri]).to eq URI("http://proxy.com")
24+
double(body: '{"foo": "bar"}', status: 200, headers: {})
25+
end
26+
27+
requestor.request_all_flags()
28+
end
29+
end
30+
describe "without a proxy" do
31+
let(:requestor) {
32+
LaunchDarkly::Requestor.new(
33+
"key",
34+
LaunchDarkly::Config.new({
35+
:base_uri => "http://ld.com"
36+
})
37+
)
38+
}
39+
it "converts the proxy option" do
40+
faraday = Faraday.new
41+
requestor.instance_variable_set(:@client, faraday)
42+
allow(faraday).to receive(:get) do |*args, &block|
43+
req = double(Faraday::Request, :headers => {}, :options => Faraday::RequestOptions.new)
44+
block.call(req)
45+
expect(args).to eq ['http://ld.com/sdk/latest-flags']
46+
expect(req.options.proxy).to eq nil
47+
double(body: '{"foo": "bar"}', status: 200, headers: {})
48+
end
49+
requestor.request_all_flags()
50+
end
51+
end
52+
end
53+
end

0 commit comments

Comments
 (0)