Skip to content

Commit

Permalink
Merge ea697cc into b42359d
Browse files Browse the repository at this point in the history
  • Loading branch information
andreas committed Nov 6, 2018
2 parents b42359d + ea697cc commit 17f22cc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 29 deletions.
6 changes: 2 additions & 4 deletions async-http-faraday.gemspec
Expand Up @@ -10,13 +10,11 @@ Gem::Specification.new do |spec|
spec.summary = "Provides an adaptor between async-http and faraday."
spec.homepage = "https://github.com/socketry/async-http"

spec.files = `git ls-files -z`.split("\x0").reject do |f|
f.match(%r{^(test|spec|features)/})
end
spec.files = [".editorconfig", ".gitignore", ".rspec", ".travis.yml", "Gemfile", "README.md", "Rakefile", "async-http-faraday.gemspec", "lib/async/http/faraday.rb", "lib/async/http/faraday/adapter.rb", "lib/async/http/faraday/version.rb"]
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency("async-http", "~> 0.5")
spec.add_dependency("async-http", "~> 0.37")
spec.add_dependency("faraday")

spec.add_development_dependency "async-rspec", "~> 1.2"
Expand Down
15 changes: 3 additions & 12 deletions lib/async/http/faraday/adapter.rb
Expand Up @@ -29,9 +29,9 @@ class Adapter < ::Faraday::Adapter
def call(env)
super

client = HTTP::Client.new(endpoints_for(env).to_a)
client = HTTP::Client.new(*endpoints_for(env).to_a)

response = client.send_request(env[:method], env[:url].request_uri, env[:request_headers], env[:body] || [])
response = client.send(env[:method], env[:url].request_uri, env[:request_headers], env[:body] || [])

save_response(env, response.status, response.body, response.headers, response.reason)

Expand All @@ -42,16 +42,7 @@ def endpoints_for(env)
return to_enum(:endpoints_for, env) unless block_given?

if url = env[:url]
port = url.port
port ||= url.scheme == 'https' ? 443 : 80

endpoint = Async::IO::Endpoint.tcp(url.hostname, port)

if url.scheme == 'https'
yield SecureEndpoint.new(endpoint, ssl_context: ssl_context_for(env[:ssl]))
else
yield endpoint
end
yield Async::HTTP::URLEndpoint.new(url)
end
end

Expand Down
26 changes: 13 additions & 13 deletions spec/async/http/faraday/adapter_spec.rb
Expand Up @@ -18,37 +18,37 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

require 'async/http/server'
require 'async/http/faraday'
require 'async/http/response'
require 'async/http/server'
require 'async/http/url_endpoint'
require 'async/reactor'

RSpec.describe Async::HTTP::Faraday::Adapter do
let(:server_addresses) {[
Async::IO::Endpoint.tcp('127.0.0.1', 9294, reuse_port: true)
]}
let(:endpoint) {
Async::HTTP::URLEndpoint.parse('http://127.0.0.1:9294')
}

it "client can get resource" do
server = Async::HTTP::Server.new(server_addresses)

def server.handle_request(request, peer, address)
[200, {}, ["Hello World"]]
app = ->(request) do
Async::HTTP::Response[200, {}, ["Hello World"]]
end
client = Async::HTTP::Client.new(server_addresses)

server = Async::HTTP::Server.new(app, endpoint)

Async::Reactor.run do |task|
server_task = task.async do
server.run
end

conn = Faraday.new(:url => 'http://127.0.0.1:9294') do |faraday|
conn = Faraday.new(:url => endpoint.url) do |faraday|
faraday.response :logger
faraday.adapter :async_http
end

response = conn.get("/index")

expect(response.body).to be == "Hello World"
expect(response.body.read).to be == "Hello World"

server_task.stop
end
Expand Down

0 comments on commit 17f22cc

Please sign in to comment.