http_proxy_from_env 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.9.3"
4
+ - "2.0.0"
5
+
6
+ script: bundle exec rspec
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # HttpProxyFromEnv
2
2
 
3
3
  Net::HTTP automatically detects and uses proxies from the environment.
4
+
4
5
  http\_proxy\_from\_env is made from https://bugs.ruby-lang.org/projects/ruby-trunk/repository/revisions/36476
5
6
 
6
7
  ## Installation
data/Rakefile CHANGED
@@ -1 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["mechamosura@gmail.com"]
11
11
  spec.description = %q{Net::HTTP automatically detects and uses proxies from the environment.}
12
12
  spec.summary = %q{Net::HTTP automatically detects and uses proxies from the environment.}
13
- spec.homepage = ""
13
+ spec.homepage = "https://github.com/mechamogera/http_proxy_from_env"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
@@ -20,4 +20,5 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
22
  spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
23
24
  end
@@ -109,7 +109,7 @@ class Net::HTTP
109
109
 
110
110
  def proxy_user
111
111
  if @proxy_from_env then
112
- proxy_uri && proxy_uri.user
112
+ @proxy_user || proxy_uri && proxy_uri.user
113
113
  else
114
114
  @proxy_user
115
115
  end
@@ -117,7 +117,7 @@ class Net::HTTP
117
117
 
118
118
  def proxy_pass
119
119
  if @proxy_from_env then
120
- proxy_uri && proxy_uri.password
120
+ @proxy_pass || proxy_uri && proxy_uri.password
121
121
  else
122
122
  @proxy_pass
123
123
  end
@@ -1,3 +1,3 @@
1
1
  module HttpProxyFromEnv
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe HttpProxyFromEnv do
4
+ it 'should have a version number' do
5
+ expect(HttpProxyFromEnv::VERSION).to be_truthy
6
+ end
7
+ end
@@ -0,0 +1,144 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe Net::HTTP do
4
+ let(:address) { 'address' }
5
+ let(:proxy_address) { 'proxy.example' }
6
+ let(:proxy_port) { 8080 }
7
+ let(:proxy_user) { "proxy_user" }
8
+ let(:proxy_pass) { "proxy_pass" }
9
+ let(:proxy_uri) { "http://#{proxy_address}:#{proxy_port}" }
10
+ let(:proxy_uri_with_auth) { "http://#{proxy_user}:#{proxy_pass}@#{proxy_address}:#{proxy_port}" }
11
+
12
+ context 'proxy env' do
13
+ before do
14
+ ENV.delete('http_proxy')
15
+ ENV.delete('HTTP_PROXY')
16
+ end
17
+
18
+ describe "HTTP.new" do
19
+ let(:http) { http = Net::HTTP.new(address) }
20
+
21
+ it { expect(http.proxy?).to be_falsey }
22
+ it { expect(http.proxy_uri).to be_nil }
23
+ it { expect(http.proxy_from_env?).to be_truthy }
24
+ it { expect(http.proxy_address).to be_nil }
25
+ it { expect(http.proxy_port).to be_nil }
26
+ it { expect(http.proxy_user).to be_nil }
27
+ it { expect(http.proxy_pass).to be_nil }
28
+ end
29
+
30
+ describe "Proxy.new" do
31
+ let(:http) { Net::HTTP::Proxy().new(address) }
32
+
33
+ it { expect(http.proxy?).to be_falsey }
34
+ it { expect(http.proxy_uri).to be_nil }
35
+ it { expect(http.proxy_from_env?).to be_truthy }
36
+ it { expect(http.proxy_address).to be_nil }
37
+ it { expect(http.proxy_port).to be_nil }
38
+ it { expect(http.proxy_user).to be_nil }
39
+ it { expect(http.proxy_pass).to be_nil }
40
+ end
41
+ end
42
+
43
+ context 'proxy env' do
44
+ before do
45
+ ENV['http_proxy'] = proxy_uri
46
+ end
47
+
48
+ describe "HTTP.new" do
49
+ let(:http) { http = Net::HTTP.new(address) }
50
+
51
+ it { expect(http.proxy?).to be_truthy }
52
+ it { expect(http.proxy_uri).to eq(URI(proxy_uri)) }
53
+ it { expect(http.proxy_from_env?).to be_truthy }
54
+ it { expect(http.proxy_address).to eq(proxy_address) }
55
+ it { expect(http.proxy_port).to eq(proxy_port) }
56
+ it { expect(http.proxy_user).to be_nil }
57
+ it { expect(http.proxy_pass).to be_nil }
58
+ end
59
+
60
+ describe "Proxy.new" do
61
+ let(:http) { Net::HTTP::Proxy().new(address) }
62
+
63
+ it { expect(http.proxy?).to be_truthy }
64
+ it { expect(http.proxy_uri).to eq(URI(proxy_uri)) }
65
+ it { expect(http.proxy_from_env?).to be_truthy }
66
+ it { expect(http.proxy_address).to eq(proxy_address) }
67
+ it { expect(http.proxy_port).to eq(proxy_port) }
68
+ it { expect(http.proxy_user).to be_nil }
69
+ it { expect(http.proxy_pass).to be_nil }
70
+ end
71
+
72
+ describe "Proxy.new with auth" do
73
+ let(:http) { http = Net::HTTP::Proxy(:ENV, nil, proxy_user, proxy_pass).new(address) }
74
+
75
+ it { expect(http.proxy?).to be_truthy }
76
+ it { expect(http.proxy_uri).to eq(URI(proxy_uri)) }
77
+ it { expect(http.proxy_from_env?).to be_truthy }
78
+ it { expect(http.proxy_address).to eq(proxy_address) }
79
+ it { expect(http.proxy_port).to eq(proxy_port) }
80
+ it { expect(http.proxy_user).to eq(proxy_user) }
81
+ it { expect(http.proxy_pass).to eq(proxy_pass) }
82
+ end
83
+
84
+ describe "Proxy.new with proxy" do
85
+ let(:force_proxy_address) { "hoge_address" }
86
+ let(:force_proxy_port) { 1234 }
87
+ let(:force_proxy_user) { "hoge_user" }
88
+ let(:force_proxy_pass) { "hoge_pass" }
89
+ let(:http) { http = Net::HTTP::Proxy(force_proxy_address, force_proxy_port, force_proxy_user, force_proxy_pass).new(address) }
90
+
91
+ it { expect(http.proxy?).to be_truthy }
92
+ it { expect(http.proxy_uri).to eq(URI(proxy_uri)) }
93
+ it { expect(http.proxy_from_env?).to be_falsey }
94
+ it { expect(http.proxy_address).to eq(force_proxy_address) }
95
+ it { expect(http.proxy_port).to eq(force_proxy_port) }
96
+ it { expect(http.proxy_user).to eq(force_proxy_user) }
97
+ it { expect(http.proxy_pass).to eq(force_proxy_pass) }
98
+ end
99
+ end
100
+
101
+ context 'auth proxy env' do
102
+ before do
103
+ ENV['http_proxy'] = proxy_uri_with_auth
104
+ end
105
+
106
+ describe "HTTP.new" do
107
+ let(:http) { http = Net::HTTP.new(address) }
108
+
109
+ it { expect(http.proxy?).to be_truthy }
110
+ it { expect(http.proxy_uri).to eq(URI(proxy_uri_with_auth)) }
111
+ it { expect(http.proxy_from_env?).to be_truthy }
112
+ it { expect(http.proxy_address).to eq(proxy_address) }
113
+ it { expect(http.proxy_port).to eq(proxy_port) }
114
+ it { expect(http.proxy_user).to eq(proxy_user) }
115
+ it { expect(http.proxy_pass).to eq(proxy_pass) }
116
+ end
117
+
118
+ describe "Proxy.new" do
119
+ let(:http) { Net::HTTP::Proxy().new(address) }
120
+
121
+ it { expect(http.proxy?).to be_truthy }
122
+ it { expect(http.proxy_uri).to eq(URI(proxy_uri_with_auth)) }
123
+ it { expect(http.proxy_from_env?).to be_truthy }
124
+ it { expect(http.proxy_address).to eq(proxy_address) }
125
+ it { expect(http.proxy_port).to eq(proxy_port) }
126
+ it { expect(http.proxy_user).to eq(proxy_user) }
127
+ it { expect(http.proxy_pass).to eq(proxy_pass) }
128
+ end
129
+
130
+ describe "Proxy.new with auth" do
131
+ let(:force_proxy_user) { "hoge_user" }
132
+ let(:force_proxy_pass) { "hoge_pass" }
133
+ let(:http) { http = Net::HTTP::Proxy(:ENV, nil, force_proxy_user, force_proxy_pass).new(address) }
134
+
135
+ it { expect(http.proxy?).to be_truthy }
136
+ it { expect(http.proxy_uri).to eq(URI(proxy_uri_with_auth)) }
137
+ it { expect(http.proxy_from_env?).to be_truthy }
138
+ it { expect(http.proxy_address).to eq(proxy_address) }
139
+ it { expect(http.proxy_port).to eq(proxy_port) }
140
+ it { expect(http.proxy_user).to eq(force_proxy_user) }
141
+ it { expect(http.proxy_pass).to eq(force_proxy_pass) }
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'http_proxy_from_env'
@@ -0,0 +1,74 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe URI::Generic do
4
+ def with_env(envs = {}, &block)
5
+ ENV.clear
6
+ envs.each { |key, value| ENV[key] = value }
7
+ block.call if block
8
+ end
9
+
10
+ def env_from_description
11
+ a = self.class.description.split(/\s+\=\>\s+|\s*,\s*/)
12
+ Hash[*a]
13
+ end
14
+
15
+
16
+ describe "#find_proxy" do
17
+ let(:proxy_uri) { "http://192.168.0.1" }
18
+
19
+ context "request to other"
20
+ let(:request_uri) { "http://192.168.2.1" }
21
+
22
+ subject { with_env(env_from_description) { URI(request_uri).find_proxy } }
23
+
24
+ context "" do
25
+ it { is_expected.to be_nil }
26
+ end
27
+
28
+ context "http_proxy => http://192.168.0.1/" do
29
+ it { is_expected.to eq(URI(proxy_uri)) }
30
+ end
31
+
32
+ context "HTTP_PROXY => http://192.168.0.1/" do
33
+ it { is_expected.to eq(URI(proxy_uri)) }
34
+ end
35
+
36
+ context "REQUEST_METHOD => GET" do
37
+ it { is_expected.to be_nil }
38
+ end
39
+
40
+ context "CGI_HTTP_PROXY => http://192.168.0.1, REQUEST_METHOD => GET" do
41
+ it { is_expected.to eq(URI(proxy_uri)) }
42
+ end
43
+
44
+ context "http_proxy => http://192.168.0.1/, no_proxy => 192.168.2.1" do
45
+ it { is_expected.to be_nil }
46
+ end
47
+
48
+ context "http_proxy => http://192.168.0.1/, no_proxy => 192.168.2.1:8080" do
49
+ it { is_expected.to eq(URI(proxy_uri)) }
50
+ end
51
+
52
+ context "http_proxy => http://192.168.0.1/, no_proxy => 192.168.2.1:80" do
53
+ it { is_expected.to be_nil }
54
+ end
55
+
56
+ context "http_proxy => http://192.168.0.1/, no_proxy => 2.1:80" do
57
+ it { is_expected.to be_nil }
58
+ end
59
+
60
+ context "CGI_HTTP_PROXY => http://192.168.0.1, REQUEST_METHOD => GET, no_proxy => 192.168.2.1" do
61
+ it { is_expected.to be_nil }
62
+ end
63
+ end
64
+
65
+ context "request to own" do
66
+ let(:request_uri) { "http://localhost" }
67
+
68
+ subject { with_env(env_from_description) { URI(request_uri).find_proxy } }
69
+
70
+ context "http_proxy => http://192.168.0.1/" do
71
+ it { is_expected.to be_nil }
72
+ end
73
+ end
74
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http_proxy_from_env
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-06-23 00:00:00.000000000 Z
12
+ date: 2014-06-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -43,6 +43,22 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
46
62
  description: Net::HTTP automatically detects and uses proxies from the environment.
47
63
  email:
48
64
  - mechamosura@gmail.com
@@ -51,6 +67,7 @@ extensions: []
51
67
  extra_rdoc_files: []
52
68
  files:
53
69
  - .gitignore
70
+ - .travis.yml
54
71
  - Gemfile
55
72
  - LICENSE.txt
56
73
  - README.md
@@ -61,7 +78,11 @@ files:
61
78
  - lib/http_proxy_from_env/open_uri.rb
62
79
  - lib/http_proxy_from_env/uri/generic.rb
63
80
  - lib/http_proxy_from_env/version.rb
64
- homepage: ''
81
+ - spec/http_proxy_from_env_spec.rb
82
+ - spec/net/http_spec.rb
83
+ - spec/spec_helper.rb
84
+ - spec/uri/generic_spec.rb
85
+ homepage: https://github.com/mechamogera/http_proxy_from_env
65
86
  licenses:
66
87
  - MIT
67
88
  post_install_message:
@@ -76,7 +97,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
76
97
  version: '0'
77
98
  segments:
78
99
  - 0
79
- hash: 2997952803154654772
100
+ hash: 4065806883800370426
80
101
  required_rubygems_version: !ruby/object:Gem::Requirement
81
102
  none: false
82
103
  requirements:
@@ -85,11 +106,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
106
  version: '0'
86
107
  segments:
87
108
  - 0
88
- hash: 2997952803154654772
109
+ hash: 4065806883800370426
89
110
  requirements: []
90
111
  rubyforge_project:
91
112
  rubygems_version: 1.8.25
92
113
  signing_key:
93
114
  specification_version: 3
94
115
  summary: Net::HTTP automatically detects and uses proxies from the environment.
95
- test_files: []
116
+ test_files:
117
+ - spec/http_proxy_from_env_spec.rb
118
+ - spec/net/http_spec.rb
119
+ - spec/spec_helper.rb
120
+ - spec/uri/generic_spec.rb