traffic_cop 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/.rvmrc +1 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +28 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/lib/traffic_cop.rb +4 -0
- data/lib/traffic_cop/http_redirect_test.rb +56 -0
- data/lib/traffic_cop/redirect_check.rb +46 -0
- data/lib/traffic_cop/resource_path.rb +19 -0
- data/lib/traffic_cop/version.rb +13 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/traffic_cop/redirect_check_spec.rb +18 -0
- metadata +116 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use ruby-1.9.2@traffic_cop --create
|
data/Gemfile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
|
+
# Runtime dependencies
|
4
|
+
gem 'test-unit', '2.3.0'
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "rspec", "~> 2.6.0"
|
10
|
+
gem "bundler", "~> 1.0.0"
|
11
|
+
gem "jeweler", "~> 1.6.0"
|
12
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.2)
|
5
|
+
git (1.2.5)
|
6
|
+
jeweler (1.6.0)
|
7
|
+
bundler (~> 1.0.0)
|
8
|
+
git (>= 1.2.5)
|
9
|
+
rake
|
10
|
+
rake (0.8.7)
|
11
|
+
rspec (2.6.0)
|
12
|
+
rspec-core (~> 2.6.0)
|
13
|
+
rspec-expectations (~> 2.6.0)
|
14
|
+
rspec-mocks (~> 2.6.0)
|
15
|
+
rspec-core (2.6.0)
|
16
|
+
rspec-expectations (2.6.0)
|
17
|
+
diff-lcs (~> 1.1.2)
|
18
|
+
rspec-mocks (2.6.0)
|
19
|
+
test-unit (2.3.0)
|
20
|
+
|
21
|
+
PLATFORMS
|
22
|
+
ruby
|
23
|
+
|
24
|
+
DEPENDENCIES
|
25
|
+
bundler (~> 1.0.0)
|
26
|
+
jeweler (~> 1.6.0)
|
27
|
+
rspec (~> 2.6.0)
|
28
|
+
test-unit (= 2.3.0)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Shay Frendt
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= traffic_cop
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Contributing to traffic_cop
|
6
|
+
|
7
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
8
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
9
|
+
* Fork the project
|
10
|
+
* Start a feature/bugfix branch
|
11
|
+
* Commit and push until you are happy with your contribution
|
12
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
13
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2011 Shay Frendt. See LICENSE.txt for
|
18
|
+
further details.
|
19
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "traffic_cop"
|
18
|
+
gem.homepage = "http://github.com/shayfrendt/traffic_cop"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{TrafficCop is a collection of test macros to test HTTP redirect rules for remote servers.}
|
21
|
+
gem.description = %Q{TrafficCop is a collection of test macros to test HTTP redirect rules for remote servers.}
|
22
|
+
gem.email = "shay.frendt@gmail.com"
|
23
|
+
gem.authors = ["Shay Frendt"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
+
end
|
33
|
+
|
34
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
35
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
36
|
+
spec.rcov = true
|
37
|
+
end
|
38
|
+
|
39
|
+
task :default => :spec
|
40
|
+
|
41
|
+
require 'rake/rdoctask'
|
42
|
+
Rake::RDocTask.new do |rdoc|
|
43
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
44
|
+
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = "traffic_cop #{version}"
|
47
|
+
rdoc.rdoc_files.include('README*')
|
48
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/traffic_cop.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
class HTTPRedirectTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def default_test; end # placeholder to stop Test::Unit from complaining
|
6
|
+
|
7
|
+
def self.domain=(domain)
|
8
|
+
RedirectCheck.domain = domain
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.should_not_redirect(path)
|
12
|
+
class_eval <<-CODE
|
13
|
+
def test_#{name_for(path)}_should_not_redirect
|
14
|
+
check = RedirectCheck.new('#{path}')
|
15
|
+
|
16
|
+
assert_equal false, check.redirected?, "#{path} is redirecting"
|
17
|
+
assert_equal true, check.success?, "#{path} is not a success response"
|
18
|
+
end
|
19
|
+
CODE
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.name_for(path)
|
23
|
+
name = path.gsub(/\W+/, '_')
|
24
|
+
name.gsub!(/^_+/, '')
|
25
|
+
name.gsub!(/_+$/, '')
|
26
|
+
|
27
|
+
name = 'root' if name == ''
|
28
|
+
|
29
|
+
name
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.should_redirect(source, options)
|
33
|
+
source_path = ResourcePath.new(source, :param => 'subdir').to_s
|
34
|
+
destination_path = ResourcePath.new(options[:to], :param => 'subdir').to_s
|
35
|
+
|
36
|
+
permanent = options.fetch(:permanent, true)
|
37
|
+
|
38
|
+
class_eval <<-CODE
|
39
|
+
def test_#{name_for(source_path)}_should_redirect_to_#{name_for(destination_path)}
|
40
|
+
redirection = RedirectCheck.new('#{source_path}', '#{destination_path}')
|
41
|
+
|
42
|
+
assert_equal true, redirection.redirected?, "'#{source_path}' is not redirecting"
|
43
|
+
assert_equal '#{destination_path}', redirection.redirected_path,
|
44
|
+
"'#{source_path}' is not redirecting to '#{destination_path}'"
|
45
|
+
|
46
|
+
if #{permanent}
|
47
|
+
assert_equal true, redirection.permanent_redirect?,
|
48
|
+
"The redirection from '#{source_path}' to '#{destination_path}' doesn't appear to be a permanent redirect"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
CODE
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'net/http'
|
3
|
+
|
4
|
+
class RedirectCheck
|
5
|
+
|
6
|
+
def self.domain=(domain)
|
7
|
+
@domain = domain
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.domain
|
11
|
+
@domain
|
12
|
+
end
|
13
|
+
|
14
|
+
attr_reader :source_path, :destination_path
|
15
|
+
|
16
|
+
def initialize(source_path, destination_path = nil)
|
17
|
+
@source_path = source_path.to_s
|
18
|
+
@destination_path = destination_path.to_s
|
19
|
+
end
|
20
|
+
|
21
|
+
def uri
|
22
|
+
URI.parse("http://#{self.class.domain}#{source_path}")
|
23
|
+
end
|
24
|
+
|
25
|
+
def response
|
26
|
+
@response ||= Net::HTTP.start(uri.host, uri.port) {|http| http.head(uri.path) }
|
27
|
+
end
|
28
|
+
|
29
|
+
def success?
|
30
|
+
response.is_a?(Net::HTTPOK)
|
31
|
+
end
|
32
|
+
|
33
|
+
def redirected?
|
34
|
+
response.is_a?(Net::HTTPRedirection)
|
35
|
+
end
|
36
|
+
|
37
|
+
def permanent_redirect?
|
38
|
+
redirected? && response.is_a?(Net::HTTPMovedPermanently)
|
39
|
+
end
|
40
|
+
|
41
|
+
def redirected_path
|
42
|
+
response['location'].sub(/#{Regexp.escape("#{uri.scheme}://#{uri.host}")}/, '') if redirected?
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class ResourcePath
|
2
|
+
|
3
|
+
attr_writer :param
|
4
|
+
|
5
|
+
def initialize(path, options = {})
|
6
|
+
@path = path
|
7
|
+
@param = options[:param]
|
8
|
+
end
|
9
|
+
|
10
|
+
def param
|
11
|
+
@param ||= (0...8).map{65.+(rand(25)).chr}.join
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_s
|
15
|
+
@path.gsub('*', param)
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'traffic_cop'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
|
12
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe RedirectCheck do
|
4
|
+
context "#domain" do
|
5
|
+
it "sets the domain" do
|
6
|
+
RedirectCheck.domain = 'example.com'
|
7
|
+
RedirectCheck.domain.should == 'example.com'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
context "#uri" do
|
12
|
+
it "returns the parsed URI" do
|
13
|
+
RedirectCheck.domain = 'example.com'
|
14
|
+
RedirectCheck.new('/blog').uri.host.should == "example.com"
|
15
|
+
RedirectCheck.new('/blog').uri.path.should == "/blog"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: traffic_cop
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Shay Frendt
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-05-19 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: test-unit
|
17
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - "="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 2.3.0
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rspec
|
28
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.6.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *id002
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: bundler
|
39
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: 1.0.0
|
45
|
+
type: :development
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *id003
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: jeweler
|
50
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ~>
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 1.6.0
|
56
|
+
type: :development
|
57
|
+
prerelease: false
|
58
|
+
version_requirements: *id004
|
59
|
+
description: TrafficCop is a collection of test macros to test HTTP redirect rules for remote servers.
|
60
|
+
email: shay.frendt@gmail.com
|
61
|
+
executables: []
|
62
|
+
|
63
|
+
extensions: []
|
64
|
+
|
65
|
+
extra_rdoc_files:
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.rdoc
|
68
|
+
files:
|
69
|
+
- .document
|
70
|
+
- .rspec
|
71
|
+
- .rvmrc
|
72
|
+
- Gemfile
|
73
|
+
- Gemfile.lock
|
74
|
+
- LICENSE.txt
|
75
|
+
- README.rdoc
|
76
|
+
- Rakefile
|
77
|
+
- VERSION
|
78
|
+
- lib/traffic_cop.rb
|
79
|
+
- lib/traffic_cop/http_redirect_test.rb
|
80
|
+
- lib/traffic_cop/redirect_check.rb
|
81
|
+
- lib/traffic_cop/resource_path.rb
|
82
|
+
- lib/traffic_cop/version.rb
|
83
|
+
- spec/spec_helper.rb
|
84
|
+
- spec/traffic_cop/redirect_check_spec.rb
|
85
|
+
homepage: http://github.com/shayfrendt/traffic_cop
|
86
|
+
licenses:
|
87
|
+
- MIT
|
88
|
+
post_install_message:
|
89
|
+
rdoc_options: []
|
90
|
+
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
hash: 4106097813317168594
|
99
|
+
segments:
|
100
|
+
- 0
|
101
|
+
version: "0"
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: "0"
|
108
|
+
requirements: []
|
109
|
+
|
110
|
+
rubyforge_project:
|
111
|
+
rubygems_version: 1.8.2
|
112
|
+
signing_key:
|
113
|
+
specification_version: 3
|
114
|
+
summary: TrafficCop is a collection of test macros to test HTTP redirect rules for remote servers.
|
115
|
+
test_files: []
|
116
|
+
|