http_redirect_test 0.1.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 64f09a427c707013226be65e99ca2046031f4193
4
+ data.tar.gz: 92ecc2d00aab3052558edd3793ae522e1b947943
5
+ SHA512:
6
+ metadata.gz: b6fde8b137000b1e2636c3cd3f6d9266413fe75831c60052409b00b980c2e44f770f7bfc1f2b413f01f91b19f3ed7f7cab436df52e72fec695e044f316014bb7
7
+ data.tar.gz: 8d56d9b74eb42a453218b1ba164009db22ae5ac95501e5df9e85192c9d770df7a5528ba7c738929f9f22a15cc4e70c9bcf28b2e6ecaf606cf17f291b62efe622
@@ -1,85 +1,72 @@
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
1
+ class HTTPRedirectTest < MiniTest::Test
7
2
  @permanent=nil
8
3
 
9
- def default_test; end # placeholder to stop Test::Unit from complaining
10
-
11
- class <<self
12
- attr_accessor :domain
13
- attr_writer :permanent
4
+ class << self
5
+ attr_reader :domain, :permanent
14
6
 
15
- def permanent?
16
- @permanent.nil? ? false : true
7
+ def set_domain(domain)
8
+ @domain = domain
17
9
  end
18
10
 
19
- def name_for(path)
20
- name = path.strip.gsub(/\W+/, '_')
21
- name = 'root' if name == ''
22
- name
11
+ def treat_all_redirects_as_permanent
12
+ @permanent = true
23
13
  end
24
14
 
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
15
+ def permanent?
16
+ !!@permanent
33
17
  end
18
+ end
34
19
 
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
20
+ def should_not_redirect(path)
21
+ assert_equal false, check_for(path).redirected?, "#{path} is redirecting"
22
+ assert_equal true, check_for(path).success?, "#{path} is not a success response"
23
+ end
55
24
 
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
25
+ def should_redirect(source, options)
26
+ source_path = ResourcePath.new(source, :param => 'subdir').to_s
27
+ destination_path = ResourcePath.new(options[:to], :param => 'subdir').to_s
64
28
 
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
29
+ redirection = check_for(source_path, destination_path)
30
+ assert_equal true, redirection.redirected?, "'#{source_path}' is not redirecting"
31
+ assert_equal destination_path, redirection.redirected_path,
32
+ "'#{source_path}' is not redirecting to '#{destination_path}'"
73
33
 
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
34
+ return if options[:permanent] == false
35
+
36
+ if permanent? || options[:permanent] == true
37
+ assert_equal true, redirection.permanent_redirect?,
38
+ "The redirection from '#{source_path}' to '#{destination_path}' doesn't appear to be a permanent redirect"
82
39
  end
83
40
  end
84
41
 
42
+ def should_be_gone(path)
43
+ assert check_for(path).gone?
44
+ end
45
+
46
+ def should_not_be_found(path)
47
+ assert check_for(path).not_found?
48
+ end
49
+
50
+ def should_have_header(path, header, options)
51
+ value = options[:with_value]
52
+ assert_equal value, check_for(path).header(header)
53
+ end
54
+
55
+ private
56
+
57
+ def check_for(path, destination=nil)
58
+ RedirectCheck.new(self.class.domain, path, destination)
59
+ end
60
+
61
+ def permanent?
62
+ !!self.class.permanent
63
+ end
64
+
65
+ def name_for(path)
66
+ if path.empty?
67
+ 'root'
68
+ else
69
+ path.strip.gsub(/\W+/, '_')
70
+ end
71
+ end
85
72
  end
@@ -11,7 +11,7 @@ class RedirectCheck
11
11
  end
12
12
 
13
13
  def uri
14
- URI.parse("http://#{@domain}#{source_path}")
14
+ URI.parse("#{scheme}://#{raw_domain}#{source_path}")
15
15
  end
16
16
 
17
17
  def source_uri
@@ -19,7 +19,9 @@ class RedirectCheck
19
19
  end
20
20
 
21
21
  def response
22
- @response ||= Net::HTTP.start(uri.host, uri.port) {|http| return http.head(source_uri) }
22
+ @response ||= Net::HTTP.start(uri.host, uri.port, use_ssl: ssl?) do |http|
23
+ return http.head(source_uri)
24
+ end
23
25
  end
24
26
 
25
27
  def success?
@@ -43,11 +45,32 @@ class RedirectCheck
43
45
  end
44
46
 
45
47
  def redirected_path
46
- response['location'].sub(/#{Regexp.escape("#{uri.scheme}://#{uri.host}")}/, '') if redirected?
48
+ response['location'].sub(/#{Regexp.escape("#{uri.scheme}://#{uri.host}")}:#{uri.port}/, '') if redirected?
47
49
  end
48
50
 
49
51
  def header(name)
50
- response.key?(name) && response.fetch(name)
52
+ response[name]
51
53
  end
52
54
 
55
+ private
56
+
57
+ def scheme
58
+ if domain_components.length == 2
59
+ scheme = domain_components.first
60
+ end
61
+
62
+ scheme || 'http'
63
+ end
64
+
65
+ def raw_domain
66
+ domain_components.last
67
+ end
68
+
69
+ def ssl?
70
+ scheme == 'https'
71
+ end
72
+
73
+ def domain_components
74
+ @domain_components ||= @domain.split('://')
75
+ end
53
76
  end
@@ -1,5 +1,4 @@
1
1
  class ResourcePath
2
-
3
2
  attr_writer :param
4
3
 
5
4
  def initialize(path, options = {})
@@ -14,5 +13,4 @@ class ResourcePath
14
13
  def to_s
15
14
  @path.gsub('*', param)
16
15
  end
17
-
18
- end
16
+ end
@@ -1 +1,7 @@
1
- require File.join(File.dirname(__FILE__), "http_redirect_test", "http_redirect_test")
1
+ require 'minitest'
2
+ require 'uri'
3
+ require 'net/http'
4
+
5
+ require 'http_redirect_test/redirect_check'
6
+ require 'http_redirect_test/resource_path'
7
+ require 'http_redirect_test/http_redirect_test'
@@ -0,0 +1,69 @@
1
+ require 'http_redirect_test'
2
+
3
+ class IntegrationTest < HTTPRedirectTest
4
+ set_domain "localhost:4567"
5
+
6
+ def test_not_redirect
7
+ should_not_redirect '/ok'
8
+ end
9
+
10
+ def test_should_redirect_non_permanantly
11
+ should_redirect '/redirect', to: '/ok'
12
+ end
13
+
14
+ def should_redirect_permanently
15
+ should_redirect '/redirect_permanant', to: '/ok', permanent: true
16
+ end
17
+
18
+ def test_should_not_be_found
19
+ should_not_be_found '/not_found'
20
+ end
21
+
22
+ def test_should_be_gone
23
+ should_be_gone '/gone'
24
+ end
25
+
26
+ def test_custom_header
27
+ should_have_header('/custom_header', 'X-One-Million-Years', with_value: 'Dungeon')
28
+ end
29
+ end
30
+
31
+ class AllRedirectsArePermanent < HTTPRedirectTest
32
+ set_domain "localhost:4567"
33
+ treat_all_redirects_as_permanent
34
+
35
+ def test_should_redirect
36
+ should_redirect '/redirect_permanant', to: '/ok'
37
+ end
38
+
39
+ def test_non_permanent_redirect_should_fail
40
+ assertion_failed = begin
41
+ should_redirect '/redirect', to: '/ok'
42
+ false
43
+ rescue MiniTest::Assertion
44
+ true
45
+ end
46
+
47
+ assert assertion_failed
48
+ end
49
+
50
+ def test_explicit_non_permanent_check_should_override
51
+ should_redirect '/redirect', to: '/ok', permanent: false
52
+ end
53
+ end
54
+
55
+ class DomainsWithExplicitSSLSchemes < HTTPRedirectTest
56
+ set_domain "https://uk.yahoo.com"
57
+
58
+ def test_should_not_redirect
59
+ should_not_redirect '/'
60
+ end
61
+ end
62
+
63
+ class DomainsWithExplicitNonSSLSchemes < HTTPRedirectTest
64
+ set_domain "http://www.example.com"
65
+
66
+ def test_should_not_redirect
67
+ should_not_redirect '/'
68
+ end
69
+ end
@@ -0,0 +1,28 @@
1
+ require 'sinatra'
2
+
3
+ get '/ok' do
4
+ status 200
5
+ end
6
+
7
+ get '/redirect' do
8
+ redirect '/ok'
9
+ end
10
+
11
+ get '/redirect_permanant' do
12
+ redirect '/ok', 301
13
+ end
14
+
15
+ get '/not_found' do
16
+ status 404
17
+ end
18
+
19
+ get '/gone' do
20
+ status 410
21
+ end
22
+
23
+ get '/custom_header' do
24
+ headers['X-One-Million-Years'] = 'Dungeon'
25
+ status 200
26
+ end
27
+
28
+
data/test/test_helper.rb CHANGED
@@ -1,5 +1,5 @@
1
- $:.unshift File.expand_path(File.join(File.dirname(__FILE__), "..", "lib"))
1
+ require 'minitest/autorun'
2
+
3
+ require 'http_redirect_test'
4
+ require 'mocha/mini_test'
2
5
 
3
- require 'rubygems'
4
- require 'test/unit'
5
- require 'mocha'
@@ -1,18 +1,19 @@
1
- require File.join(File.dirname(__FILE__), "..", "test_helper")
2
- require 'http_redirect_test'
1
+ require 'test_helper'
3
2
 
4
- class HttpRedirectTestTests < Test::Unit::TestCase
3
+ class HttpRedirectTestTests < MiniTest::Test
5
4
  def test_that_permanent_flag_is_being_set_correctly
6
5
  assert_equal false, HTTPRedirectTest.permanent?
7
- HTTPRedirectTest.permanent=true
6
+
7
+ HTTPRedirectTest.treat_all_redirects_as_permanent
8
+
8
9
  assert_equal true, HTTPRedirectTest.permanent?
9
10
  end
10
11
 
11
12
  def test_each_class_should_have_its_own_domain
12
13
  a = Class.new(HTTPRedirectTest)
13
- a.__send__(:domain=, "a.example.com")
14
+ a.set_domain "a.example.com"
14
15
  b = Class.new(HTTPRedirectTest)
15
- b.__send__(:domain=, "b.example.com")
16
+ b.set_domain "b.example.com"
16
17
  assert_equal "a.example.com", a.__send__(:domain)
17
18
  assert_equal "b.example.com", b.__send__(:domain)
18
19
  end
@@ -1,10 +1,11 @@
1
- require File.join(File.dirname(__FILE__), "..", "test_helper")
2
- require 'http_redirect_test'
1
+ require 'test_helper'
2
+ require 'net/http'
3
3
 
4
+ class RedirectCheckTest < MiniTest::Test
4
5
 
5
- class RedirectCheckTest < Test::Unit::TestCase
6
-
7
- require 'net/http'
6
+ def test_redirect_check_supports_https
7
+ assert_equal 'https', RedirectCheck.new('https://www.example.com', '/dontcare').uri.scheme
8
+ end
8
9
 
9
10
  def test_that_redirect_checker_returns_legitimate_response
10
11
  stub_http_head_request(Net::HTTPOK)
@@ -66,15 +67,16 @@ private
66
67
  @http_head = response_type.new(stub, stub, stub)
67
68
  @http_session = stub(:session)
68
69
  @http_session.stubs(:head).with("/index.html").returns(@http_head)
69
- Net::HTTP.stubs(:start).with("example.com", 80).yields(@http_session)
70
+ Net::HTTP.stubs(:start).with("example.com", 80, use_ssl: false).yields(@http_session)
70
71
  end
71
72
 
72
73
  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)
74
+ http_head = Net::HTTPOK.new(stub, stub, stub)
75
+ http_head.initialize_http_header({ header_name => value })
76
+
77
+ http_session = stub(:session)
78
+ http_session.stubs(:head).with("/index.html").returns(http_head)
79
+
80
+ Net::HTTP.stubs(:start).with("example.com", 80, use_ssl: false).yields(http_session)
79
81
  end
80
82
  end
@@ -1,11 +1,9 @@
1
- require File.join(File.dirname(__FILE__), "..", "test_helper")
2
- require 'http_redirect_test'
1
+ require 'test_helper'
3
2
 
4
- class ResourcePathTest < Test::Unit::TestCase
3
+ class ResourcePathTest < MiniTest::Test
5
4
 
6
5
  def test_that_resource_path_is_created_successfully
7
6
  rp = ResourcePath.new("")
8
7
  assert rp.instance_of? ResourcePath
9
8
  end
10
-
11
- end
9
+ end
metadata CHANGED
@@ -1,89 +1,109 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: http_redirect_test
3
- version: !ruby/object:Gem::Version
4
- hash: 29
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 1
9
- - 3
10
- version: 0.1.3
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Matt House
14
8
  autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
-
18
- date: 2010-10-21 00:00:00 +01:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
11
+ date: 2010-10-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 5.3.5
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 5.3.5
27
+ - !ruby/object:Gem::Dependency
22
28
  name: mocha
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.1.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 1.1.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 10.3.0
48
+ type: :development
23
49
  prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 49
30
- segments:
31
- - 0
32
- - 9
33
- - 5
34
- version: 0.9.5
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 10.3.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: sinatra
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.4.5
35
62
  type: :development
36
- version_requirements: *id001
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 1.4.5
37
69
  description:
38
70
  email: matt@eightbitraptor.com
39
71
  executables: []
40
-
41
72
  extensions: []
42
-
43
73
  extra_rdoc_files: []
44
-
45
- files:
74
+ files:
75
+ - lib/http_redirect_test.rb
46
76
  - lib/http_redirect_test/http_redirect_test.rb
47
77
  - lib/http_redirect_test/redirect_check.rb
48
78
  - lib/http_redirect_test/resource_path.rb
49
- - lib/http_redirect_test.rb
79
+ - test/integration_test.rb
80
+ - test/test_app/test_redirect_server.rb
50
81
  - test/test_helper.rb
51
82
  - test/unit/http_redirect_test_test.rb
52
83
  - test/unit/redirect_check_test.rb
53
84
  - test/unit/resource_path_test.rb
54
- has_rdoc: true
55
85
  homepage: http://github.com/eightbitraptor/http_redirect_test
56
86
  licenses: []
57
-
87
+ metadata: {}
58
88
  post_install_message:
59
89
  rdoc_options: []
60
-
61
- require_paths:
90
+ require_paths:
62
91
  - lib
63
- required_ruby_version: !ruby/object:Gem::Requirement
64
- none: false
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- hash: 3
69
- segments:
70
- - 0
71
- version: "0"
72
- required_rubygems_version: !ruby/object:Gem::Requirement
73
- none: false
74
- requirements:
75
- - - ">="
76
- - !ruby/object:Gem::Version
77
- hash: 3
78
- segments:
79
- - 0
80
- version: "0"
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
81
102
  requirements: []
82
-
83
103
  rubyforge_project:
84
- rubygems_version: 1.3.7
104
+ rubygems_version: 2.2.2
85
105
  signing_key:
86
- specification_version: 3
87
- summary: Test Apache behavior using Test::Unit, based on a gist by Patrick Reagan of Viget Labs
106
+ specification_version: 4
107
+ summary: Test Apache behavior using Test::Unit, based on a gist by Patrick Reagan
108
+ of Viget Labs
88
109
  test_files: []
89
-