Skip to content

Commit

Permalink
maintenance file in json
Browse files Browse the repository at this point in the history
  • Loading branch information
serixscorpio committed Feb 13, 2014
1 parent 2d3635a commit 8ca8bd1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
PATH
remote: .
specs:
rack-maintenance (0.3.0)
rack-maintenance (1.0.0)
rack (>= 1.0)

GEM
remote: http://rubygems.org/
specs:
diff-lcs (1.1.3)
rack (1.4.1)
rack (1.5.2)
rake (0.9.2.2)
rspec (2.11.0)
rspec-core (~> 2.11.0)
Expand Down
6 changes: 5 additions & 1 deletion lib/rack/maintenance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ def initialize(app, options={})
def call(env)
if maintenance? && path_in_app(env)
data = File.read(file)
[ 503, { 'Content-Type' => 'text/html', 'Content-Length' => data.length.to_s }, [data] ]
[ 503, { 'Content-Type' => content_type, 'Content-Length' => data.length.to_s }, [data] ]
else
app.call(env)
end
end

private ######################################################################

def content_type
file.end_with?('json') ? 'application/json' : 'text/html'
end

def environment
options[:env]
end
Expand Down
31 changes: 25 additions & 6 deletions spec/rack-maintenance_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
require 'fileutils'

describe "RackMaintenance" do
shared_examples "RackMaintenance" do
let(:app) { Class.new { def call(env); end }.new }
let(:rack) { Rack::Maintenance.new(app, :file => "spec/maintenance.html") }
let(:rack) { Rack::Maintenance.new(app, :file => file_name) }

context "without a :file option" do
it "raises an error" do
Expand All @@ -22,11 +22,11 @@

context "with maintenance file" do
before do
FileUtils.touch 'spec/maintenance.html'
FileUtils.touch file_name
end

after do
FileUtils.rm 'spec/maintenance.html'
FileUtils.rm file_name
end

it "does not call the app" do
Expand All @@ -35,11 +35,11 @@
end

it "returns the maintenance response" do
rack.call({}).should eq [503, {"Content-Type"=>"text/html", "Content-Length"=>"0"}, [""]]
rack.call({}).should eq [503, {"Content-Type"=>content_type, "Content-Length"=>"0"}, [""]]
end

context "and :env option MAINTENANCE" do
let(:rack) { Rack::Maintenance.new(app, :file => "spec/maintenance.html", :env => "MAINTENANCE") }
let(:rack) { Rack::Maintenance.new(app, :file => file_name, :env => "MAINTENANCE") }

context "outside MAINTENANCE env" do
it "calls the app" do
Expand All @@ -53,6 +53,10 @@
ENV['MAINTENANCE'] = "true"
end

after do
ENV.delete("MAINTENANCE")
end

it "does not call the app" do
app.should_not_receive :call
rack.call({})
Expand All @@ -68,3 +72,18 @@
end
end
end

describe "RackMaintenance with json maintenance file" do
it_behaves_like "RackMaintenance" do
let(:file_name) { "spec/maintenance.json" }
let(:content_type) { "application/json" }
end
end

describe "RackMaintenance with html maintenance file" do
it_behaves_like "RackMaintenance" do
let(:file_name) { "spec/maintenance.html" }
let(:content_type) { "text/html" }
end
end

0 comments on commit 8ca8bd1

Please sign in to comment.