Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to be compatible with Faraday #116

Merged
merged 4 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/wasabi/resolver.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require "httpi"
require "faraday"

module Wasabi

Expand All @@ -22,7 +22,7 @@ def initialize(message, response=nil)

def initialize(document, request = nil, adapter = nil)
@document = document
@request = request || HTTPI::Request.new
@request = request || Faraday.new
@adapter = adapter
end

Expand All @@ -41,10 +41,10 @@ def resolve
private

def load_from_remote
request.url = document
response = HTTPI.get(request, adapter)
request.adapter *adapter if adapter
response = request.get(document)

raise HTTPError.new("Error: #{response.code} for url #{request.url}", response) if response.error?
raise HTTPError.new("Error: #{response.status} for url #{document}", response) unless response.success?
LukeIGS marked this conversation as resolved.
Show resolved Hide resolved

response.body
end
Expand Down
2 changes: 1 addition & 1 deletion lib/wasabi/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Wasabi
VERSION = '4.0.0'
VERSION = '5.0.0'
end
20 changes: 0 additions & 20 deletions spec/support/adapter.rb

This file was deleted.

8 changes: 8 additions & 0 deletions spec/support/responses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Responses
class << self
def mock_faraday(code, headers, body)
env = Faraday::Env.from(status: code, response_headers: headers, response_body: body)
Faraday::Response.new(env)
end
end
end
8 changes: 6 additions & 2 deletions spec/wasabi/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
subject { Wasabi::Document.new fixture(:authentication).read }

it "accepts a URL" do
expect(HTTPI).to receive(:get) { HTTPI::Response.new(200, {}, "wsdl") }
path = 'http://example.com?wsdl'
stubs = Faraday::Adapter::Test::Stubs.new
stubs.get(path) do
[200, {'Content-Type': 'application/xml'}, 'wsdl']
end

document = Wasabi::Document.new("http://example.com?wsdl")
document = Wasabi::Document.new(path, [:test, stubs])
expect(document.xml).to eq("wsdl")
end

Expand Down
31 changes: 16 additions & 15 deletions spec/wasabi/resolver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@

describe "#resolve" do
it "resolves remote documents" do
expect(HTTPI).to receive(:get) { HTTPI::Response.new(200, {}, "wsdl") }
expect(Faraday::Connection).to receive(:new).and_return(
connection = instance_double(Faraday::Connection, get: Responses.mock_faraday(200, {}, "wsdl"))
)
xml = Wasabi::Resolver.new("http://example.com?wsdl").resolve
expect(xml).to eq("wsdl")
end

it "resolves remote documents with custom adapter" do
prev_logging = HTTPI.instance_variable_get(:@log)
HTTPI.log = false # Don't pollute rspec output by request logging
xml = Wasabi::Resolver.new("http://example.com?wsdl", nil, :fake_adapter_for_test).resolve
expect(xml).to eq("wsdl_by_adapter")
expect(FakeAdapterForTest.class_variable_get(:@@requests).size).to eq(1)
expect(FakeAdapterForTest.class_variable_get(:@@requests).first.url).to eq(URI.parse("http://example.com?wsdl"))
expect(FakeAdapterForTest.class_variable_get(:@@methods)).to eq([:get])
HTTPI.log = prev_logging
path = 'http://example.com?wsdl'
stubs = Faraday::Adapter::Test::Stubs.new
stubs.get(path) do
[200, {'Content-Type': 'application/xml'}, 'wsdl']
end
xml = Wasabi::Resolver.new("http://example.com?wsdl", nil, [:test, stubs]).resolve
expect(xml).to eq("wsdl")
end

it "resolves local documents" do
Expand All @@ -38,19 +39,19 @@
"content-type" => "text/html"
}
body = "<html><head><title>404 Not Found</title></head><body>Oops!</body></html>"
failed_response = HTTPI::Response.new(code, headers, body)

expect(HTTPI).to receive(:get) { failed_response }

failed_response = Responses.mock_faraday(code, headers, body)
expect(Faraday::Connection).to receive(:new).and_return(
connection = instance_double(Faraday::Connection, get: failed_response)
)
url = "http://example.com?wsdl"

expect do
Wasabi::Resolver.new(url).resolve
end.to raise_error { |ex|
end.to(raise_error{ |ex|
expect(ex).to be_a(Wasabi::Resolver::HTTPError)
expect(ex.message).to eq("Error: #{code} for url #{url}")
expect(ex.response).to eq(failed_response)
}
})
end
end

Expand Down
2 changes: 1 addition & 1 deletion wasabi.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Gem::Specification.new do |s|

s.license = 'MIT'

s.add_dependency "httpi", "~> 3.0"
s.add_dependency "faraday", "~> 2.8"
s.add_dependency "nokogiri", ">= 1.13.9"
s.add_dependency "addressable"

Expand Down