Skip to content

Commit

Permalink
removed the dependency on HTTPI /cc #27
Browse files Browse the repository at this point in the history
Since Wasabi only does GET requests, it only needs a very simple
interface for sending HTTP requests which works great with testing
and allows you to use any HTTP client via a very simple adapter.

see spec/support/http_mock.rb for an example.
  • Loading branch information
rubiii committed May 29, 2013
1 parent b674622 commit 320ef2a
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 76 deletions.
4 changes: 2 additions & 2 deletions lib/wasabi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class Wasabi
SOAP_1_1 = 'http://schemas.xmlsoap.org/wsdl/soap/'
SOAP_1_2 = 'http://schemas.xmlsoap.org/wsdl/soap12/'

def initialize(wsdl, request = nil)
resolver = Resolver.new(request)
def initialize(wsdl, http = nil)
resolver = Resolver.new(http)
importer = Importer.new(resolver, self)

@documents, @schemas = importer.import(wsdl)
Expand Down
11 changes: 0 additions & 11 deletions lib/wasabi/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,4 @@ class Wasabi

Error = Class.new(StandardError)

HTTPError = Class.new(Error) do

def initialize(message, response = nil)
super(message)
@response = response
end

attr_reader :response

end

end
13 changes: 3 additions & 10 deletions lib/wasabi/resolver.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
require 'httpi'

class Wasabi
class Resolver

URL_PATTERN = /^http[s]?:/
XML_PATTERN = /^</

def initialize(request = nil)
@request = request || HTTPI::Request.new
def initialize(http)
@http = http
end

def resolve(location)
Expand All @@ -21,12 +19,7 @@ def resolve(location)
private

def load_from_remote(location)
@request.url = location
response = HTTPI.get(@request)

raise HTTPError.new("Error: #{response.code}", response) if response.error?

response.body
@http.get(location)
end

def load_from_disc(location)
Expand Down
35 changes: 35 additions & 0 deletions spec/support/http_mock.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module SpecSupport

class HTTPMock

MockError = Class.new(StandardError)

def initialize
@fakes = {}
end

def get(url)
@fakes[url] or raise_mock_error! url
end

def fake_request(url, fixture)
@fakes[url] = load_fixture(fixture)
end

private

def load_fixture(fixture)
Fixture.new(fixture).read
end

def raise_mock_error!(url)
raise MockError, "Unmocked HTTP request to #{url.inspect}"
end

end

def http_mock
@http_mock ||= HTTPMock.new
end

end
8 changes: 0 additions & 8 deletions spec/support/mock_request.rb

This file was deleted.

10 changes: 5 additions & 5 deletions spec/wasabi/integration/bookt_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
describe Wasabi do
context 'with: bookt.wsdl' do

subject(:wsdl) { Wasabi.new(wsdl_url) }
subject(:wsdl) { Wasabi.new(wsdl_url, http_mock) }

let(:wsdl_url) { 'http://connect.bookt.com/svc/connect.svc?wsdl' }
let(:wsdl2_url) { 'http://connect.bookt.com/svc/connect.svc?wsdl=wsdl1' }
let(:wsdl3_url) { 'http://connect.bookt.com/svc/connect.svc?wsdl=wsdl0' }

before do
mock_request wsdl_url, 'bookt/bookt.wsdl'
mock_request wsdl2_url, 'bookt/bookt2.wsdl'
mock_request wsdl3_url, 'bookt/bookt3.wsdl'
http_mock.fake_request(wsdl_url, 'bookt/bookt.wsdl')
http_mock.fake_request(wsdl2_url, 'bookt/bookt2.wsdl')
http_mock.fake_request(wsdl3_url, 'bookt/bookt3.wsdl')

# 16 schemas to import
schema_import_base = 'http://connect.bookt.com/svc/connect.svc?xsd=xsd%d'
(0..15).each do |i|
url = schema_import_base % i
mock_request url, "bookt/bookt#{i}.xsd"
http_mock.fake_request(url, "bookt/bookt#{i}.xsd")
end
end

Expand Down
8 changes: 4 additions & 4 deletions spec/wasabi/integration/bydexchange_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
describe Wasabi do
context 'with: bydexchange.wsdl' do

subject(:wsdl) { Wasabi.new(wsdl_url) }
subject(:wsdl) { Wasabi.new(wsdl_url, http_mock) }

let(:wsdl_url) { 'http://bydexchange.nbs-us.com/BYDExchangeServer.svc?wsdl' }
let(:wsdl2_url) { 'http://bydexchange.nbs-us.com/BYDExchangeServer.svc?wsdl=wsdl0' }

before do
mock_request wsdl_url, 'bydexchange/bydexchange.wsdl'
mock_request wsdl2_url, 'bydexchange/bydexchange2.wsdl'
http_mock.fake_request(wsdl_url, 'bydexchange/bydexchange.wsdl')
http_mock.fake_request(wsdl2_url, 'bydexchange/bydexchange2.wsdl')

# 8 schemas to import
schema_import_base = 'http://bydexchange.nbs-us.com/BYDExchangeServer.svc?xsd=xsd%d'
(0..8).each do |i|
url = schema_import_base % i
mock_request url, "bydexchange/bydexchange#{i}.xsd"
http_mock.fake_request(url, "bydexchange/bydexchange#{i}.xsd")
end
end

Expand Down
66 changes: 32 additions & 34 deletions spec/wasabi/resolver_spec.rb
Original file line number Diff line number Diff line change
@@ -1,40 +1,38 @@
require "spec_helper"
require 'spec_helper'

describe Wasabi::Resolver do

describe "#resolve" do
it "resolves remote documents" do
HTTPI.expects(:get).returns HTTPI::Response.new(200, {}, "wsdl")
xml = Wasabi::Resolver.new.resolve("http://example.com?wsdl")
xml.should == "wsdl"
end

it "resolves local documents" do
xml = Wasabi::Resolver.new.resolve(fixture(:authentication).path)
xml.should == fixture(:authentication).read
end

it "simply returns raw XML" do
xml = Wasabi::Resolver.new.resolve("<xml/>")
xml.should == "<xml/>"
end

it "raises HTTPError when #load_from_remote gets a response error" do
code = 404
headers = {
"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)
HTTPI.stubs(:get).returns(failed_response)

expect { Wasabi::Resolver.new.resolve("http://example.com?wsdl") }.to raise_error { |error|
error.should be_a(Wasabi::Resolver::HTTPError)
error.message.should == "Error: #{code}"
error.response.should == failed_response
}
end
subject(:resolver) { Wasabi::Resolver.new(http_test_client) }

let(:http_test_client) {
Class.new {

def get(url)
"raw_response for #{url}"
end

}.new
}

it 'resolves remote files using a simple HTTP client interface' do
url = 'http://example.com?wsdl'

xml = resolver.resolve(url)
expect(xml).to eq("raw_response for #{url}")
end

it 'resolves local files' do
fixture = fixture(:authentication)

xml = resolver.resolve(fixture.path)
expect(xml).to eq(fixture.read)
end

it 'simply returns any raw input' do
string = '<xml/>'

xml = resolver.resolve(string)
expect(xml).to eq(string)
end

end
3 changes: 1 addition & 2 deletions wasabi.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ Gem::Specification.new do |s|

s.rubyforge_project = s.name

s.add_dependency "httpi", "~> 2.0"
s.add_dependency "nokogiri", ">= 1.4.0"
s.add_dependency "nokogiri", ">= 1.4"

s.add_development_dependency "rake", "~> 10.0"
s.add_development_dependency "rspec", "~> 2.13"
Expand Down

0 comments on commit 320ef2a

Please sign in to comment.