Skip to content

Commit

Permalink
fix: use of class methods on ConnectProxy::HTTPClient
Browse files Browse the repository at this point in the history
  • Loading branch information
stakach committed May 24, 2024
1 parent 20dcd07 commit 04e39bc
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ jobs:
- uses: actions/checkout@v2
- name: Install dependencies
run: shards install --ignore-crystal-version
- name: Lint
run: ./bin/ameba
# - name: Lint
# run: ./bin/ameba
- name: Format
run: crystal tool format --check
- name: Run tests
Expand Down
6 changes: 1 addition & 5 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
name: connect-proxy
version: 2.0.0
version: 2.0.1
license: MIT
crystal: ">= 0.36.1"

libraries:
libssl: ">= 1.0.2"

development_dependencies:
ameba:
github: veelenga/ameba
29 changes: 29 additions & 0 deletions spec/connect_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ require "../src/connect-proxy"
require "./proxy_server"

describe ConnectProxy do
before_each do
ConnectProxy.proxy_uri = nil
end

it "connect to a website and get a response" do
host = URI.parse("https://github.com/")
response = ConnectProxy::HTTPClient.new(host) do |client|
Expand All @@ -11,6 +15,31 @@ describe ConnectProxy do
response.success?.should eq(true)
end

it "connect to a website and get a response using ENV Vars" do
ENV["HTTP_PROXY"] = "http://localhost:22222"
ConnectProxy.proxy_uri = "http://localhost:22222"
expected_count = CONNECTION_COUNT[0] + 1

host = URI.parse("https://github.com/")
response = ConnectProxy::HTTPClient.new(host) do |client|
client.exec("GET", "/")
end
response.success?.should eq(true)

expected_count.should eq(CONNECTION_COUNT[0])
end

it "connect to a website using class vars get a response using ENV Vars" do
ENV["HTTP_PROXY"] = "http://localhost:22222"
ConnectProxy.proxy_uri = "http://localhost:22222"
expected_count = CONNECTION_COUNT[0] + 1

response = ConnectProxy::HTTPClient.get("https://github.com/")
response.success?.should eq(true)

expected_count.should eq(CONNECTION_COUNT[0])
end

it "connect to a website and get a response using explicit proxy" do
expected_count = CONNECTION_COUNT[0] + 1
host = URI.parse("https://github.com/")
Expand Down
27 changes: 26 additions & 1 deletion src/connect-proxy/http_client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,34 @@ module ConnectProxy::ProxyHTTP
inst
end

def self.new(uri : URI, tls : TLSContext = nil, ignore_env = false)
def self.new(uri : URI, tls : TLSContext = nil, ignore_env = false, &)
yield new(uri, tls, ignore_env)
end

# for some reason previous_def doesn't work for these when extending HTTP::Client
{% begin %}
{% if @type.stringify != "HTTP::Client" %}
def self.new(io : IO, host : String = "", port : Int32 = 80)
inst = super(io, host, port)

if ConnectProxy.behind_proxy?
inst.set_proxy ConnectProxy.new(*ConnectProxy.parse_proxy_url)
end

inst
end

def self.new(host : String, port : Int32? = nil, tls : TLSContext = nil)
inst = super(host, port, tls)

if ConnectProxy.behind_proxy?
inst.set_proxy ConnectProxy.new(*ConnectProxy.parse_proxy_url)
end

inst
end
{% end %}
{% end %}
end

def set_proxy(proxy : ConnectProxy = nil)
Expand Down

0 comments on commit 04e39bc

Please sign in to comment.