Skip to content

Commit

Permalink
feat(config): Support setting proxy via https_proxy env variable (#437)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cawllec authored and kattrali committed Apr 5, 2018
1 parent 8fb2117 commit 1b9448e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
20 changes: 13 additions & 7 deletions lib/bugsnag/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,9 @@ def initialize
# Read the API key from the environment
self.api_key = ENV["BUGSNAG_API_KEY"]

# Read NET::HTTP proxy environment variable
if ENV["http_proxy"]
uri = URI.parse(ENV["http_proxy"])
self.proxy_host = uri.host
self.proxy_port = uri.port
self.proxy_user = uri.user
self.proxy_password = uri.password
# Read NET::HTTP proxy environment variables
if (proxy_uri = ENV["https_proxy"] || ENV['http_proxy'])
parse_proxy(proxy_uri)
end

# Set up logging
Expand Down Expand Up @@ -184,6 +180,16 @@ def debug(message)
logger.debug(PROG_NAME) { message }
end

##
# Parses and sets proxy from a uri
def parse_proxy(uri)
proxy = URI.parse(uri)
self.proxy_host = proxy.host
self.proxy_port = proxy.port
self.proxy_user = proxy.user
self.proxy_password = proxy.password
end

private

PROG_NAME = "[Bugsnag]"
Expand Down
49 changes: 49 additions & 0 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,55 @@
end
end

describe "set_proxy" do
it "defaults proxy settings to nil" do
expect(subject.proxy_host).to be nil
expect(subject.proxy_port).to be nil
expect(subject.proxy_user).to be nil
expect(subject.proxy_password).to be nil
end

it "allows proxy settings to be set directly" do
subject.proxy_host = "http://localhost"
subject.proxy_port = 34000
subject.proxy_user = "user"
subject.proxy_password = "password"
expect(subject.proxy_host).to eq("http://localhost")
expect(subject.proxy_port).to eq(34000)
expect(subject.proxy_user).to eq("user")
expect(subject.proxy_password).to eq("password")
end

it "parses a uri if provided" do
subject.parse_proxy("http://user:password@localhost:34000")
expect(subject.proxy_host).to eq("localhost")
expect(subject.proxy_port).to eq(34000)
expect(subject.proxy_user).to eq("user")
expect(subject.proxy_password).to eq("password")
end

it "automatically parses http_proxy environment variable" do
ENV['http_proxy'] = "http://user:password@localhost:34000"
expect(subject.proxy_host).to eq("localhost")
expect(subject.proxy_port).to eq(34000)
expect(subject.proxy_user).to eq("user")
expect(subject.proxy_password).to eq("password")
end

it "automatically parses https_proxy environment variable" do
ENV['https_proxy'] = "https://user:password@localhost:34000"
expect(subject.proxy_host).to eq("localhost")
expect(subject.proxy_port).to eq(34000)
expect(subject.proxy_user).to eq("user")
expect(subject.proxy_password).to eq("password")
end

after do
ENV['http_proxy'] = nil
ENV['https_proxy'] = nil
end
end

describe "logger" do
class TestLogger
attr_accessor :logs
Expand Down

0 comments on commit 1b9448e

Please sign in to comment.