From 8ca8bd1de4837f7845d47d5ca673212bf55ac240 Mon Sep 17 00:00:00 2001 From: Eric Chiang Date: Thu, 13 Feb 2014 10:29:25 -0500 Subject: [PATCH] maintenance file in json --- Gemfile.lock | 4 ++-- lib/rack/maintenance.rb | 6 +++++- spec/rack-maintenance_spec.rb | 31 +++++++++++++++++++++++++------ 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index d6a6d38..7a1ee82 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) diff --git a/lib/rack/maintenance.rb b/lib/rack/maintenance.rb index 8c700b1..d3822df 100644 --- a/lib/rack/maintenance.rb +++ b/lib/rack/maintenance.rb @@ -14,7 +14,7 @@ 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 @@ -22,6 +22,10 @@ def call(env) private ###################################################################### + def content_type + file.end_with?('json') ? 'application/json' : 'text/html' + end + def environment options[:env] end diff --git a/spec/rack-maintenance_spec.rb b/spec/rack-maintenance_spec.rb index 5f637de..03e1b26 100644 --- a/spec/rack-maintenance_spec.rb +++ b/spec/rack-maintenance_spec.rb @@ -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 @@ -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 @@ -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 @@ -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({}) @@ -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 +