Skip to content

Commit

Permalink
Add DoubleWeb.init!, update README
Browse files Browse the repository at this point in the history
  • Loading branch information
reinh committed Jun 17, 2010
1 parent 634cd85 commit 012bb60
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ pkg

## PROJECT::SPECIFIC
tags
.yardoc
doc
24 changes: 20 additions & 4 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,36 @@ testing the network to testing canned responses.
DoubleWeb.cache_strategy = :yaml
DoubleWeb.cache.path = File.join(SPECDIR, 'fixtures', 'network.yaml')

# To record network requests

# Record network requests
before(:all) { DoubleWeb.watch! }
after(:all) { DoubleWeb.unwatch! }

# To playback network requests

# Playback network requests
before(:all) { DoubleWeb.playback! }
after(:all) { DoubleWeb.stop_playback! }

In playback mode, DoubleWeb will return the recorded responses for each request
and will raise a `DoubleWeb::UnexpectedRequestError` for any requests that do
not have recorded responses.

== DoubleWeb.init!

DoubleWeb.init! lets you control DoubleWeb from your environment. For instance,
with the following in your spec/spec_helper.rb

SPECDIR = File.dirname(__FILE__)

require 'doubleweb'
DoubleWeb.cache_strategy = :yaml
DoubleWeb.cache.path = File.join(SPECDIR, 'fixtures', 'doubleweb.yaml')
DoubleWeb.init!

DoubleWeb will be controlled by an "internet" environment variable. For instance:

[<tt>$ rake spec internet=on</tt>] Disable DoubleWeb
[<tt>$ rake spec internet=watch</tt>] DoubleWeb in watch mode
[<tt>$ rake spec internet=off</tt>] DoubleWeb in playback mode (internet disabled)

== Note on Patches/Pull Requests

* Fork the project.
Expand Down
20 changes: 20 additions & 0 deletions lib/double_web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ module Caches

class UnexpectedRequestError < StandardError; end

def self.init!
case ENV['internet']
when 'on' then puts "DoubleWeb: inactive"; clear!
when 'off' then puts "DoubleWeb: playback mode"; playback!
when 'watch' then puts "DoubleWeb: watching"; watch!
else
puts "DoubleWeb: set the `internet` enviroment variable to control DoubleWeb"
puts " options are 'on', 'off', 'watch'"
puts " DoubleWeb is currently disabled."
end
end

def self.patch!
unless @patched
require 'net/http' unless defined?(Net::HTTP)
Expand Down Expand Up @@ -81,4 +93,12 @@ def self.[]=(request, response)
cache[request] = response
end

class << self; attr_accessor :out end

private

def self.puts(*args)
(out || STDOUT).puts(*args)
end

end
2 changes: 1 addition & 1 deletion lib/double_web/caches/yaml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def dump(object)

def load
clear unless exists?
open('r') {|fh| YAML.load(fh) } || {}
YAML.load_file(fh) || {}
end

def path?; path end
Expand Down
73 changes: 73 additions & 0 deletions spec/double_web_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,79 @@
DoubleWeb.clear!
end

describe ".init!" do
before do
DoubleWeb.out = StringIO.new
end

after { DoubleWeb.out = nil }
subject { DoubleWeb }

describe "with internet=on" do
before do
ENV['internet'] = 'on'
DoubleWeb.init!
end

it { should_not be_watch }
it { should_not be_playback }

it "should notify that doubleweb is inactive" do
DoubleWeb.out.rewind
DoubleWeb.out.read.should == "DoubleWeb: inactive\n"
end
end

describe "with internet=off" do
before do
ENV['internet'] = 'off'
DoubleWeb.init!
end

it { should_not be_watch }
it { should be_playback }

it "should notify that doubleweb is in playback mode" do
DoubleWeb.out.rewind
DoubleWeb.out.read.should == "DoubleWeb: playback mode\n"
end
end


describe "with internet=watch" do
before do
ENV['internet'] = 'watch'
DoubleWeb.init!
end

it { should be_watch }
it { should_not be_playback }

it "should notify that doubleweb is in playback mode" do
DoubleWeb.out.rewind
DoubleWeb.out.read.should == "DoubleWeb: watching\n"
end
end

describe "without internet=" do
before do
ENV['internet'] = nil
DoubleWeb.init!
end

it { should_not be_watch }
it { should_not be_playback }

it "should notify that doubleweb is in playback mode" do
DoubleWeb.out.rewind
DoubleWeb.out.read.should == "DoubleWeb: set the `internet` enviroment variable to control DoubleWeb\n" +
" options are 'on', 'off', 'watch'\n" +
" DoubleWeb is currently disabled.\n"
end
end

end

describe ".patch!" do
before { DoubleWeb.patch! }

Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
require 'spec'
require 'spec/autorun'

SPECDIR = File.dirname(__FILE__)

Spec::Runner.configure do |config|

end

0 comments on commit 012bb60

Please sign in to comment.