http_redirect_test 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,51 +3,68 @@ require File.join(File.dirname(__FILE__), "resource_path")
3
3
  require 'test/unit'
4
4
 
5
5
  class HTTPRedirectTest < Test::Unit::TestCase
6
+ # permanent can be overriden in 2 ways, with a call to self.permanent to set globally. Or via the options array for each redirect call
7
+ @permanent=nil
6
8
 
7
9
  def default_test; end # placeholder to stop Test::Unit from complaining
8
10
 
9
- def self.domain=(domain)
10
- RedirectCheck.domain = domain
11
- end
11
+ class <<self
12
+ def domain=(domain)
13
+ @domain = domain
14
+ end
12
15
 
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
16
+ def domain
17
+ @domain
18
+ end
22
19
 
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
20
+ def permanent=(permanent)
21
+ @permanent=permanent
22
+ end
23
+
24
+ def permanent?
25
+ @permanent.nil? ? false : true
26
+ end
27
+
28
+ def should_not_redirect(path)
29
+ class_eval <<-CODE
30
+ def test_#{name_for(path)}_should_not_redirect
31
+ check = RedirectCheck.new(self.class.domain, '#{path}')
32
+ assert_equal false, check.redirected?, "#{path} is redirecting"
33
+ assert_equal true, check.success?, "#{path} is not a success response"
34
+ end
35
+ CODE
36
+ end
37
+
38
+ def name_for(path)
39
+ name = path.gsub(/\W+/, '_')
40
+ name.gsub!(/^_+/, '')
41
+ name.gsub!(/_+$/, '')
42
+
43
+ name = 'root' if name == ''
44
+
45
+ name
46
+ end
47
+
48
+ def should_redirect(source, options)
49
+ source_path = ResourcePath.new(source, :param => 'subdir').to_s
50
+ destination_path = ResourcePath.new(options[:to], :param => 'subdir').to_s
51
+
52
+ @permanent = options.fetch(:permanent, true) if @permanent.nil?
53
+
54
+ class_eval <<-CODE
55
+ def test_#{name_for(source_path)}_should_redirect_to_#{name_for(destination_path)}
56
+ redirection = RedirectCheck.new(self.class.domain, '#{source_path}', '#{destination_path}')
57
+ assert_equal true, redirection.redirected?, "'#{source_path}' is not redirecting"
58
+ assert_equal '#{destination_path}', redirection.redirected_path,
59
+ "'#{source_path}' is not redirecting to '#{destination_path}'"
32
60
 
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
61
+ if #{@permanent}
62
+ assert_equal true, redirection.permanent_redirect?,
63
+ "The redirection from '#{source_path}' to '#{destination_path}' doesn't appear to be a permanent redirect"
64
+ end
65
+ end
66
+ CODE
67
+ end
51
68
  end
52
69
 
53
- end
70
+ end
@@ -1,45 +1,37 @@
1
1
  require 'uri'
2
2
  require 'net/http'
3
-
3
+
4
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
5
  attr_reader :source_path, :destination_path
15
-
16
- def initialize(source_path, destination_path = nil)
6
+
7
+ def initialize(domain, source_path, destination_path = nil)
8
+ @domain = domain
17
9
  @source_path = source_path.to_s
18
10
  @destination_path = destination_path.to_s
19
11
  end
20
-
12
+
21
13
  def uri
22
- URI.parse("http://#{self.class.domain}#{source_path}")
14
+ URI.parse("http://#{@domain}#{source_path}")
23
15
  end
24
-
16
+
25
17
  def response
26
- @response ||= Net::HTTP.start(uri.host, uri.port) {|http| http.head(uri.path) }
18
+ @response ||= Net::HTTP.start(uri.host, uri.port) {|http| return http.head(uri.path) }
27
19
  end
28
-
20
+
29
21
  def success?
30
22
  response.is_a?(Net::HTTPOK)
31
23
  end
32
-
24
+
33
25
  def redirected?
34
26
  response.is_a?(Net::HTTPRedirection)
35
27
  end
36
-
28
+
37
29
  def permanent_redirect?
38
30
  redirected? && response.is_a?(Net::HTTPMovedPermanently)
39
31
  end
40
-
32
+
41
33
  def redirected_path
42
34
  response['location'].sub(/#{Regexp.escape("#{uri.scheme}://#{uri.host}")}/, '') if redirected?
43
35
  end
44
-
45
- end
36
+
37
+ end
@@ -1,18 +1,18 @@
1
1
  class ResourcePath
2
-
2
+
3
3
  attr_writer :param
4
-
4
+
5
5
  def initialize(path, options = {})
6
6
  @path = path
7
7
  @param = options[:param]
8
8
  end
9
-
9
+
10
10
  def param
11
11
  @param ||= (0...8).map{65.+(rand(25)).chr}.join
12
12
  end
13
-
13
+
14
14
  def to_s
15
15
  @path.gsub('*', param)
16
16
  end
17
-
17
+
18
18
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http_redirect_test
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt House
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-08 00:00:00 +01:00
12
+ date: 2009-12-04 00:00:00 +00:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -26,8 +26,10 @@ files:
26
26
  - lib/http_redirect_test/redirect_check.rb
27
27
  - lib/http_redirect_test/resource_path.rb
28
28
  - lib/http_redirect_test.rb
29
- has_rdoc: false
30
- homepage: http://github.com/shadowaspect
29
+ has_rdoc: true
30
+ homepage: http://github.com/shadowaspect/http_redirect_test
31
+ licenses: []
32
+
31
33
  post_install_message:
32
34
  rdoc_options: []
33
35
 
@@ -48,9 +50,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
48
50
  requirements: []
49
51
 
50
52
  rubyforge_project:
51
- rubygems_version: 1.3.1
53
+ rubygems_version: 1.3.5
52
54
  signing_key:
53
- specification_version: 2
55
+ specification_version: 3
54
56
  summary: A subclass of Test::Unit::TestCase to allow for http redirect testing, packaged from a gist by Patrick Reagan of Viget Labs
55
57
  test_files: []
56
58