Skip to content

Commit

Permalink
Merge pull request #15 from hojberg/cache_control
Browse files Browse the repository at this point in the history
added cache control
  • Loading branch information
hojberg committed Mar 22, 2012
2 parents c1b334d + 45825e9 commit d494969
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
9 changes: 6 additions & 3 deletions lib/rack/combobot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ class Combobot
def initialize(app, options = {})
@app = app

root = Pathname.new(options[:root] || Dir.pwd)
expires = options[:expires]
@config = Rack::Combobot::Config.new(root, expires)
options[:root] = Pathname.new(options[:root] || Dir.pwd)
@config = Rack::Combobot::Config.new(options)
end

# rack request handler
Expand Down Expand Up @@ -55,6 +54,10 @@ def create_headers(extension)
if @config.expires
headers['Expires'] = @config.expires.httpdate
end

if @config.cache_control
headers['Cache-Control'] = @config.cache_control
end

headers
end
Expand Down
9 changes: 5 additions & 4 deletions lib/rack/combobot/config.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module Rack
class Combobot
class Config
attr_accessor :root, :expires
attr_accessor :root, :expires, :cache_control

def initialize(root, expires = nil)
@root = root
@expires = expires
def initialize(options)
@root = options[:root]
@expires = options[:expires]
@cache_control = options[:cache_control]
end

end
Expand Down
2 changes: 1 addition & 1 deletion rack-combobot.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ $:.push File.expand_path("../lib", __FILE__)

Gem::Specification.new do |s|
s.name = "rack-combobot"
s.version = "0.2.0"
s.version = "0.2.1"
s.authors = ["Simon Højberg", "Christopher Meiklejohn"]
s.email = ["r.hackr@gmail.com", "christopher.meiklejohn@gmail.com"]
s.homepage = "https://github.com/hojberg/rack-combobot"
Expand Down
13 changes: 13 additions & 0 deletions spec/lib/rack/combobot_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
# response helpers
let(:combobot_js_response) { [200, {"Content-Type" => "text/javascript"}, ["function lorem() { return \"a\"; }\nfunction ipsum() { return \"b\"; }\n"]] }
let(:expires_js_response) { [200, {"Content-Type" => "text/javascript", 'Expires' => 'Wed, 01 Jan 2020 00:00:00 GMT'}, ["function lorem() { return \"a\"; }\nfunction ipsum() { return \"b\"; }\n"]] }
let(:cache_control_js_response) { [200, {"Content-Type" => "text/javascript", 'Cache-Control' => 'public'}, ["function lorem() { return \"a\"; }\nfunction ipsum() { return \"b\"; }\n"]] }
let(:expires_and_cache_control_js_response) { [200, {"Content-Type" => "text/javascript", 'Expires' => 'Wed, 01 Jan 2020 00:00:00 GMT', 'Cache-Control' => 'public'}, ["function lorem() { return \"a\"; }\nfunction ipsum() { return \"b\"; }\n"]] }
let(:combobot_css_response) { [200, {"Content-Type" => "text/css"}, [".lorem { background: blue; }\n#lipsum { border: 1px solid red }\n"]] }
let(:combobot_404_response) { [404, {'Content-Type' => 'text/html'}, ['File not found']] }

Expand Down Expand Up @@ -57,6 +59,17 @@
@app.call(combobot_js_request).must_equal(expires_js_response)
end

it 'adds cache control headers' do
@app.config.cache_control = 'public'
@app.call(combobot_js_request).must_equal(cache_control_js_response)
end

it 'adds both expires and cache control headers' do
@app.config.expires = Time.gm(2020)
@app.config.cache_control = 'public'
@app.call(combobot_js_request).must_equal(expires_and_cache_control_js_response)
end

it 'allows for versioning' do
@app.call(
combobot_js_request.merge({ 'PATH_INFO' => '/combobot/CACHEBUSTINGSHA1'})
Expand Down

0 comments on commit d494969

Please sign in to comment.