http_redirect_test 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "http_redirect_test", "http_redirect_test")
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "redirect_check")
|
2
|
+
require File.join(File.dirname(__FILE__), "resource_path")
|
3
|
+
require 'test/unit'
|
4
|
+
|
5
|
+
class HTTPRedirectTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def default_test; end # placeholder to stop Test::Unit from complaining
|
8
|
+
|
9
|
+
def self.domain=(domain)
|
10
|
+
RedirectCheck.domain = domain
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.should_not_redirect(path)
|
14
|
+
class_eval <<-CODE
|
15
|
+
def test_#{name_for(path)}_should_not_redirect
|
16
|
+
check = RedirectCheck.new('#{path}')
|
17
|
+
assert_equal false, check.redirected?, "#{path} is redirecting"
|
18
|
+
assert_equal true, check.success?, "#{path} is not a success response"
|
19
|
+
end
|
20
|
+
CODE
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.name_for(path)
|
24
|
+
name = path.gsub(/\W+/, '_')
|
25
|
+
name.gsub!(/^_+/, '')
|
26
|
+
name.gsub!(/_+$/, '')
|
27
|
+
|
28
|
+
name = 'root' if name == ''
|
29
|
+
|
30
|
+
name
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.should_redirect(source, options)
|
34
|
+
source_path = ResourcePath.new(source, :param => 'subdir').to_s
|
35
|
+
destination_path = ResourcePath.new(options[:to], :param => 'subdir').to_s
|
36
|
+
|
37
|
+
permanent = options.fetch(:permanent, true)
|
38
|
+
|
39
|
+
class_eval <<-CODE
|
40
|
+
def test_#{name_for(source_path)}_should_redirect_to_#{name_for(destination_path)}
|
41
|
+
redirection = RedirectCheck.new('#{source_path}', '#{destination_path}')
|
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
|
+
if #{permanent}
|
46
|
+
assert_equal true, redirection.permanent_redirect?,
|
47
|
+
"The redirection from '#{source_path}' to '#{destination_path}' doesn't appear to be a permanent redirect"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
CODE
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
@@ -0,0 +1,45 @@
|
|
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
|
@@ -0,0 +1,18 @@
|
|
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
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: http_redirect_test
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matt House
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-04-08 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: matt@theshadowaspect.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib/http_redirect_test/http_redirect_test.rb
|
26
|
+
- lib/http_redirect_test/redirect_check.rb
|
27
|
+
- lib/http_redirect_test/resource_path.rb
|
28
|
+
- lib/http_redirect_test.rb
|
29
|
+
has_rdoc: false
|
30
|
+
homepage: http://github.com/shadowaspect
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: "0"
|
41
|
+
version:
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
requirements: []
|
49
|
+
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.3.1
|
52
|
+
signing_key:
|
53
|
+
specification_version: 2
|
54
|
+
summary: A subclass of Test::Unit::TestCase to allow for http redirect testing, packaged from a gist by Patrick Reagan of Viget Labs
|
55
|
+
test_files: []
|
56
|
+
|