rspec-webservice_matchers 1.4.3 → 1.4.4
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 61e21d1a503a63265b3ff7608ac69a75daa846b6
|
4
|
+
data.tar.gz: 89c6aa33d5e010a2835e30b1d0a99a4d830587eb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dc905ac397698db66464b220afe03b19aa704e7cf4d83d17bdd5f5cbf914ff349ffdde0f1796678f8dab7cfc6677e8b93879ca5ccd553d6fcf141f0164604503
|
7
|
+
data.tar.gz: 25380cba323ad7aac3d13b6b27533d0d0fe88b0dcd0bd83359b2c045673430a8bc088e7a1a6849628f4332d3468036e1ad4e789fafad6d1ff192f90d2760abbf
|
@@ -26,6 +26,7 @@ module RSpec
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
+
|
29
30
|
# Return true if the given page has status 200,
|
30
31
|
# and follow a few redirects if necessary.
|
31
32
|
def self.up?(url_or_domain_name)
|
@@ -36,16 +37,42 @@ module RSpec
|
|
36
37
|
end
|
37
38
|
|
38
39
|
|
40
|
+
def self.try_ssl_connection(domain_name_or_url)
|
41
|
+
# Normalize the input: remove 'http(s)://' if it's there
|
42
|
+
if %r|^https?://(.+)$| === domain_name_or_url
|
43
|
+
domain_name_or_url = $1
|
44
|
+
end
|
45
|
+
connection.head("https://#{domain_name_or_url}")
|
46
|
+
end
|
47
|
+
|
48
|
+
|
39
49
|
# RSpec Custom Matchers ###########################################
|
40
50
|
# See https://www.relishapp.com/rspec/rspec-expectations/v/2-3/docs/custom-matchers/define-matcher
|
41
51
|
|
42
52
|
# Test whether https is correctly implemented
|
43
53
|
RSpec::Matchers.define :have_a_valid_cert do
|
54
|
+
error_message = nil
|
55
|
+
|
44
56
|
match do |domain_name_or_url|
|
45
|
-
|
57
|
+
begin
|
58
|
+
RSpec::WebserviceMatchers.try_ssl_connection(domain_name_or_url)
|
59
|
+
true
|
60
|
+
rescue Exception => e
|
61
|
+
error_message = e.message
|
62
|
+
false
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
failure_message_for_should do
|
67
|
+
error_message
|
68
|
+
end
|
69
|
+
|
70
|
+
failure_message_for_should_not do
|
71
|
+
error_message
|
46
72
|
end
|
47
73
|
end
|
48
74
|
|
75
|
+
|
49
76
|
# Pass successfully if we get a 301 to the place we intend.
|
50
77
|
RSpec::Matchers.define :redirect_permanently_to do |expected|
|
51
78
|
match do |url_or_domain_name|
|
@@ -76,36 +103,47 @@ module RSpec
|
|
76
103
|
# 2. to an https url
|
77
104
|
# 3. which is correctly configured
|
78
105
|
RSpec::Matchers.define :enforce_https_everywhere do
|
79
|
-
actual_status = actual_protocol = actual_valid_cert = nil
|
106
|
+
error_msg = actual_status = actual_protocol = actual_valid_cert = nil
|
80
107
|
|
81
108
|
match do |domain_name|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
109
|
+
begin
|
110
|
+
response = RSpec::WebserviceMatchers.connection.head("http://#{domain_name}")
|
111
|
+
new_url = response.headers['location']
|
112
|
+
actual_status = response.status
|
113
|
+
if new_url =~ /^(https?)/
|
114
|
+
actual_protocol = $1
|
115
|
+
end
|
116
|
+
actual_valid_cert = RSpec::WebserviceMatchers.has_valid_ssl_cert?(new_url)
|
117
|
+
(actual_status == 301) &&
|
118
|
+
(actual_protocol == 'https') &&
|
119
|
+
(actual_valid_cert == true)
|
120
|
+
rescue Faraday::Error::ConnectionFailed => e
|
121
|
+
error_msg = "Connection failed"
|
122
|
+
false
|
88
123
|
end
|
89
|
-
actual_valid_cert = RSpec::WebserviceMatchers.has_valid_ssl_cert?(new_url)
|
90
|
-
|
91
|
-
(actual_status == 301) &&
|
92
|
-
(actual_protocol == 'https') &&
|
93
|
-
(actual_valid_cert == true)
|
94
124
|
end
|
95
125
|
|
126
|
+
|
127
|
+
# Create a compound error message listing all of the
|
128
|
+
# relevant actual values received.
|
96
129
|
failure_message_for_should do
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
130
|
+
if !error_msg.nil?
|
131
|
+
error_msg
|
132
|
+
else
|
133
|
+
mesgs = []
|
134
|
+
if actual_status != 301
|
135
|
+
mesgs << "received status #{actual_status} instead of 301"
|
136
|
+
end
|
137
|
+
if !actual_protocol.nil? && actual_protocol != 'https'
|
138
|
+
mesgs << "destination uses protocol #{actual_protocol.upcase}"
|
139
|
+
end
|
140
|
+
if ! actual_valid_cert
|
141
|
+
mesgs << "there's no valid SSL certificate"
|
142
|
+
end
|
143
|
+
mesgs.join('; ').capitalize
|
103
144
|
end
|
104
|
-
if ! actual_valid_cert
|
105
|
-
mesgs << "There's no valid SSL certificate"
|
106
|
-
end
|
107
|
-
mesgs.join('; ')
|
108
145
|
end
|
146
|
+
|
109
147
|
end
|
110
148
|
|
111
149
|
|
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'spec_helper'
|
1
2
|
require 'rspec/webservice_matchers'
|
2
3
|
|
3
4
|
# VCR may be the tool to use for this. Can it handle https? Would that work?
|
@@ -15,6 +16,18 @@ describe 'have_a_valid_cert matcher' do
|
|
15
16
|
expect('www.psu.edu').to have_a_valid_cert
|
16
17
|
}.to fail
|
17
18
|
end
|
19
|
+
|
20
|
+
it 'provides a relevant error message' do
|
21
|
+
expect {
|
22
|
+
expect('www.psu.edu').to have_a_valid_cert
|
23
|
+
}.to fail_matching(/no route to host/i)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "provides a relevant error message when the domain name doesn't exist" do
|
27
|
+
expect {
|
28
|
+
expect('sdfgkljhsdfghjkhsdfgj.edu').to have_a_valid_cert
|
29
|
+
}.to fail_matching(/not known/i)
|
30
|
+
end
|
18
31
|
end
|
19
32
|
|
20
33
|
|
@@ -29,4 +42,10 @@ describe 'enforce_https_everywhere' do
|
|
29
42
|
expect('www.psu.edu').to enforce_https_everywhere
|
30
43
|
}.to fail_matching(/200/)
|
31
44
|
end
|
45
|
+
|
46
|
+
it "provides a relevant error message when the domain name doesn't exist" do
|
47
|
+
expect {
|
48
|
+
expect('asdhfjkalsdhfjklasdfhjkasdhfl.com').to enforce_https_everywhere
|
49
|
+
}.to fail_matching(/connection failed/i)
|
50
|
+
end
|
32
51
|
end
|