rtomayko / rack-cache

Real HTTP Caching for Ruby Web Apps

This URL has Read+Write access

rack-cache / test / spec_setup.rb
100644 238 lines (195 sloc) 5.381 kb
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
require 'pp'
require 'tmpdir'
require 'stringio'
 
[ STDOUT, STDERR ].each { |io| io.sync = true }
 
begin
  require 'test/spec'
rescue LoadError => boom
  require 'rubygems' rescue nil
  require 'test/spec'
end
 
# Set the MEMCACHED environment variable as follows to enable testing
# of the MemCached meta and entity stores.
ENV['MEMCACHED'] ||= 'localhost:11211'
$memcached = nil
$memcache = nil
 
def have_memcached?(server=ENV['MEMCACHED'])
  return $memcached unless $memcached.nil?
  v, $VERBOSE = $VERBOSE, nil # silence warnings from memcached
  require 'memcached'
  $VERBOSE = v
  $memcached = Memcached.new(server)
  $memcached.set('ping', '')
  true
rescue LoadError => boom
  $memcached = false
  false
rescue => boom
  STDERR.puts "memcached not working. related tests will be skipped."
  $memcached = false
  false
end
 
have_memcached?
 
def have_memcache?(server=ENV['MEMCACHED'])
  return $memcache unless $memcache.nil?
  require 'memcache'
  $memcache = MemCache.new(server)
  $memcache.set('ping', '')
  true
rescue LoadError => boom
  $memcache = false
  false
rescue => boom
  STDERR.puts "memcache not working. related tests will be skipped."
  $memcache = false
  false
end
 
have_memcache?
 
def need_memcached(forwhat)
  if have_memcached?
    yield
  else
    STDERR.puts "skipping memcached #{forwhat}"
  end
end
 
def need_memcache(forwhat)
  if have_memcache?
    yield
  else
    STDERR.puts "skipping memcache #{forwhat}"
  end
end
 
def need_java(forwhat)
  if RUBY_PLATFORM =~ /java/
    yield
  else
    STDERR.puts "skipping app engine #{forwhat}"
  end
end
 
 
# Setup the load path ..
$LOAD_PATH.unshift File.dirname(File.dirname(__FILE__)) + '/lib'
$LOAD_PATH.unshift File.dirname(__FILE__)
 
require 'rack/cache'
 
 
# Methods for constructing downstream applications / response
# generators.
module CacheContextHelpers
 
  # 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 = nil
  end
 
  # A basic response with 200 status code and a tiny body.
  def respond_with(status=200, headers={}, body=['Hello World'])
    called = false
    @app =
      lambda do |env|
        called = true
        response = Rack::Response.new(body, status, headers)
        request = Rack::Request.new(env)
        yield request, response if block_given?
        response.finish
      end
    @app.meta_def(:called?) { called }
    @app.meta_def(:reset!) { called = false }
    @app
  end
 
  def cache_config(&block)
    @cache_config = block
  end
 
  def request(method, uri='/', opts={})
    opts = {
      'rack.run_once' => true,
      '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
 
class Test::Unit::TestCase
  include TestHelpers
  include CacheContextHelpers
end
 
# Metaid == a few simple metaclass helper
# (See http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html.)
class Object
  # The hidden singleton lurks behind everyone
  def metaclass; class << self; self; end; end
  def meta_eval(&blk); metaclass.instance_eval(&blk); end
  # Adds methods to a metaclass
  def meta_def name, &blk
    meta_eval { define_method name, &blk }
  end
  # Defines an instance method within a class
  def class_def name, &blk
    class_eval { define_method name, &blk }
  end
 
  # True when the Object is neither false or nil.
  def truthy?
    !!self
  end
end