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 +7 -0
- data/lib/http_redirect_test/http_redirect_test.rb +56 -69
- data/lib/http_redirect_test/redirect_check.rb +27 -4
- data/lib/http_redirect_test/resource_path.rb +1 -3
- data/lib/http_redirect_test.rb +7 -1
- data/test/integration_test.rb +69 -0
- data/test/test_app/test_redirect_server.rb +28 -0
- data/test/test_helper.rb +4 -4
- data/test/unit/http_redirect_test_test.rb +7 -6
- data/test/unit/redirect_check_test.rb +14 -12
- data/test/unit/resource_path_test.rb +3 -5
- metadata +79 -59
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
|
-
|
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
|
-
|
10
|
-
|
11
|
-
class <<self
|
12
|
-
attr_accessor :domain
|
13
|
-
attr_writer :permanent
|
4
|
+
class << self
|
5
|
+
attr_reader :domain, :permanent
|
14
6
|
|
15
|
-
def
|
16
|
-
@
|
7
|
+
def set_domain(domain)
|
8
|
+
@domain = domain
|
17
9
|
end
|
18
10
|
|
19
|
-
def
|
20
|
-
|
21
|
-
name = 'root' if name == ''
|
22
|
-
name
|
11
|
+
def treat_all_redirects_as_permanent
|
12
|
+
@permanent = true
|
23
13
|
end
|
24
14
|
|
25
|
-
def
|
26
|
-
|
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
|
-
|
36
|
-
|
37
|
-
|
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
|
-
|
57
|
-
|
58
|
-
|
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
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
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
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
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("
|
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)
|
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
|
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
|
data/lib/http_redirect_test.rb
CHANGED
@@ -1 +1,7 @@
|
|
1
|
-
require
|
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,18 +1,19 @@
|
|
1
|
-
require
|
2
|
-
require 'http_redirect_test'
|
1
|
+
require 'test_helper'
|
3
2
|
|
4
|
-
class HttpRedirectTestTests < Test
|
3
|
+
class HttpRedirectTestTests < MiniTest::Test
|
5
4
|
def test_that_permanent_flag_is_being_set_correctly
|
6
5
|
assert_equal false, HTTPRedirectTest.permanent?
|
7
|
-
|
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.
|
14
|
+
a.set_domain "a.example.com"
|
14
15
|
b = Class.new(HTTPRedirectTest)
|
15
|
-
b.
|
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
|
2
|
-
require '
|
1
|
+
require 'test_helper'
|
2
|
+
require 'net/http'
|
3
3
|
|
4
|
+
class RedirectCheckTest < MiniTest::Test
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
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
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
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
|
2
|
-
require 'http_redirect_test'
|
1
|
+
require 'test_helper'
|
3
2
|
|
4
|
-
class ResourcePathTest < Test
|
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
|
-
|
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
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
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
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
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
|
-
|
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
|
-
|
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
|
-
-
|
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
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
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:
|
104
|
+
rubygems_version: 2.2.2
|
85
105
|
signing_key:
|
86
|
-
specification_version:
|
87
|
-
summary: Test Apache behavior using Test::Unit, based on a gist by Patrick Reagan
|
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
|
-
|