stop_it 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+ gem 'rake'
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Andrew Gridnev
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1 @@
1
+ # StopIt
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new('spec')
7
+
8
+ task :default => :spec
@@ -0,0 +1,36 @@
1
+ class StopIt
2
+
3
+ class << self
4
+ def stop &block
5
+ if block_given?
6
+ @stop = block
7
+ else
8
+ @stop
9
+ end
10
+ end
11
+ end
12
+
13
+ def initialize(app)
14
+ @app = app
15
+ end
16
+
17
+ def call(env)
18
+ if stop?(env)
19
+ return [200, { 'Content-Type' => 'text/html', 'Content-Length' => '0' }, []]
20
+ else
21
+ @app.call(env)
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def stop?(env)
28
+ StopIt.stop && StopIt.stop.call(
29
+ env["PATH_INFO"],
30
+ env["REMOTE_ADDR"],
31
+ env["QUERY_STRING"],
32
+ env["REQUEST_METHOD"],
33
+ env["HTTP_USER_AGENT"]
34
+ )
35
+ end
36
+ end
@@ -0,0 +1,5 @@
1
+ require 'stop_it'
2
+
3
+ RSpec.configure do |config|
4
+ config.mock_with :rspec
5
+ end
@@ -0,0 +1,86 @@
1
+ require 'spec_helper'
2
+
3
+ describe StopIt do
4
+ let!(:app) { app = double("App", call: nil) }
5
+
6
+ subject(:middleware) { StopIt.new(app) }
7
+
8
+ shared_examples_for "blocker" do |env|
9
+ specify do
10
+ middleware.call(env)
11
+ expect(app).not_to have_received(:call)
12
+ end
13
+ end
14
+
15
+ shared_examples_for "non-blocker" do |env|
16
+ specify do
17
+ middleware.call(env)
18
+ expect(app).to have_received(:call)
19
+ end
20
+ end
21
+
22
+ describe "does not stop request if stop block returns false" do
23
+ before { StopIt.stop { |path_info, remote_addr, query_string, request_method, user_agent| false } }
24
+ it_should_behave_like "non-blocker", {}
25
+ end
26
+
27
+ describe "stops request if stop block returns true" do
28
+ before { StopIt.stop { |path_info, remote_addr, query_string, request_method, user_agent| true } }
29
+ it_should_behave_like "blocker", {}
30
+ end
31
+
32
+ describe "filter requests by PATH_INFO env variable" do
33
+ before do
34
+ StopIt.stop do |path_info, remote_addr, query_string, request_method, user_agent|
35
+ path_info == "/forbidden"
36
+ end
37
+ end
38
+
39
+ it_should_behave_like "blocker", "PATH_INFO" => "/forbidden"
40
+ it_should_behave_like "non-blocker", "PATH_INFO" => "/public"
41
+ end
42
+
43
+ describe "filter requests by REMOTE_ADDR env variable" do
44
+ before do
45
+ StopIt.stop do |path_info, remote_addr, query_string, request_method, user_agent|
46
+ remote_addr == "192.168.0.1"
47
+ end
48
+ end
49
+
50
+ it_should_behave_like "blocker", "REMOTE_ADDR" => "192.168.0.1"
51
+ it_should_behave_like "non-blocker", "REMOTE_ADDR" => "127.0.0.1"
52
+ end
53
+
54
+ describe "filter requests by QUERY_STRING env variable" do
55
+ before do
56
+ StopIt.stop do |path_info, remote_addr, query_string, request_method, user_agent|
57
+ query_string == "?block"
58
+ end
59
+ end
60
+
61
+ it_should_behave_like "blocker", "QUERY_STRING" => "?block"
62
+ it_should_behave_like "non-blocker", "QUERY_STRING" => ""
63
+ end
64
+
65
+ describe "filter requests by REQUEST_METHOD env variable" do
66
+ before do
67
+ StopIt.stop do |path_info, remote_addr, query_string, request_method, user_agent|
68
+ request_method == "POST"
69
+ end
70
+ end
71
+
72
+ it_should_behave_like "blocker", "REQUEST_METHOD" => "POST"
73
+ it_should_behave_like "non-blocker", "REQUEST_METHOD" => "GET"
74
+ end
75
+
76
+ describe "filter requests by HTTP_USER_AGENT env variable" do
77
+ before do
78
+ StopIt.stop do |path_info, remote_addr, query_string, request_method, user_agent|
79
+ user_agent == "evil robot"
80
+ end
81
+ end
82
+
83
+ it_should_behave_like "blocker", "HTTP_USER_AGENT" => "evil robot"
84
+ it_should_behave_like "non-blocker", "HTTP_USER_AGENT" => "IE"
85
+ end
86
+ end
@@ -0,0 +1,17 @@
1
+ Gem::Specification.new do |gem|
2
+ gem.authors = ["Andrew Gridnev"]
3
+ gem.email = ["andrew.gridnev@gmail.com"]
4
+ gem.description = %q{}
5
+ gem.summary = %q{Middleware for blocking requests to rake apps.}
6
+ gem.homepage = "https://github.com/andrewgr/stop_it/"
7
+ gem.license = 'MIT'
8
+
9
+ gem.files = `git ls-files`.split($\)
10
+ gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
11
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
12
+ gem.name = "stop_it"
13
+ gem.require_paths = ["lib"]
14
+ gem.version = "0.1.0"
15
+
16
+ gem.add_development_dependency 'rspec'
17
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stop_it
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andrew Gridnev
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70181456105600 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70181456105600
25
+ description: ''
26
+ email:
27
+ - andrew.gridnev@gmail.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - lib/stop_it.rb
38
+ - spec/spec_helper.rb
39
+ - spec/stop_it_spec.rb
40
+ - stop_it.gemspec
41
+ homepage: https://github.com/andrewgr/stop_it/
42
+ licenses:
43
+ - MIT
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project:
62
+ rubygems_version: 1.8.15
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Middleware for blocking requests to rake apps.
66
+ test_files:
67
+ - spec/spec_helper.rb
68
+ - spec/stop_it_spec.rb