http_redirect_test 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,85 @@
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
+ # 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
8
+
9
+ def default_test; end # placeholder to stop Test::Unit from complaining
10
+
11
+ class <<self
12
+ attr_accessor :domain
13
+ attr_writer :permanent
14
+
15
+ def permanent?
16
+ @permanent.nil? ? false : true
17
+ end
18
+
19
+ def name_for(path)
20
+ name = path.strip.gsub(/\W+/, '_')
21
+ name = 'root' if name == ''
22
+ name
23
+ end
24
+
25
+ def should_not_redirect(path)
26
+ class_eval <<-CODE
27
+ def test_#{name_for(path)}_should_not_redirect
28
+ check = RedirectCheck.new(self.class.domain, '#{path}')
29
+ assert_equal false, check.redirected?, "#{path} is redirecting"
30
+ assert_equal true, check.success?, "#{path} is not a success response"
31
+ end
32
+ CODE
33
+ end
34
+
35
+ def should_redirect(source, options)
36
+ source_path = ResourcePath.new(source, :param => 'subdir').to_s
37
+ destination_path = ResourcePath.new(options[:to], :param => 'subdir').to_s
38
+
39
+ @permanent = options.fetch(:permanent, true) if @permanent.nil?
40
+
41
+ class_eval <<-CODE
42
+ def test_#{name_for(source_path)}_should_redirect_to_#{name_for(destination_path)}
43
+ redirection = RedirectCheck.new(self.class.domain, '#{source_path}', '#{destination_path}')
44
+ assert_equal true, redirection.redirected?, "'#{source_path}' is not redirecting"
45
+ assert_equal '#{destination_path}', redirection.redirected_path,
46
+ "'#{source_path}' is not redirecting to '#{destination_path}'"
47
+
48
+ if #{@permanent}
49
+ assert_equal true, redirection.permanent_redirect?,
50
+ "The redirection from '#{source_path}' to '#{destination_path}' doesn't appear to be a permanent redirect"
51
+ end
52
+ end
53
+ CODE
54
+ end
55
+
56
+ def should_be_gone(path)
57
+ class_eval <<-CODE
58
+ def test_#{name_for(path)}_should_be_gone
59
+ check = RedirectCheck.new(self.class.domain, '#{path}')
60
+ assert check.gone?
61
+ end
62
+ CODE
63
+ end
64
+
65
+ def should_not_be_found(path)
66
+ class_eval <<-CODE
67
+ def test_#{name_for(path)}_should_be_gone
68
+ check = RedirectCheck.new(self.class.domain, '#{path}')
69
+ assert check.not_found?
70
+ end
71
+ CODE
72
+ end
73
+
74
+ def should_have_header(path, header, options)
75
+ value = options[:with_value]
76
+ class_eval <<-CODE
77
+ def test_should_return_value_for_#{name_for(header)}_header
78
+ check = RedirectCheck.new(self.class.domain, '#{path}')
79
+ assert_equal '#{value}', check.header('#{header}')
80
+ end
81
+ CODE
82
+ end
83
+ end
84
+
85
+ end
@@ -0,0 +1,53 @@
1
+ require 'uri'
2
+ require 'net/http'
3
+
4
+ class RedirectCheck
5
+ attr_reader :source_path, :destination_path
6
+
7
+ def initialize(domain, source_path, destination_path = nil)
8
+ @domain = domain
9
+ @source_path = source_path.to_s
10
+ @destination_path = destination_path.to_s
11
+ end
12
+
13
+ def uri
14
+ URI.parse("http://#{@domain}#{source_path}")
15
+ end
16
+
17
+ def source_uri
18
+ @source_uri ||= (uri.query.nil?) ? uri.path : uri.path + "?" + uri.query
19
+ end
20
+
21
+ def response
22
+ @response ||= Net::HTTP.start(uri.host, uri.port) {|http| return http.head(source_uri) }
23
+ end
24
+
25
+ def success?
26
+ response.is_a?(Net::HTTPOK)
27
+ end
28
+
29
+ def gone?
30
+ response.is_a?(Net::HTTPGone)
31
+ end
32
+
33
+ def not_found?
34
+ response.is_a?(Net::HTTPNotFound)
35
+ end
36
+
37
+ def redirected?
38
+ response.is_a?(Net::HTTPRedirection)
39
+ end
40
+
41
+ def permanent_redirect?
42
+ redirected? && response.is_a?(Net::HTTPMovedPermanently)
43
+ end
44
+
45
+ def redirected_path
46
+ response['location'].sub(/#{Regexp.escape("#{uri.scheme}://#{uri.host}")}/, '') if redirected?
47
+ end
48
+
49
+ def header(name)
50
+ response.key?(name) && response.fetch(name)
51
+ end
52
+
53
+ 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
@@ -0,0 +1,19 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'http_redirect_test'
3
+
4
+ class HttpRedirectTestTests < Test::Unit::TestCase
5
+ def test_that_permanent_flag_is_being_set_correctly
6
+ assert_equal false, HTTPRedirectTest.permanent?
7
+ HTTPRedirectTest.permanent=true
8
+ assert_equal true, HTTPRedirectTest.permanent?
9
+ end
10
+
11
+ def test_each_class_should_have_its_own_domain
12
+ a = Class.new(HTTPRedirectTest)
13
+ a.__send__(:domain=, "a.example.com")
14
+ b = Class.new(HTTPRedirectTest)
15
+ b.__send__(:domain=, "b.example.com")
16
+ assert_equal "a.example.com", a.__send__(:domain)
17
+ assert_equal "b.example.com", b.__send__(:domain)
18
+ end
19
+ end
@@ -0,0 +1,80 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'http_redirect_test'
3
+
4
+
5
+ class RedirectCheckTest < Test::Unit::TestCase
6
+
7
+ require 'net/http'
8
+
9
+ def test_that_redirect_checker_returns_legitimate_response
10
+ stub_http_head_request(Net::HTTPOK)
11
+ assert_equal @http_head, RedirectCheck.new("example.com", "/index.html").response
12
+ end
13
+
14
+ def test_that_http_ok_results_in_success
15
+ stub_http_head_request(Net::HTTPOK)
16
+ assert RedirectCheck.new("example.com", "/index.html").success?
17
+ end
18
+
19
+ def test_that_http_redirection_resuts_in_positive_redirection
20
+ stub_http_head_request(Net::HTTPRedirection)
21
+ assert RedirectCheck.new("example.com", "/index.html").redirected?
22
+ end
23
+
24
+ def test_that_http_301_resuts_in_permanent_redirection
25
+ stub_http_head_request(Net::HTTPMovedPermanently)
26
+ assert RedirectCheck.new("example.com", "/index.html").permanent_redirect?
27
+ end
28
+
29
+ def test_that_gone_page_returns_correctly
30
+ stub_http_head_request(Net::HTTPGone)
31
+ assert RedirectCheck.new("example.com", "/index.html").gone?
32
+ end
33
+
34
+ def test_that_not_found_page_returns_correctly
35
+ stub_http_head_request(Net::HTTPNotFound)
36
+ assert RedirectCheck.new("example.com", "/index.html").not_found?
37
+ end
38
+
39
+ def test_that_uri_can_be_built_correctly
40
+ assert_equal RedirectCheck.new("example.com", "/index.html").uri, URI.parse("http://example.com/index.html")
41
+ end
42
+
43
+ def test_redirection_path
44
+ stub_http_head_request(Net::HTTPMovedPermanently)
45
+ r = RedirectCheck.new("example.com", "/index.html")
46
+ r.response['location'] = "http://www.someurl.com"
47
+
48
+ assert_equal "http://www.someurl.com", r.redirected_path
49
+ end
50
+
51
+ def test_redirection_path_without_query_params
52
+ assert_equal "/index.html", RedirectCheck.new("example.com", "/index.html").source_uri
53
+ end
54
+
55
+ def test_redirection_path_with_query_params
56
+ assert_equal "/index.html?foo=bar", RedirectCheck.new("example.com", "/index.html?foo=bar").source_uri
57
+ end
58
+
59
+ def test_request_for_specific_header
60
+ stub_http_header('X-Powered-By', 'Pixies and Stardust')
61
+ assert RedirectCheck.new("example.com", "/index.html").header('X-Powered-By') == "Pixies and Stardust"
62
+ end
63
+
64
+ private
65
+ def stub_http_head_request(response_type)
66
+ @http_head = response_type.new(stub, stub, stub)
67
+ @http_session = stub(:session)
68
+ @http_session.stubs(:head).with("/index.html").returns(@http_head)
69
+ Net::HTTP.stubs(:start).with("example.com", 80).yields(@http_session)
70
+ end
71
+
72
+ def stub_http_header(header_name, value)
73
+ @http_head = Net::HTTPOK.new(stub, stub, stub)
74
+ @http_head.stubs(:key?).with(header_name).returns(true)
75
+ @http_head.stubs(:fetch).with(header_name).returns(value)
76
+ @http_session = stub(:session)
77
+ @http_session.stubs(:head).with("/index.html").returns(@http_head)
78
+ Net::HTTP.stubs(:start).with("example.com", 80).yields(@http_session)
79
+ end
80
+ end
@@ -0,0 +1,11 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'http_redirect_test'
3
+
4
+ class ResourcePathTest < Test::Unit::TestCase
5
+
6
+ def test_that_resource_path_is_created_successfully
7
+ rp = ResourcePath.new("")
8
+ assert rp.instance_of? ResourcePath
9
+ end
10
+
11
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http_redirect_test
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 29
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 2
10
- version: 0.1.2
9
+ - 3
10
+ version: 0.1.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Matt House
@@ -43,9 +43,14 @@ extensions: []
43
43
  extra_rdoc_files: []
44
44
 
45
45
  files:
46
- - bin/http_redirect_test.rb
46
+ - lib/http_redirect_test/http_redirect_test.rb
47
+ - lib/http_redirect_test/redirect_check.rb
48
+ - lib/http_redirect_test/resource_path.rb
47
49
  - lib/http_redirect_test.rb
48
50
  - test/test_helper.rb
51
+ - test/unit/http_redirect_test_test.rb
52
+ - test/unit/redirect_check_test.rb
53
+ - test/unit/resource_path_test.rb
49
54
  has_rdoc: true
50
55
  homepage: http://github.com/eightbitraptor/http_redirect_test
51
56
  licenses: []
@@ -1,17 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'http_redirect_test'
4
- require 'optparse'
5
-
6
- OptionParser.new do |opts|
7
- opts.banner = "Usage: http_redirect_test command project_name"
8
-
9
- opts.on("g", "generate DIR", "The directory your projects are located, defaults to ~/code") do |d|
10
- @dir = d
11
- end
12
-
13
- opts.on_tail("-h", "--help", "Show this help message.") do
14
- puts opts;
15
- exit
16
- end
17
- end.parse!