Skip to content

Commit

Permalink
Merge pull request #32 from projecthydra/webmock
Browse files Browse the repository at this point in the history
Webmock
  • Loading branch information
Jim Coble committed Sep 24, 2013
2 parents 01acc56 + 9f97057 commit ddfd5cf
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 42 deletions.
5 changes: 5 additions & 0 deletions lib/tasks/test.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
desc "Respect your authorities! Runs tests with Webmock turned off"
task :respect => :environment do
ENV["WEBMOCK"] ||= "disabled"
Rake::Task["spec"].invoke
end
32 changes: 17 additions & 15 deletions spec/controllers/terms_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,24 @@
response.code.should == "400"
end

it "should not return 400 if vocabulary is valid" do
get :index, { :q => "foo", :vocab => "loc" }
response.code.should_not == "400"
end

it "should return 400 if sub_authority is not valid" do
get :index, { :q => "foo", :vocab => "loc", :sub_authority => "foo" }
response.code.should == "400"
end

it "should not return 400 if sub_authority is valid" do
get :index, { :q => "foo", :vocab => "loc", :sub_authority => "relators" }
response.code.should_not == "400"
end
end

describe "successful queries" do

before :each do
WebMock.disable_net_connect!
stub_request(:get, "http://id.loc.gov/authorities/suggest/?q=Blues").
to_return(:body => File.new(Rails.root.join("spec/fixtures", "lcsh-response.txt")), :status => 200)
end

after :each do
WebMock.allow_net_connect!
to_return(:body => File.new(Rails.root.join("spec/fixtures", "lcsh-response.txt")), :status => 200)
stub_request(:get, "http://id.loc.gov/search/?format=json&q=").
with(:headers => {'Accept'=>'application/json'}).
to_return(:body => File.new(Rails.root.join("spec/fixtures", "loc-response.txt")), :status => 200)
stub_request(:get, "http://id.loc.gov/search/?format=json&q=cs:http://id.loc.gov/vocabulary/relators").
with(:headers => {'Accept'=>'application/json'}).
to_return(:body => File.new(Rails.root.join("spec/fixtures", "loc-response.txt")), :status => 200)
end

it "should return a set of terms for a lcsh query" do
Expand All @@ -58,6 +50,16 @@
response.should be_success
end

it "should not return 400 if vocabulary is valid" do
get :index, { :q => "foo", :vocab => "loc" }
response.code.should_not == "400"
end

it "should not return 400 if sub_authority is valid" do
get :index, { :q => "foo", :vocab => "loc", :sub_authority => "relators" }
response.code.should_not == "400"
end

end

end
Expand Down
8 changes: 0 additions & 8 deletions spec/lib/authorities_lcsh_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,12 @@
describe Authorities::Lcsh do

before :all do
WebMock.disable_net_connect!
stub_request(:get, "http://id.loc.gov/authorities/suggest/?q=ABBA").
to_return(:body => File.new(Rails.root.join("spec/fixtures", "lcsh-response.txt")), :status => 200)
@terms = Authorities::Lcsh.new "ABBA"
end

after :all do
WebMock.allow_net_connect!
end

describe "response from LOC" do

it "should have the query term for its first element" do
@terms.raw_response[0].should be_kind_of String
@terms.raw_response[0].should == "ABBA"
Expand All @@ -37,11 +31,9 @@
@terms.raw_response[3].should include "http://id.loc.gov/authorities/names/n98029154"
@terms.raw_response[3].length.should == 10
end

end

describe "presenting the results from LOC" do

it "should give us the query term" do
@terms.query.should == "ABBA"
end
Expand Down
13 changes: 1 addition & 12 deletions spec/lib/authorities_loc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,11 @@
describe Authorities::Loc do

before :all do
WebMock.disable_net_connect!
# TODO: Need a more elegant way to stub the request when the URL string is crazy long
# This uses the WebMock gem.
# Any requests to this URL will use the mock return data in the loc-response.txt file
stub_request(:get, "http://id.loc.gov/search/?format=json&q=haw*&q=cs:http://id.loc.gov/vocabulary/geographicAreas").
with(:headers => {'Accept'=>'application/json'}).
to_return(:body => File.new(Rails.root.join("spec/fixtures", "loc-response.txt")), :status => 200)
@authority = Authorities::Loc.new("haw*", "geographicAreas")
end

after :all do
WebMock.allow_net_connect!
end


it "should instantiate with a query and return data" do
expect(@authority).not_to be_nil
Expand All @@ -30,7 +21,6 @@
end

it "should not return a url for an invalid sub_authority" do
#authority = Authorities::Loc.new("software*")
@authority.should_not be_nil
url = @authority.sub_authorityURL("invalid sub_authority")
expect(url).to eq("")
Expand All @@ -41,6 +31,5 @@
json = @authority.parse_authority_response
expect(json).not_to be_empty
end



end
5 changes: 0 additions & 5 deletions spec/lib/authorities_oclcts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
describe Authorities::Oclcts do

before :each do
WebMock.disable_net_connect!
stub_request(:get, "http://tspilot.oclc.org/mesh/?maximumRecords=10&operation=searchRetrieve&query=oclcts.rootHeading%20exact%20%22ball*%22&recordPacking=xml&recordSchema=http://zthes.z3950.org/xml/1.0/&recordXPath=&resultSetTTL=300&sortKeys=&startRecord=1&version=1.1").
to_return(:body => File.new(Rails.root.join("spec/fixtures", "oclcts-response-mesh-1.txt")), :status => 200)
stub_request(:get, "http://tspilot.oclc.org/mesh/?maximumRecords=10&operation=searchRetrieve&query=oclcts.rootHeading%20exact%20%22alph*%22&recordPacking=xml&recordSchema=http://zthes.z3950.org/xml/1.0/&recordXPath=&resultSetTTL=300&sortKeys=&startRecord=1&version=1.1").
Expand All @@ -17,10 +16,6 @@
@second_query = Authorities::Oclcts.new("alph", "mesh")
end

after :each do
WebMock.allow_net_connect!
end

describe "a query for terms" do

it "should have an array of hashes that match the query" do
Expand Down
5 changes: 3 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
# Set WebMock to allow all HTTP requests until tests can be refactored
WebMock.allow_net_connect!

# Disable Webmock if we choose so we can test against the authorities, instead of their mocks
WebMock.disable! if ENV["WEBMOCK"] == "disabled"
end

0 comments on commit ddfd5cf

Please sign in to comment.