1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
require 'pp'
require 'tmpdir'
require 'stringio'
[STDOUT, STDERR].each { |io| io.sync = true }
require 'maxitest/global_must'
require 'maxitest/autorun'
require 'mocha/minitest'
# Set the MEMCACHED environment variable as follows to enable testing
# of the MemCached meta and entity stores.
ENV['MEMCACHED'] ||= 'localhost:11211'
$memcached = nil
$dalli = nil
def have_memcached?(server=ENV['MEMCACHED'])
return $memcached unless $memcached.nil?
# silence warnings from memcached
begin
v, $VERBOSE = $VERBOSE, nil
require 'memcached'
ensure
$VERBOSE = v
end
$memcached = Memcached.new(server)
$memcached.set('ping', '')
true
rescue LoadError => boom
warn "memcached library not available. related tests will be skipped."
$memcached = false
false
rescue => boom
warn "memcached not working. related tests will be skipped."
$memcached = false
false
end
have_memcached?
def have_dalli?(server=ENV['MEMCACHED'])
return $dalli unless $dalli.nil?
require 'dalli'
$dalli = Dalli::Client.new(server)
$dalli.set('ping', '')
true
rescue LoadError => boom
warn "dalli library not available. related tests will be skipped."
$dalli = false
false
rescue => boom
warn "dalli not working. related tests will be skipped."
$dalli = false
false
end
have_dalli?
def need_dalli(forwhat)
yield if have_dalli?
end
def need_memcached(forwhat)
yield if have_memcached?
end
def need_java(forwhat)
yield if RUBY_PLATFORM =~ /java/
end
require 'rack/cache'
# Methods for constructing downstream applications / response
# generators.
module CacheContextHelpers
class FakeApp
def initialize(status=200, headers={}, body=['Hello World'], bk)
@status = status
@headers = headers
@body = body
@bk = bk
end
def call(env)
@called = true
response = Rack::Response.new(@body, @status, @headers)
request = Rack::Request.new(env)
@bk.call(request, response) if @bk
response.finish
end
def called?
@called
end
def reset!
@called = false
end
end
# The Rack::Cache::Context instance used for the most recent
# request.
attr_reader :cache
# An Array of Rack::Cache::Context instances used for each request, in
# request order.
attr_reader :caches
# The Rack::Response instance result of the most recent request.
attr_reader :response
# An Array of Rack::Response instances for each request, in request order.
attr_reader :responses
# The backend application object.
attr_reader :app
def setup_cache_context
# holds each Rack::Cache::Context
@app = nil
# each time a request is made, a clone of @cache_template is used
# and appended to @caches.
@cache_template = nil
@cache = nil
@caches = []
@errors = StringIO.new
@cache_config = nil
@called = false
@request = nil
@response = nil
@responses = []
@storage = Rack::Cache::Storage.new
end
def teardown_cache_context
@app, @cache_template, @cache, @caches, @called,
@request, @response, @responses, @cache_config, @cache_prototype = nil
end
# A basic response with 200 status code and a tiny body.
def respond_with(status=200, headers={}, body=['Hello World'], &bk)
@app = FakeApp.new(status, headers, body, bk)
end
def cache_config(&block)
@cache_config = block
end
def request(method, uri='/', opts={})
opts = {
'rack.run_once' => true,
'rack.multithread' => false,
'rack.errors' => @errors,
'rack-cache.storage' => @storage
}.merge(opts)
fail 'response not specified (use respond_with)' if @app.nil?
@app.reset! if @app.respond_to?(:reset!)
@cache_prototype ||= Rack::Cache::Context.new(@app, &@cache_config)
@cache = @cache_prototype.clone
@caches << @cache
@request = Rack::MockRequest.new(@cache)
yield @cache if block_given?
@response = @request.request(method.to_s.upcase, uri, opts)
@responses << @response
@response
end
def get(stem, env={}, &b)
request(:get, stem, env, &b)
end
def head(stem, env={}, &b)
request(:head, stem, env, &b)
end
def post(*args, &b)
request(:post, *args, &b)
end
end
module TestHelpers
include FileUtils
F = File
@@temp_dir_count = 0
def create_temp_directory
@@temp_dir_count += 1
path = F.join(Dir.tmpdir, "rack-cache-#{$$}-#{@@temp_dir_count}")
mkdir_p path
if block_given?
yield path
remove_entry_secure path
end
path
end
def create_temp_file(root, file, data='')
path = F.join(root, file)
mkdir_p F.dirname(path)
F.open(path, 'w') { |io| io.write(data) }
end
end
Minitest::Test.class_eval do
include TestHelpers
include CacheContextHelpers
end
|