From 4d402003540ff2f2c31a0840de9429c411db225d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Arild=20M=C3=A6land?= Date: Thu, 24 Jan 2013 19:04:17 +0100 Subject: [PATCH 1/3] Add activesupport as a dependency and require AS/dependencies The gem won't load without it. --- lib/secure_headers/railtie.rb | 2 ++ secure-headers.gemspec | 1 + 2 files changed, 3 insertions(+) diff --git a/lib/secure_headers/railtie.rb b/lib/secure_headers/railtie.rb index fc6463d..702c0a1 100644 --- a/lib/secure_headers/railtie.rb +++ b/lib/secure_headers/railtie.rb @@ -1,3 +1,5 @@ +require 'active_support/dependencies' + # rails 3.1+ if defined?(Rails::Railtie) module SecureHeaders diff --git a/secure-headers.gemspec b/secure-headers.gemspec index 0628d71..db11f10 100644 --- a/secure-headers.gemspec +++ b/secure-headers.gemspec @@ -19,5 +19,6 @@ Gem::Specification.new do |gem| gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) gem.require_paths = ["lib"] gem.add_dependency "brwsr", ">= 1.1.1" + gem.add_dependency "activesupport", ">= 2.0" gem.add_development_dependency "rake" end From 3f04883d51450b45c0dd09828915c474e2538dc6 Mon Sep 17 00:00:00 2001 From: Neil Matatall Date: Thu, 24 Jan 2013 23:56:39 -0800 Subject: [PATCH 2/3] remove jank, support sinatra better now that we depend on active_support, we can remove some jank. While we're at it, remove rails-specific code --- .../content_security_policy_controller.rb | 2 +- ...content_security_policy_controller_spec.rb | 3 +- spec/spec_helper.rb | 37 +------------------ 3 files changed, 4 insertions(+), 38 deletions(-) diff --git a/app/controllers/content_security_policy_controller.rb b/app/controllers/content_security_policy_controller.rb index 5f2b145..9acb264 100644 --- a/app/controllers/content_security_policy_controller.rb +++ b/app/controllers/content_security_policy_controller.rb @@ -31,7 +31,7 @@ class ContentSecurityPolicyController < ActionController::Base head :ok rescue StandardError => e - Rails.logger.warn "Unable to POST CSP report to #{report_uri} because #{e}" + Rails.logger.warn("Unable to POST CSP report to #{report_uri} because #{e}") if defined?(Rails.logger) head :bad_request end diff --git a/spec/controllers/content_security_policy_controller_spec.rb b/spec/controllers/content_security_policy_controller_spec.rb index fa84786..e7a8017 100644 --- a/spec/controllers/content_security_policy_controller_spec.rb +++ b/spec/controllers/content_security_policy_controller_spec.rb @@ -39,6 +39,7 @@ describe ContentSecurityPolicyController do it "makes a POST request" do Net::HTTP.stub(:new).and_return(request) request.should_receive(:request).with(instance_of(::Net::HTTP::Post)) + params.stub(:to_json) subject.scribe end @@ -60,7 +61,7 @@ describe ContentSecurityPolicyController do end it "logs errors when it cannot forward the CSP report" do - class Rails; end + class Rails; def logger; end; end logger = double(:repond_to? => true) Rails.stub(:logger).and_return(logger) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 84d51ac..e44ecf5 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,5 +1,6 @@ require 'rubygems' require 'spork' +require 'active_support' unless Spork.using_spork? require 'simplecov' @@ -11,42 +12,6 @@ end Spork.prefork do require 'pry' require 'rspec' - - # Need these features from Rails, stubbing various features from various versions - class ApplicationController; end - - module ActionController - module Routing - class Routes - def self.draw; end - end - - module RouteSet; class Mapper; end; end - end - end - - module ActiveSupport - class JSON - def self.encode obj, options = nil; end - end - - module Dependencies - class << self - attr_accessor :autoload_paths - autoload_paths = [] - end - end - end - - class Object - def try(*a, &b); end - - def to_json(options = nil); end - end - - class NilClass - def try(*args); end - end end Spork.each_run do From f43a46d92d74b3de374a20268130ac5dc5c76c2d Mon Sep 17 00:00:00 2001 From: Neil Matatall Date: Fri, 25 Jan 2013 00:12:31 -0800 Subject: [PATCH 3/3] Instead of requiring dependency, only exe if defined --- lib/secure_headers/railtie.rb | 12 ++++++------ secure-headers.gemspec | 1 - spec/spec_helper.rb | 1 - 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/secure_headers/railtie.rb b/lib/secure_headers/railtie.rb index 702c0a1..7e31f57 100644 --- a/lib/secure_headers/railtie.rb +++ b/lib/secure_headers/railtie.rb @@ -1,5 +1,3 @@ -require 'active_support/dependencies' - # rails 3.1+ if defined?(Rails::Railtie) module SecureHeaders @@ -25,10 +23,12 @@ else end end - if ActiveSupport::Dependencies.autoload_paths - ActiveSupport::Dependencies.autoload_paths << File.expand_path(File.join("..", "..", "..", "app", "controllers"), __FILE__) - else - ActiveSupport::Dependencies.autoload_paths = [File.expand_path(File.join("..", "..", "..", "app", "controllers"), __FILE__)] + if defined?(ActiveSupport::Dependencies) + if ActiveSupport::Dependencies.autoload_paths + ActiveSupport::Dependencies.autoload_paths << File.expand_path(File.join("..", "..", "..", "app", "controllers"), __FILE__) + else + ActiveSupport::Dependencies.autoload_paths = [File.expand_path(File.join("..", "..", "..", "app", "controllers"), __FILE__)] + end end if defined? ActionController::Routing diff --git a/secure-headers.gemspec b/secure-headers.gemspec index db11f10..0628d71 100644 --- a/secure-headers.gemspec +++ b/secure-headers.gemspec @@ -19,6 +19,5 @@ Gem::Specification.new do |gem| gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) gem.require_paths = ["lib"] gem.add_dependency "brwsr", ">= 1.1.1" - gem.add_dependency "activesupport", ">= 2.0" gem.add_development_dependency "rake" end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e44ecf5..f8b7d10 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,5 @@ require 'rubygems' require 'spork' -require 'active_support' unless Spork.using_spork? require 'simplecov'