Skip to content

Commit 0e9c40a

Browse files
committed
Add support for proxy. Fixes #18.
1 parent 6ca13b4 commit 0e9c40a

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

.github/workflows/development.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
matrix:
1212
os:
1313
- ubuntu
14-
- macos
14+
# - macos
1515

1616
ruby:
1717
- "2.6"
@@ -32,6 +32,10 @@ jobs:
3232
ruby: head
3333
experimental: true
3434

35+
services:
36+
squid:
37+
image: sameersbn/squid
38+
3539
steps:
3640
- uses: actions/checkout@v2
3741
- uses: ruby/setup-ruby@v1
@@ -41,4 +45,6 @@ jobs:
4145

4246
- name: Run tests
4347
timeout-minutes: 5
48+
env:
49+
PROXY_URL: "http://localhost:3128"
4450
run: ${{matrix.env}} bundle exec rspec

lib/async/http/faraday/adapter.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,29 @@ def call(env)
6060
super
6161

6262
Sync do
63+
endpoint = Endpoint.parse(env[:url].to_s)
64+
65+
if proxy = env[:proxy]
66+
proxy_endpoint = Endpoint.parse(proxy)
67+
client = @internet.client_for(proxy_endpoint).proxied_endpoint(endpoint)
68+
else
69+
client = @internet.client_for(endpoint)
70+
end
71+
72+
if body = env[:body]
73+
body = Body::Buffered.wrap(body)
74+
end
75+
76+
if headers = env[:request_headers]
77+
headers = ::Protocol::HTTP::Headers[headers]
78+
end
79+
80+
method = env[:method].to_s.upcase
81+
82+
request = ::Protocol::HTTP::Request.new(endpoint.scheme, endpoint.authority, method, endpoint.path, nil, headers, body)
83+
6384
with_timeout do
64-
response = @internet.call(env[:method].to_s.upcase, env[:url].to_s, env[:request_headers], env[:body] || [])
85+
response = client.call(request)
6586

6687
save_response(env, response.status, response.read, response.headers)
6788
end
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in
13+
# all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
# THE SOFTWARE.
22+
23+
require 'async/http/faraday'
24+
require 'async/http/server'
25+
require 'async/http/endpoint'
26+
27+
require 'async'
28+
29+
RSpec.describe Async::HTTP::Faraday::Adapter, if: ENV.key?('PROXY_URL') do
30+
include_context Async::RSpec::Reactor
31+
32+
def get_response(url = endpoint.url, path = '/index', adapter_options: {})
33+
connection = Faraday.new(url: url) do |faraday|
34+
faraday.response :logger
35+
faraday.adapter :async_http, **adapter_options
36+
end
37+
38+
connection.get(path, proxy: ENV['PROXY_URL'])
39+
end
40+
41+
it "can get remote resource via proxy" do
42+
Async do
43+
response = get_response('http://www.google.com', '/search?q=cats')
44+
45+
expect(response).to be_success
46+
end
47+
end
48+
end

0 commit comments

Comments
 (0)