Skip to content
Browse files

use of webmock so that no live requests are made unless requested (e.…

…g. pass LIVE=true to rake spec)
  • Loading branch information...
1 parent 008e32e commit 0a2d64a37ed71f707069758b4606389bf5f5b1f6 Martin Sadler committed
Showing with 41 additions and 36 deletions.
  1. +7 −0 spec/spec_helper.rb
  2. +34 −36 spec/spy_spec.rb
View
7 spec/spec_helper.rb
@@ -6,6 +6,13 @@
require 'twitter'
require 'mechanize'
+require 'webmock/rspec'
+include WebMock
+
+LIVE = ENV['LIVE']
+WebMock.allow_net_connect! if LIVE
+
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'net-http-spy')
View
70 spec/spy_spec.rb
@@ -7,22 +7,21 @@
end
describe "a get request with default options" do
-
+
before(:all) do
- Net::HTTP.http_logger.reset!
- Twitter::Search.new('httparty').each { |r| r }
+ stub_request(:any, "search.twitter.com/search.json?q=httparty").to_return(:body => "\{\"results\"\: 1\}", :status => 200) unless LIVE
Net::HTTP.http_logger_options = :default
-
+ Twitter::Search.new('httparty').fetch
end
-
+
it "should give the connection" do
Net::HTTP.http_logger.lines.should include("CONNECT: [\"search.twitter.com\", 80]")
end
-
+
it "should give GET uri and query string" do
Net::HTTP.http_logger.lines.should include("GET /search.json?q=httparty")
end
-
+
it "should give the BODY response code" do
Net::HTTP.http_logger.lines.should include("BODY: Net::HTTPOK")
end
@@ -31,56 +30,55 @@
describe "a get request with body option set to true" do
before(:each) do
- Net::HTTP.http_logger.reset!
+ stub_request(:any, "search.twitter.com/search.json?q=httparty").to_return(:body => "\{\"results\"\: 1\}", :status => 200) unless LIVE
Net::HTTP.http_logger_options = {:body => true}
end
it "should give the body output" do
- Twitter::Search.new('httparty').each { |r| r }
- Net::HTTP.http_logger.lines.grep(/BODY: \{\"results\":/).should_not be_empty
+ Twitter::Search.new('httparty').fetch
+ Net::HTTP.http_logger.lines
+ Net::HTTP.http_logger.lines.grep(/BODY: \{\"results\":/).should_not be_empty
end
end
-
+
describe "a get request with trace option set to true" do
before(:each) do
- Net::HTTP.http_logger.reset!
Net::HTTP.http_logger_options = {:trace => true}
+ stub_request(:any, "search.twitter.com/search.json?q=httparty").to_return(:body => "\{\"results\"\: 1\}", :status => 200) unless LIVE
end
it "should give the trace output" do
- Twitter::Search.new('httparty').each { |r| r }
- Net::HTTP.http_logger.lines.grep(/TRACE: /).should_not be_empty
+ Twitter::Search.new('httparty').fetch
+ Net::HTTP.http_logger.lines.grep(/TRACE: /).should_not be_empty
end
end
-
+
describe "a post request with default options" do
before(:all) do
- # don't make the request!
- # TODO reset with after(:all)
- module Net
- class HTTP
- def old_request(*args); end
- def started?; false end
- end
- end
Net::HTTP.http_logger_options = {:verbose => false}
+ stub_request(:any, "search.twitter.com/search").to_return(:body => "\{\"results\"\: 1\}", :status => 200) unless LIVE
+ @connection = Net::HTTP.new('search.twitter.com')
+ @connection.post('/search','?q=hello')
+ end
- Net::HTTP.http_logger.reset!
- @connection = Net::HTTP.new('http://search.twitter.com')
- @connection.post('/','?q=hello')
+ it "should give the post url" do
+ Net::HTTP.http_logger.lines.should include("POST /search")
end
-
- it "should give the post uri" do
- Net::HTTP.http_logger.lines.should include("POST /")
+
+
+ if LIVE # only works if real live request
+ it "should give the post params" do
+ Net::HTTP.http_logger.lines.should include("PARAMS {\"?q\"=>[\"hello\"]} ")
+ end
end
-
- it "should give the post params" do
- Net::HTTP.http_logger.lines.should include("PARAMS {\"?q\"=>[\"hello\"]} ")
+
+ it "should give the BODY response code" do
+ Net::HTTP.http_logger.lines.should include("BODY: Net::HTTPOK")
end
-
- #it "should give the BODY response code" do
- #Net::HTTP.http_logger.lines.should include("BODY: Net::HTTPFound")
- #end
+ end
+
+ after(:all) do
+ Net::HTTP.http_logger.reset!
end
end

0 comments on commit 0a2d64a

Please sign in to comment.
Something went wrong with that request. Please try again.