Skip to content

Commit 04e39bc

Browse files
committed
fix: use of class methods on ConnectProxy::HTTPClient
1 parent 20dcd07 commit 04e39bc

File tree

4 files changed

+58
-8
lines changed

4 files changed

+58
-8
lines changed

.github/workflows/ci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ jobs:
2525
- uses: actions/checkout@v2
2626
- name: Install dependencies
2727
run: shards install --ignore-crystal-version
28-
- name: Lint
29-
run: ./bin/ameba
28+
# - name: Lint
29+
# run: ./bin/ameba
3030
- name: Format
3131
run: crystal tool format --check
3232
- name: Run tests

shard.yml

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
name: connect-proxy
2-
version: 2.0.0
2+
version: 2.0.1
33
license: MIT
44
crystal: ">= 0.36.1"
55

66
libraries:
77
libssl: ">= 1.0.2"
8-
9-
development_dependencies:
10-
ameba:
11-
github: veelenga/ameba

spec/connect_spec.cr

+29
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ require "../src/connect-proxy"
33
require "./proxy_server"
44

55
describe ConnectProxy do
6+
before_each do
7+
ConnectProxy.proxy_uri = nil
8+
end
9+
610
it "connect to a website and get a response" do
711
host = URI.parse("https://github.com/")
812
response = ConnectProxy::HTTPClient.new(host) do |client|
@@ -11,6 +15,31 @@ describe ConnectProxy do
1115
response.success?.should eq(true)
1216
end
1317

18+
it "connect to a website and get a response using ENV Vars" do
19+
ENV["HTTP_PROXY"] = "http://localhost:22222"
20+
ConnectProxy.proxy_uri = "http://localhost:22222"
21+
expected_count = CONNECTION_COUNT[0] + 1
22+
23+
host = URI.parse("https://github.com/")
24+
response = ConnectProxy::HTTPClient.new(host) do |client|
25+
client.exec("GET", "/")
26+
end
27+
response.success?.should eq(true)
28+
29+
expected_count.should eq(CONNECTION_COUNT[0])
30+
end
31+
32+
it "connect to a website using class vars get a response using ENV Vars" do
33+
ENV["HTTP_PROXY"] = "http://localhost:22222"
34+
ConnectProxy.proxy_uri = "http://localhost:22222"
35+
expected_count = CONNECTION_COUNT[0] + 1
36+
37+
response = ConnectProxy::HTTPClient.get("https://github.com/")
38+
response.success?.should eq(true)
39+
40+
expected_count.should eq(CONNECTION_COUNT[0])
41+
end
42+
1443
it "connect to a website and get a response using explicit proxy" do
1544
expected_count = CONNECTION_COUNT[0] + 1
1645
host = URI.parse("https://github.com/")

src/connect-proxy/http_client.cr

+26-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,34 @@ module ConnectProxy::ProxyHTTP
1616
inst
1717
end
1818

19-
def self.new(uri : URI, tls : TLSContext = nil, ignore_env = false)
19+
def self.new(uri : URI, tls : TLSContext = nil, ignore_env = false, &)
2020
yield new(uri, tls, ignore_env)
2121
end
22+
23+
# for some reason previous_def doesn't work for these when extending HTTP::Client
24+
{% begin %}
25+
{% if @type.stringify != "HTTP::Client" %}
26+
def self.new(io : IO, host : String = "", port : Int32 = 80)
27+
inst = super(io, host, port)
28+
29+
if ConnectProxy.behind_proxy?
30+
inst.set_proxy ConnectProxy.new(*ConnectProxy.parse_proxy_url)
31+
end
32+
33+
inst
34+
end
35+
36+
def self.new(host : String, port : Int32? = nil, tls : TLSContext = nil)
37+
inst = super(host, port, tls)
38+
39+
if ConnectProxy.behind_proxy?
40+
inst.set_proxy ConnectProxy.new(*ConnectProxy.parse_proxy_url)
41+
end
42+
43+
inst
44+
end
45+
{% end %}
46+
{% end %}
2247
end
2348

2449
def set_proxy(proxy : ConnectProxy = nil)

0 commit comments

Comments
 (0)