Skip to content

Commit

Permalink
Working YAML strategy, updated tests
Browse files Browse the repository at this point in the history
YAML strategy works
HTTPRequest and HTTPResponse implement ==, #eql?, #hash
Improved testing of puts for command-line use
  • Loading branch information
reinh committed Jun 22, 2010
1 parent 012bb60 commit 12721b9
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 39 deletions.
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ begin
gem.email = "reinh@reinh.com"
gem.homepage = "http://github.com/reinh/doubleweb"
gem.authors = ["Rein Henrichs"]
gem.add_dependency "zaml"
gem.add_development_dependency "rspec", ">= 1.2.9"
gem.add_development_dependency "yard", ">= 0"
gem.add_development_dependency "fakeweb"
Expand Down
16 changes: 5 additions & 11 deletions lib/double_web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ def self.init!
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."
puts "DoubleWeb: set the `internet` enviroment variable to control DoubleWeb",
" options are 'on', 'off', 'watch'",
" DoubleWeb is currently disabled."
end
end

def self.patch!
unless @patched
require 'net/http' unless defined?(Net::HTTP)
Net::HTTP.send(:include, DoubleWeb::DriverPatches::NetHTTP)
Net::HTTPRequest.send(:include, DoubleWeb::DriverPatches::NetHTTP::RequestPatch)
Net::HTTPResponse.send(:include, DoubleWeb::DriverPatches::NetHTTP::ResponsePatch)
end
@patched = true
end
Expand Down Expand Up @@ -93,12 +95,4 @@ 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
16 changes: 9 additions & 7 deletions lib/double_web/caches/yaml.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
module DoubleWeb::Caches::Yaml

include DoubleWeb::Caches::Base

class Store
require 'yaml'
require 'zaml' # YAML.dump is broken
require 'singleton'
include Singleton
attr_accessor :path
Expand All @@ -11,28 +13,28 @@ def clear
dump({}) if path?
end

def []=(key, value)
dump(load.merge(key => value))
def []=(request, response)
dump(load.merge(request => response))
end

def [](key)
load[key]
def [](request)
p self.load
self.load[request]
end

private

def open(mode, &block)
raise "Set DoubleWeb.cache.path to use YAML storage" unless path
File.open(path, mode, &block)
end

def dump(object)
open('w') {|fh| YAML.dump(object, fh) }
open('w') {|fh| fh.write ZAML.dump(object) }
end

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

def path?; path end
Expand Down
42 changes: 41 additions & 1 deletion lib/double_web/driver_patches/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,46 @@ def request_with_doubleweb(request, body = nil, &block)
response
end
end

module Equivocable
def hash
[self.class, 0, *hash_attrs].map(&:hash).reduce(:^)
end

def eql?(other)
hash == other.hash
end

def ==(other)
hash_attrs = other.hash_attrs
end

protected
def hash_attrs
raise NotImplementedError, "define 'hash_attrs' to provide #hash, #eql? and #== semantics"
end

end

module RequestPatch
include Equivocable

protected
def hash_attrs
[path, method, body]
end

end

module ResponsePatch
include Equivocable

protected
def hash_attrs
[code, @version, message]
end

end

end
end

25 changes: 18 additions & 7 deletions spec/double_web/caches/yaml_spec.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')

describe DoubleWeb::Caches::Yaml do
TMPFILE = "#{SPECDIR}/tmp/double-web-yaml-store"
class DoubleWeb::Caches::Yaml::Store; public :load; end

before do
DoubleWeb.cache_strategy = :yaml
DoubleWeb.cache.path = '/tmp/double-web-yaml-store'
DoubleWeb.cache.path = TMPFILE
DoubleWeb.patch!
end

after { DoubleWeb.clear! }
after { File.unlink('/tmp/double-web-yaml-store') if File.exists?('/tmp/double-web-yaml-store') }
after(:all) { File.unlink(TMPFILE) if File.exists?(TMPFILE)}

subject { DoubleWeb.cache }
let(:request) { Net::HTTP::Get.new('/foo') }
let(:response) { Net::HTTPSuccess.new(1, 200, "Success") }

describe "cache" do
it { should be_a_kind_of DoubleWeb::Caches::Yaml::Store }
end

describe "net http patch" do
subject { Net::HTTP::Get.new('/foo') }
it { should == Net::HTTP::Get.new('/foo') }
it { should be_eql Net::HTTP::Get.new('/foo') }
it("should have the same hash") { subject.hash.should == Net::HTTP::Get.new('/foo').hash }
end

describe "#clear" do
it "should store the empty Hash in the YAML cache" do
subject.clear
Expand All @@ -26,23 +37,23 @@ class DoubleWeb::Caches::Yaml::Store; public :load; end

describe "[]=" do
it "stores the key and value to the YAML file" do
subject["request"] = "response"
DoubleWeb::Caches::Yaml.cache.load["request"].should == "response"
subject[request] = response
subject[request].should == response
end
end

describe "[]" do
it "retrieves the value for the key from the YAML file" do
subject["request"] = "response"
subject["request"].should == "response"
subject[request] = response
subject[request].should == response
end
end

describe "when the path is not set" do
before { DoubleWeb.cache.path = nil }

it "raises an error that instructs to set the path" do
lambda { subject["request"] }.should raise_error
lambda { subject[request] }.should raise_error
end

end
Expand Down
23 changes: 10 additions & 13 deletions spec/double_web_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@
end

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

after { DoubleWeb.out = nil }
before { @old, $stdout = $stdout, StringIO.new }
after { $stdout = @old }
subject { DoubleWeb }

describe "with internet=on" do
Expand All @@ -32,8 +29,8 @@
it { should_not be_playback }

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

Expand All @@ -47,8 +44,8 @@
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"
$stdout.rewind
$stdout.read.should == "DoubleWeb: playback mode\n"
end
end

Expand All @@ -63,8 +60,8 @@
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"
$stdout.rewind
$stdout.read.should == "DoubleWeb: watching\n"
end
end

Expand All @@ -78,8 +75,8 @@
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" +
$stdout.rewind
$stdout.read.should == "DoubleWeb: set the `internet` enviroment variable to control DoubleWeb\n" +
" options are 'on', 'off', 'watch'\n" +
" DoubleWeb is currently disabled.\n"
end
Expand Down
30 changes: 30 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
require 'rubygems'
require 'spork'

Spork.prefork do
# Loading more in this block will cause your tests to run faster. However,
# if you change any configuration or code from libraries loaded here, you'll
# need to restart spork for it take effect.

end

Spork.each_run do
# This code will be run each time you run your specs.

end

# --- Instructions ---
# - Sort through your spec_helper file. Place as much environment loading
# code that you don't normally modify during development in the
# Spork.prefork block.
# - Place the rest under Spork.each_run block
# - Any code that is left outside of the blocks will be ran during preforking
# and during each_run!
# - These instructions should self-destruct in 10 seconds. If they don't,
# feel free to delete them.
#




$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'doubleweb'
require 'spec'
require 'spec/autorun'
require 'net/http'

SPECDIR = File.dirname(__FILE__)

Expand Down

0 comments on commit 12721b9

Please sign in to comment.