rspec-webservice_matchers 1.4.6 → 2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 301bff8ff03e8860581b9c5b384b1ad69defd7f3
4
- data.tar.gz: 99bf6e38a2c4e81d6ed31d209ab62f15a662bd4b
3
+ metadata.gz: 5f010c192b642b65ee3b60e1e8fa6d60aa4166c8
4
+ data.tar.gz: 0363d1f7fbe3032c6268964b4f23f845e79996e8
5
5
  SHA512:
6
- metadata.gz: 05411b388dbe46901b03bf312ce54d3aed2fb105856c87118099be5c5fd7836b8e1b909772b7400695f0d9ca4d6fdb043c395cf7e5e6f3f96e30ce8cce9acc66
7
- data.tar.gz: a3cd931442454b0cc94fc933ef0af07edb9cc1edfdb66a38b1cfb39f43861f7a56e0706522538f1a0c3cdac133b35b85573079a510faf2d102464fd05e3ee31b
6
+ metadata.gz: 3d7e514a44a4fef365fdd968fdc66a72cb15779305cc12d152069d8ef08e727d200ad8f410ecae8f9ad16fd925fa31080b5ab4ce70da3abbdbf85c4adb09dc8f
7
+ data.tar.gz: b93ac4024054368135ff8d6577165c9ba9e9c248c6baf223515069a56aba7814e7304e4f826258e936925cc5c441edc3d61e38ffd76f67be51a3b60a86b24b2d
@@ -1,61 +1,23 @@
1
1
  require 'rspec/webservice_matchers/version'
2
2
  require 'faraday'
3
3
  require 'faraday_middleware'
4
+ require 'pry'
4
5
 
5
6
  # Seconds
6
- TIMEOUT = 5
7
+ TIMEOUT = 5
7
8
  OPEN_TIMEOUT = 2
8
9
 
9
-
10
10
  module RSpec
11
+ # RSpec Custom Matchers
12
+ # See https://www.relishapp.com/rspec/rspec-expectations/v/2-3/docs/custom-matchers/define-matcher
11
13
  module WebserviceMatchers
12
-
13
- def self.has_valid_ssl_cert?(domain_name_or_url)
14
- # Normalize the input: remove 'http(s)://' if it's there
15
- if %r|^https?://(.+)$| === domain_name_or_url
16
- domain_name_or_url = $1
17
- end
18
-
19
- # Test by seeing if Curl retrieves without complaining
20
- begin
21
- connection.head("https://#{domain_name_or_url}")
22
- return true
23
- rescue
24
- # Not serving SSL, expired, or incorrect domain name in certificate
25
- return false
26
- end
27
- end
28
-
29
-
30
- # Return true if the given page has status 200,
31
- # and follow a few redirects if necessary.
32
- def self.up?(url_or_domain_name)
33
- url = RSpec::WebserviceMatchers.make_url(url_or_domain_name)
34
- conn = RSpec::WebserviceMatchers.connection(follow: true)
35
- response = conn.head(url)
36
- response.status == 200
37
- end
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
-
49
- # RSpec Custom Matchers ###########################################
50
- # See https://www.relishapp.com/rspec/rspec-expectations/v/2-3/docs/custom-matchers/define-matcher
51
-
52
14
  # Test whether https is correctly implemented
53
15
  RSpec::Matchers.define :have_a_valid_cert do
54
16
  error_message = nil
55
17
 
56
18
  match do |domain_name_or_url|
57
19
  begin
58
- RSpec::WebserviceMatchers.try_ssl_connection(domain_name_or_url)
20
+ WebserviceMatchers.try_ssl_connection(domain_name_or_url)
59
21
  true
60
22
  rescue Exception => e
61
23
  error_message = e.message
@@ -68,37 +30,36 @@ module RSpec
68
30
  end
69
31
  end
70
32
 
71
-
72
33
  # Pass successfully if we get a 301 to the place we intend.
73
34
  RSpec::Matchers.define :redirect_permanently_to do |expected|
74
35
  error_message = actual_status = actual_location = nil
75
36
 
76
37
  match do |url_or_domain_name|
77
38
  begin
78
- response = RSpec::WebserviceMatchers.connection.head(RSpec::WebserviceMatchers.make_url url_or_domain_name)
79
- expected = RSpec::WebserviceMatchers.make_url(expected)
39
+ response = WebserviceMatchers.connection.head(WebserviceMatchers.make_url url_or_domain_name)
40
+ expected = WebserviceMatchers.make_url(expected)
80
41
  actual_location = response.headers['location']
81
42
  actual_status = response.status
82
43
 
83
- (actual_status == 301) && (%r|#{expected}/?| === actual_location)
44
+ (actual_status == 301) && (/#{expected}\/?/.match(actual_location))
84
45
  rescue Exception => e
85
46
  error_message = e.message
86
- false
47
+ false
87
48
  end
88
49
  end
89
50
 
90
51
  failure_message_for_should do
91
- if ! error_message.nil?
52
+ if !error_message.nil?
92
53
  error_message
93
54
  else
94
55
  mesgs = []
95
56
  if [302, 307].include? actual_status
96
57
  mesgs << "received a temporary redirect, status #{actual_status}"
97
58
  end
98
- if ! actual_location.nil? && ! (%r|#{expected}/?| === actual_location)
59
+ if !actual_location.nil? && ! (%r|#{expected}/?| === actual_location)
99
60
  mesgs << "received location #{actual_location}"
100
61
  end
101
- if ! [301, 302, 307].include? actual_status
62
+ if ![301, 302, 307].include? actual_status
102
63
  mesgs << "not a redirect: received status #{actual_status}"
103
64
  end
104
65
  mesgs.join('; ').capitalize
@@ -106,37 +67,37 @@ module RSpec
106
67
  end
107
68
  end
108
69
 
109
-
110
70
  # Pass successfully if we get a 302 or 307 to the place we intend.
111
71
  RSpec::Matchers.define :redirect_temporarily_to do |expected|
72
+ include RSpec
112
73
  error_message = actual_status = actual_location = nil
113
74
 
114
75
  match do |url_or_domain_name|
115
76
  begin
116
- response = RSpec::WebserviceMatchers.connection.head(RSpec::WebserviceMatchers.make_url url_or_domain_name)
117
- expected = RSpec::WebserviceMatchers.make_url(expected)
77
+ response = WebserviceMatchers.connection.head(WebserviceMatchers.make_url url_or_domain_name)
78
+ expected = WebserviceMatchers.make_url(expected)
118
79
  actual_location = response.headers['location']
119
80
  actual_status = response.status
120
81
 
121
- [302, 307].include?(actual_status) && (%r|#{expected}/?| === actual_location)
82
+ [302, 307].include?(actual_status) && (/#{expected}\/?/ =~ actual_location)
122
83
  rescue Exception => e
123
84
  error_message = e.message
124
- false
85
+ false
125
86
  end
126
87
  end
127
88
 
128
89
  failure_message_for_should do
129
- if ! error_message.nil?
90
+ if !error_message.nil?
130
91
  error_message
131
92
  else
132
93
  mesgs = []
133
94
  if actual_status == 301
134
95
  mesgs << "received a permanent redirect, status #{actual_status}"
135
96
  end
136
- if ! actual_location.nil? && ! (%r|#{expected}/?| === actual_location)
97
+ if !actual_location.nil? && ! (%r|#{expected}/?| === actual_location)
137
98
  mesgs << "received location #{actual_location}"
138
99
  end
139
- if ! [301, 302, 307].include? actual_status
100
+ if ![301, 302, 307].include? actual_status
140
101
  mesgs << "not a redirect: received status #{actual_status}"
141
102
  end
142
103
  mesgs.join('; ').capitalize
@@ -144,7 +105,6 @@ module RSpec
144
105
  end
145
106
  end
146
107
 
147
-
148
108
  # This is a high level matcher which checks three things:
149
109
  # 1. Permanent redirect
150
110
  # 2. to an https url
@@ -154,18 +114,17 @@ module RSpec
154
114
 
155
115
  match do |domain_name|
156
116
  begin
157
- response = RSpec::WebserviceMatchers.connection.head("http://#{domain_name}")
117
+ response = WebserviceMatchers.connection.head("http://#{domain_name}")
158
118
  new_url = response.headers['location']
159
119
  actual_status = response.status
160
- if new_url =~ /^(https?)/
161
- actual_protocol = $1
162
- end
163
- actual_valid_cert = RSpec::WebserviceMatchers.has_valid_ssl_cert?(new_url)
120
+ /^(?<protocol>https?)/ =~ new_url
121
+ actual_protocol = protocol || nil
122
+ actual_valid_cert = WebserviceMatchers.valid_ssl_cert?(new_url)
164
123
  (actual_status == 301) &&
165
124
  (actual_protocol == 'https') &&
166
125
  (actual_valid_cert == true)
167
- rescue Faraday::Error::ConnectionFailed => e
168
- error_msg = "Connection failed"
126
+ rescue Faraday::Error::ConnectionFailed
127
+ error_msg = 'Connection failed'
169
128
  false
170
129
  end
171
130
  end
@@ -183,7 +142,7 @@ module RSpec
183
142
  if !actual_protocol.nil? && actual_protocol != 'https'
184
143
  mesgs << "destination uses protocol #{actual_protocol.upcase}"
185
144
  end
186
- if ! actual_valid_cert
145
+ if !actual_valid_cert
187
146
  mesgs << "there's no valid SSL certificate"
188
147
  end
189
148
  mesgs.join('; ').capitalize
@@ -192,18 +151,17 @@ module RSpec
192
151
 
193
152
  end
194
153
 
195
-
196
154
  # Pass when a URL returns the expected status code
197
155
  # Codes are defined in http://www.rfc-editor.org/rfc/rfc2616.txt
198
- RSpec::Matchers.define :be_status do |expected|
156
+ RSpec::Matchers.define :be_status do |expected_code|
199
157
  actual_code = nil
200
-
158
+
201
159
  match do |url_or_domain_name|
202
- url = RSpec::WebserviceMatchers.make_url(url_or_domain_name)
203
- response = RSpec::WebserviceMatchers.connection.head(url)
204
- actual_code = response.status
205
- expected = expected.to_i
206
- actual_code == expected
160
+ url = WebserviceMatchers.make_url(url_or_domain_name)
161
+ response = WebserviceMatchers.connection.head(url)
162
+ actual_code = response.status
163
+ expected_code = expected_code.to_i
164
+ actual_code == expected_code
207
165
  end
208
166
 
209
167
  failure_message_for_should do
@@ -211,15 +169,14 @@ module RSpec
211
169
  end
212
170
  end
213
171
 
214
-
215
172
  # Pass when the response code is 200, following redirects
216
173
  # if necessary.
217
174
  RSpec::Matchers.define :be_up do
218
175
  actual_status = nil
219
176
 
220
177
  match do |url_or_domain_name|
221
- url = RSpec::WebserviceMatchers.make_url(url_or_domain_name)
222
- conn = RSpec::WebserviceMatchers.connection(follow: true)
178
+ url = WebserviceMatchers.make_url(url_or_domain_name)
179
+ conn = WebserviceMatchers.connection(follow: true)
223
180
  response = conn.head(url)
224
181
  actual_status = response.status
225
182
  actual_status == 200
@@ -230,6 +187,26 @@ module RSpec
230
187
  end
231
188
  end
232
189
 
190
+ # Return true if the given page has status 200,
191
+ # and follow a few redirects if necessary.
192
+ def self.up?(url_or_domain_name)
193
+ url = make_url(url_or_domain_name)
194
+ conn = connection(follow: true)
195
+ response = conn.head(url)
196
+ response.status == 200
197
+ end
198
+
199
+ def self.valid_ssl_cert?(domain_name_or_url)
200
+ try_ssl_connection(domain_name_or_url)
201
+ true
202
+ rescue
203
+ # Not serving SSL, expired, or incorrect domain name in certificate
204
+ false
205
+ end
206
+
207
+ def self.try_ssl_connection(domain_name_or_url)
208
+ connection.head("https://#{remove_protocol(domain_name_or_url)}")
209
+ end
233
210
 
234
211
  private
235
212
 
@@ -237,22 +214,25 @@ module RSpec
237
214
  Faraday.new do |c|
238
215
  c.options[:timeout] = TIMEOUT
239
216
  c.options[:open_timeout] = TIMEOUT
240
- if follow
241
- c.use FaradayMiddleware::FollowRedirects, limit: 4
242
- end
217
+ c.use(FaradayMiddleware::FollowRedirects, limit: 4) if follow
243
218
  c.adapter :net_http
244
- end
219
+ end
245
220
  end
246
221
 
247
222
  # Ensure that the given string is a URL,
248
223
  # making it into one if necessary.
249
224
  def self.make_url(url_or_domain_name)
250
- unless %r|^https?://| === url_or_domain_name
251
- "http://#{url_or_domain_name}"
252
- else
225
+ if %r{^https?://} =~ url_or_domain_name
253
226
  url_or_domain_name
227
+ else
228
+ "http://#{url_or_domain_name}"
254
229
  end
255
230
  end
256
231
 
232
+ # Normalize the input: remove 'http(s)://' if it's there
233
+ def self.remove_protocol(domain_name_or_url)
234
+ %r{^https?://(?<name>.+)$} =~ domain_name_or_url
235
+ name || domain_name_or_url
236
+ end
257
237
  end
258
238
  end
@@ -1,5 +1,5 @@
1
1
  module RSpec
2
2
  module WebserviceMatchers
3
- VERSION = "1.4.6"
3
+ VERSION = '2.0'
4
4
  end
5
5
  end
@@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "rake"
23
23
  spec.add_development_dependency "pry"
24
24
  spec.add_development_dependency 'webmock'
25
+ spec.add_development_dependency "codeclimate-test-reporter"
25
26
 
26
27
  spec.add_runtime_dependency 'rspec', '~> 2'
27
28
  spec.add_runtime_dependency 'faraday'
@@ -1,31 +1,28 @@
1
1
  require 'spec_helper'
2
2
  require 'rspec/webservice_matchers'
3
3
 
4
- # VCR may be the tool to use for this. Can it handle https? Would that work?
5
-
6
-
7
4
  describe 'have_a_valid_cert matcher' do
8
5
  it 'passes when SSL is properly configured' do
9
6
  # EFF created the HTTPS Everywhere movement
10
7
  # TODO: set up a test server for this. (?)
11
8
  expect('www.eff.org').to have_a_valid_cert
12
- end
9
+ end
13
10
 
14
11
  it 'fails if the server is not serving SSL at all' do
15
12
  expect {
16
- expect('www.psu.edu').to have_a_valid_cert
13
+ expect('www.psu.edu').to have_a_valid_cert
17
14
  }.to fail
18
15
  end
19
16
 
20
17
  it 'provides a relevant error message' do
21
18
  expect {
22
- expect('www.psu.edu').to have_a_valid_cert
23
- }.to fail_matching(/no route to host/i)
19
+ expect('www.psu.edu').to have_a_valid_cert
20
+ }.to fail_matching(/(no route to host)|(connection refused)/i)
24
21
  end
25
22
 
26
23
  it "provides a relevant error message when the domain name doesn't exist" do
27
24
  expect {
28
- expect('sdfgkljhsdfghjkhsdfgj.edu').to have_a_valid_cert
25
+ expect('sdfgkljhsdfghjkhsdfgj.edu').to have_a_valid_cert
29
26
  }.to fail_matching(/not known/i)
30
27
  end
31
28
 
@@ -36,7 +33,6 @@ describe 'have_a_valid_cert matcher' do
36
33
  end
37
34
  end
38
35
 
39
-
40
36
  # See https://www.eff.org/https-everywhere
41
37
  describe 'enforce_https_everywhere' do
42
38
  it 'passes when http requests are redirected to valid https urls' do
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,6 @@
1
+ require "codeclimate-test-reporter"
2
+ CodeClimate::TestReporter.start
3
+
1
4
  require 'webmock/rspec'
2
5
 
3
6
  RSpec.configure do |config|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-webservice_matchers
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.6
4
+ version: '2.0'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robb Shecter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-18 00:00:00.000000000 Z
11
+ date: 2014-03-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: codeclimate-test-reporter
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: rspec
71
85
  requirement: !ruby/object:Gem::Requirement