Skip to content

Instantly share code, notes, and snippets.

@frojasg
Created June 28, 2013 19:43
Show Gist options
  • Save frojasg/5887493 to your computer and use it in GitHub Desktop.
Save frojasg/5887493 to your computer and use it in GitHub Desktop.
Try to use caching when we use WebMock library
diff --git a/Gemfile b/Gemfile
index b235c52..2cfee39 100644
--- a/Gemfile
+++ b/Gemfile
@@ -9,6 +9,7 @@ group :development, :test do
gem "sinatra", "~> 1.3"
gem "json"
gem "faraday", "~> 0.8.4"
+ gem "webmock"
if RUBY_PLATFORM == "java"
gem "spoon"
diff --git a/lib/typhoeus.rb b/lib/typhoeus.rb
index c7b8054..8b71e14 100644
--- a/lib/typhoeus.rb
+++ b/lib/typhoeus.rb
@@ -100,6 +100,17 @@ module Typhoeus
@before
end
+ before { |request|
+ puts 'lalala'.inspect
+ if request.cacheable? && response = Typhoeus::Config.cache.get(self)
+ request.finish(response)
+ false
+ else
+ true
+ end
+ }
+
+
# Execute given block as if block connection is turned off.
# The old block connection state is restored afterwards.
#
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index ca288b3..5c5ec05 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -5,6 +5,7 @@ require "bundler"
Bundler.setup
require "typhoeus"
require "rspec"
+require "webmock"
if defined? require_relative
require_relative 'support/localhost_server.rb'
@@ -17,6 +18,8 @@ end
RSpec.configure do |config|
config.order = :rand
+ WebMock.disable!
+
config.before(:suite) do
LocalhostServer.new(TESTSERVER.new, 3001)
end
diff --git a/spec/typhoeus/request/cacheable_spec.rb b/spec/typhoeus/request/cacheable_spec.rb
index 7ca8b87..3bc40a5 100644
--- a/spec/typhoeus/request/cacheable_spec.rb
+++ b/spec/typhoeus/request/cacheable_spec.rb
@@ -1,4 +1,5 @@
require 'spec_helper'
+require 'webmock'
describe Typhoeus::Request::Cacheable do
let(:cache) {
@@ -65,6 +66,18 @@ describe Typhoeus::Request::Cacheable do
request.run
end
end
+ context "when webmock is activated" do
+ let(:response) { Typhoeus::Response.new }
+ before {
+ WebMock.enable!
+ WebMock::API.stub_request(:get, request.url).to_return(body: 'OK', status: 200)
+ cache.memory[request] = response}
+ after { WebMock.disable! }
+ it "finishes request" do
+ request.should_receive(:finish).with(response)
+ request.run
+ end
+ end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment