rspec-webservice_matchers 4.9.0 → 4.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/HISTORY.md +4 -0
- data/lib/rspec/webservice_matchers/be_fast.rb +2 -46
- data/lib/rspec/webservice_matchers/be_status.rb +3 -2
- data/lib/rspec/webservice_matchers/be_up.rb +2 -24
- data/lib/rspec/webservice_matchers/enforce_https_everywhere.rb +5 -4
- data/lib/rspec/webservice_matchers/have_a_valid_cert.rb +3 -2
- data/lib/rspec/webservice_matchers/redirect_helpers.rb +5 -4
- data/lib/rspec/webservice_matchers/redirect_permanently_to.rb +2 -1
- data/lib/rspec/webservice_matchers/redirect_temporarily_to.rb +2 -1
- data/lib/rspec/webservice_matchers/version.rb +1 -1
- data/lib/web_test/be_fast.rb +48 -0
- data/lib/web_test/be_up.rb +25 -0
- data/lib/web_test/util.rb +95 -0
- data/rspec-webservice_matchers.gemspec +4 -3
- data/spec/rspec/webservice_matchers/page_speed_spec.rb +2 -9
- data/spec/rspec/webservice_matchers/{protcol_spec.rb → protocol_spec.rb} +3 -2
- data/spec/rspec/webservice_matchers/public_api_spec.rb +5 -4
- data/spec/web_test/be_fast_spec.rb +36 -0
- data/spec/web_test/be_up_spec.rb +93 -0
- data/spec/web_test/util_spec.rb +6 -0
- metadata +29 -11
- data/lib/rspec/webservice_matchers/util.rb +0 -97
- data/spec/rspec/webservice_matchers/be_fast_spec.rb +0 -28
- data/spec/rspec/webservice_matchers/be_up_spec.rb +0 -94
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 03bc4ee87b606016d8853436df80b164d0f4f5f4
|
4
|
+
data.tar.gz: 0429080abde8b75215e9ce6700e7ed6c84620a38
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7bdddc6c091db3c310fc7ded0457a1655d9c8cc8f2fff3fbe788c91601f74d81b35a19ff62930d36c3f4843621853fa4d693e0e6648bb219fd4346abdf9bd70e
|
7
|
+
data.tar.gz: 6e4f6b04be911e8a6f55898dcc78bc8ea8497d82978b3f9cf387cccbeb1dd447eb9ebf93565ca37db845d6902924a347227152cf6b13c600e435b5af50ce5d5f
|
data/HISTORY.md
CHANGED
@@ -1,59 +1,15 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
require 'cgi'
|
3
|
-
require 'json'
|
4
2
|
require 'rspec/matchers'
|
5
|
-
require '
|
6
|
-
require 'validated_object'
|
3
|
+
require 'web_test/be_fast'
|
7
4
|
|
8
5
|
module RSpec
|
9
6
|
module WebserviceMatchers
|
10
7
|
module BeFast
|
11
|
-
|
12
|
-
class TestResult < ValidatedObject::Base
|
13
|
-
attr_accessor :success, :score
|
14
|
-
alias success? success
|
15
|
-
|
16
|
-
validates :success, inclusion: [true, false]
|
17
|
-
validates :score, inclusion: (0..100)
|
18
|
-
end
|
19
|
-
|
20
|
-
|
21
|
-
def self.parse(json:)
|
22
|
-
response = JSON.parse(json)
|
23
|
-
unless response.key?('ruleGroups')
|
24
|
-
raise "Couldn't parse the PageSpeed response: #{response.inspect}"
|
25
|
-
end
|
26
|
-
score = response.fetch('ruleGroups').fetch('SPEED').fetch('score')
|
27
|
-
{ score: score }
|
28
|
-
end
|
29
|
-
|
30
|
-
def self.page_speed_score(url:)
|
31
|
-
url_param = CGI.escape(Util.make_url(url))
|
32
|
-
key = ENV['WEBSERVICE_MATCHER_INSIGHTS_KEY']
|
33
|
-
if key.nil?
|
34
|
-
raise 'be_fast requires the WEBSERVICE_MATCHER_INSIGHTS_KEY '\
|
35
|
-
'environment variable to be set to a Google PageSpeed '\
|
36
|
-
'Insights API key.'
|
37
|
-
end
|
38
|
-
endpoint = 'https://www.googleapis.com/pagespeedonline/v2/runPagespeed'
|
39
|
-
api_url = "#{endpoint}?url=#{url_param}&screenshot=false&key=#{key}"
|
40
|
-
response = Faraday.get(api_url)
|
41
|
-
BeFast.parse(json: response.body).fetch(:score)
|
42
|
-
end
|
43
|
-
|
44
|
-
def self.test(url:)
|
45
|
-
TestResult.new do |r|
|
46
|
-
r.score = BeFast.page_speed_score(url: url)
|
47
|
-
r.success = r.score >= 85
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
|
52
8
|
RSpec::Matchers.define :be_fast do
|
53
9
|
score = nil
|
54
10
|
|
55
11
|
match do |url|
|
56
|
-
result = BeFast.test(url: url)
|
12
|
+
result = WebTest::BeFast.test(url: url)
|
57
13
|
score = result.score
|
58
14
|
result.success?
|
59
15
|
end
|
@@ -1,5 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
require '
|
2
|
+
require 'web_test/util'
|
3
|
+
|
3
4
|
|
4
5
|
module RSpec
|
5
6
|
module WebserviceMatchers
|
@@ -10,7 +11,7 @@ module RSpec
|
|
10
11
|
actual_code = nil
|
11
12
|
|
12
13
|
match do |url_or_domain_name|
|
13
|
-
actual_code = Util.status(url_or_domain_name)
|
14
|
+
actual_code = WebTest::Util.status(url_or_domain_name)
|
14
15
|
actual_code == expected_code.to_i
|
15
16
|
end
|
16
17
|
|
@@ -1,37 +1,16 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
require 'rspec/matchers'
|
3
|
-
require '
|
4
|
-
require 'validated_object'
|
3
|
+
require 'web_test/be_up'
|
5
4
|
|
6
5
|
# Pass when the response code is 200, following redirects if necessary.
|
7
6
|
module RSpec
|
8
7
|
module WebserviceMatchers
|
9
8
|
module BeUp
|
10
|
-
|
11
|
-
class TestResult < ValidatedObject::Base
|
12
|
-
attr_accessor :success, :status_code
|
13
|
-
alias success? success
|
14
|
-
|
15
|
-
validates :success, inclusion: [true, false]
|
16
|
-
validates :status_code, inclusion: 100..510
|
17
|
-
end
|
18
|
-
|
19
|
-
|
20
|
-
def self.test(url:nil, domain:nil)
|
21
|
-
raise 'Must specify a url or domain' if url.nil? && domain.nil?
|
22
|
-
|
23
|
-
TestResult.new do |r|
|
24
|
-
r.status_code = Util.status(url || domain, follow: true)
|
25
|
-
r.success = (r.status_code == 200)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
|
30
9
|
RSpec::Matchers.define :be_up do
|
31
10
|
status = nil
|
32
11
|
|
33
12
|
match do |url_or_domain_name|
|
34
|
-
result = BeUp.test(url: url_or_domain_name)
|
13
|
+
result = WebTest::BeUp.test(url: url_or_domain_name)
|
35
14
|
status = result.status_code
|
36
15
|
result.success?
|
37
16
|
end
|
@@ -40,7 +19,6 @@ module RSpec
|
|
40
19
|
"Received status #{status}"
|
41
20
|
end
|
42
21
|
end
|
43
|
-
|
44
22
|
end
|
45
23
|
end
|
46
24
|
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
require 'faraday'
|
3
|
-
require '
|
3
|
+
require 'web_test/util'
|
4
|
+
|
4
5
|
require 'rspec/webservice_matchers/redirect_helpers'
|
5
6
|
|
6
7
|
module RSpec
|
@@ -17,7 +18,7 @@ module RSpec
|
|
17
18
|
match do |domain_name|
|
18
19
|
begin
|
19
20
|
status, new_url, final_protocol = get_info(domain_name)
|
20
|
-
meets_expectations?(status, final_protocol, Util.valid_cert?(new_url))
|
21
|
+
meets_expectations?(status, final_protocol, WebTest::Util.valid_cert?(new_url))
|
21
22
|
rescue Faraday::Error::ConnectionFailed
|
22
23
|
error_msg = 'Connection failed'
|
23
24
|
false
|
@@ -25,7 +26,7 @@ module RSpec
|
|
25
26
|
end
|
26
27
|
|
27
28
|
def get_info(domain_name)
|
28
|
-
status, headers = Util.head(domain_name)
|
29
|
+
status, headers = WebTest::Util.head(domain_name)
|
29
30
|
location = headers['location']
|
30
31
|
/^(https?)/ =~ location
|
31
32
|
protocol = Regexp.last_match(1) || nil
|
@@ -51,7 +52,7 @@ module RSpec
|
|
51
52
|
errors << "destination uses protocol #{protocol.upcase}"
|
52
53
|
end
|
53
54
|
errors << "there's no valid SSL certificate" unless cert_is_valid
|
54
|
-
Util.error_message(errors)
|
55
|
+
WebTest::Util.error_message(errors)
|
55
56
|
end
|
56
57
|
end
|
57
58
|
end
|
@@ -1,5 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
require '
|
2
|
+
require 'web_test/util'
|
3
|
+
|
3
4
|
require 'rspec/matchers'
|
4
5
|
|
5
6
|
module RSpec
|
@@ -11,7 +12,7 @@ module RSpec
|
|
11
12
|
|
12
13
|
match do |domain_name_or_url|
|
13
14
|
begin
|
14
|
-
Util.try_ssl_connection(domain_name_or_url)
|
15
|
+
WebTest::Util.try_ssl_connection(domain_name_or_url)
|
15
16
|
rescue Exception => e
|
16
17
|
error_message = fix_for_excon_bug(e.message)
|
17
18
|
false
|
@@ -1,11 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
require '
|
2
|
+
require 'web_test/util'
|
3
|
+
|
3
4
|
|
4
5
|
module RSpec
|
5
6
|
module WebserviceMatchers
|
6
7
|
module RedirectHelpers
|
7
8
|
def redirect_failure_message(exception, status, actual_location, kind)
|
8
|
-
return Util.error_message(exception) if exception
|
9
|
+
return WebTest::Util.error_message(exception) if exception
|
9
10
|
|
10
11
|
errors = []
|
11
12
|
if redirect? status
|
@@ -19,7 +20,7 @@ module RSpec
|
|
19
20
|
errors << "not a redirect: received status #{status}"
|
20
21
|
end
|
21
22
|
|
22
|
-
Util.error_message(errors)
|
23
|
+
WebTest::Util.error_message(errors)
|
23
24
|
end
|
24
25
|
|
25
26
|
def redirects_correctly?(status, actual_loc, expected_loc, kind)
|
@@ -27,7 +28,7 @@ module RSpec
|
|
27
28
|
end
|
28
29
|
|
29
30
|
def redirect_result(url_or_domain_name)
|
30
|
-
status, headers = Util.head(url_or_domain_name)
|
31
|
+
status, headers = WebTest::Util.head(url_or_domain_name)
|
31
32
|
[status, headers['location']]
|
32
33
|
end
|
33
34
|
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'cgi'
|
3
|
+
require 'json'
|
4
|
+
require 'validated_object'
|
5
|
+
require 'web_test/util'
|
6
|
+
|
7
|
+
module WebTest
|
8
|
+
module BeFast
|
9
|
+
class TestResult < ValidatedObject::Base
|
10
|
+
attr_accessor :success, :score
|
11
|
+
alias success? success
|
12
|
+
|
13
|
+
validates :success, inclusion: [true, false]
|
14
|
+
validates :score, inclusion: 0..100
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
def self.parse(json:)
|
19
|
+
response = JSON.parse(json)
|
20
|
+
unless response.key?('ruleGroups')
|
21
|
+
raise "Couldn't parse the PageSpeed response: #{response.inspect}"
|
22
|
+
end
|
23
|
+
score = response.fetch('ruleGroups').fetch('SPEED').fetch('score')
|
24
|
+
{ score: score }
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.page_speed_score(url:)
|
28
|
+
url_param = CGI.escape(WebTest::Util.make_url(url))
|
29
|
+
key = ENV['WEBSERVICE_MATCHER_INSIGHTS_KEY']
|
30
|
+
if key.nil?
|
31
|
+
raise 'be_fast requires the WEBSERVICE_MATCHER_INSIGHTS_KEY '\
|
32
|
+
'environment variable to be set to a Google PageSpeed '\
|
33
|
+
'Insights API key.'
|
34
|
+
end
|
35
|
+
endpoint = 'https://www.googleapis.com/pagespeedonline/v2/runPagespeed'
|
36
|
+
api_url = "#{endpoint}?url=#{url_param}&screenshot=false&key=#{key}"
|
37
|
+
response = Faraday.get(api_url)
|
38
|
+
BeFast.parse(json: response.body).fetch(:score)
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.test(url:)
|
42
|
+
TestResult.new do |r|
|
43
|
+
r.score = BeFast.page_speed_score(url: url)
|
44
|
+
r.success = r.score >= 85
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'validated_object'
|
3
|
+
require 'web_test/util'
|
4
|
+
|
5
|
+
module WebTest
|
6
|
+
module BeUp
|
7
|
+
class TestResult < ValidatedObject::Base
|
8
|
+
attr_accessor :success, :status_code
|
9
|
+
alias success? success
|
10
|
+
|
11
|
+
validates :success, inclusion: [true, false]
|
12
|
+
validates :status_code, inclusion: 100..510
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
def self.test(url:nil, domain:nil)
|
17
|
+
raise 'Must specify a url or domain' if url.nil? && domain.nil?
|
18
|
+
|
19
|
+
TestResult.new do |r|
|
20
|
+
r.status_code = WebTest::Util.status(url || domain, follow: true)
|
21
|
+
r.success = (r.status_code == 200)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'faraday'
|
3
|
+
require 'faraday_middleware'
|
4
|
+
|
5
|
+
TIMEOUT_IN_SECONDS = 5
|
6
|
+
OPEN_TIMEOUT_IN_SECONDS = 5
|
7
|
+
|
8
|
+
module WebTest
|
9
|
+
module Util
|
10
|
+
def self.error_message(errors)
|
11
|
+
return errors.message if errors.respond_to?(:message)
|
12
|
+
|
13
|
+
errors
|
14
|
+
.map(&:to_s)
|
15
|
+
.join('; ')
|
16
|
+
.capitalize
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.status(url_or_domain_name, follow: false)
|
20
|
+
code = head(url_or_domain_name, follow: follow)[0]
|
21
|
+
return code if code != 405
|
22
|
+
get(url_or_domain_name, follow: follow)[0]
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.head(url_or_domain_name, follow: false)
|
26
|
+
request(:head, url_or_domain_name, follow: follow)
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.get(url_or_domain_name, follow: false)
|
30
|
+
request(:get, url_or_domain_name, follow: follow)
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.request(method, url_or_domain_name, follow: false)
|
34
|
+
url = make_url(url_or_domain_name)
|
35
|
+
response = recheck_on_timeout { connection(follow: follow).send(method, url) }
|
36
|
+
[response.status, response.headers]
|
37
|
+
end
|
38
|
+
|
39
|
+
# @return true if the given page has status 200,
|
40
|
+
# and follow a few redirects if necessary.
|
41
|
+
def self.up?(url_or_domain_name)
|
42
|
+
url = make_url(url_or_domain_name)
|
43
|
+
conn = connection(follow: true)
|
44
|
+
response = recheck_on_timeout { conn.head(url) }
|
45
|
+
response.status == 200
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.valid_cert?(domain_name_or_url)
|
49
|
+
try_ssl_connection(domain_name_or_url)
|
50
|
+
true
|
51
|
+
rescue
|
52
|
+
# Not serving SSL, expired, or incorrect domain name in certificate
|
53
|
+
false
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.try_ssl_connection(domain_name_or_url)
|
57
|
+
url = "https://#{remove_protocol(domain_name_or_url)}"
|
58
|
+
recheck_on_timeout { connection.head(url) }
|
59
|
+
true
|
60
|
+
end
|
61
|
+
|
62
|
+
# private
|
63
|
+
|
64
|
+
def self.connection(follow: false)
|
65
|
+
Faraday.new do |c|
|
66
|
+
c.options[:timeout] = TIMEOUT_IN_SECONDS
|
67
|
+
c.options[:open_timeout] = OPEN_TIMEOUT_IN_SECONDS
|
68
|
+
c.use(FaradayMiddleware::FollowRedirects, limit: 4) if follow
|
69
|
+
c.adapter :net_http
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# Ensure that the given string is a URL,
|
74
|
+
# making it into one if necessary.
|
75
|
+
def self.make_url(url_or_domain_name)
|
76
|
+
if %r{^https?://} =~ url_or_domain_name
|
77
|
+
url_or_domain_name
|
78
|
+
else
|
79
|
+
"http://#{url_or_domain_name}"
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
# Normalize the input: remove 'http(s)://' if it's there
|
84
|
+
def self.remove_protocol(domain_name_or_url)
|
85
|
+
%r{^https?://(?<name>.+)$} =~ domain_name_or_url
|
86
|
+
name || domain_name_or_url
|
87
|
+
end
|
88
|
+
|
89
|
+
def self.recheck_on_timeout
|
90
|
+
yield
|
91
|
+
rescue Faraday::Error::TimeoutError
|
92
|
+
yield
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -15,8 +15,8 @@ Gem::Specification.new do |spec|
|
|
15
15
|
spec.license = 'MIT'
|
16
16
|
|
17
17
|
spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
18
|
-
spec.executables = spec.files.grep(
|
19
|
-
spec.test_files = spec.files.grep(
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
20
|
spec.require_paths = ['lib']
|
21
21
|
spec.required_ruby_version = '>= 2.1.0'
|
22
22
|
|
@@ -27,7 +27,8 @@ Gem::Specification.new do |spec|
|
|
27
27
|
spec.add_development_dependency 'rspec_junit_formatter', '0.2.2'
|
28
28
|
spec.add_development_dependency 'webmock'
|
29
29
|
|
30
|
-
spec.add_runtime_dependency 'rspec', '~> 3.0'
|
30
|
+
spec.add_runtime_dependency 'rspec-core', '~> 3.0'
|
31
|
+
spec.add_runtime_dependency 'rspec-expectations', '~> 3.0'
|
31
32
|
spec.add_runtime_dependency 'faraday'
|
32
33
|
spec.add_runtime_dependency 'faraday_middleware'
|
33
34
|
spec.add_runtime_dependency 'validated_object'
|
@@ -3,19 +3,12 @@
|
|
3
3
|
|
4
4
|
require 'spec_helper'
|
5
5
|
require 'rspec/webservice_matchers'
|
6
|
-
require '
|
6
|
+
require 'web_test/util'
|
7
|
+
|
7
8
|
|
8
9
|
SAMPLE_JSON_RESPONSE = 'spec/fixtures/pagespeed.json'
|
9
10
|
|
10
11
|
describe RSpec::WebserviceMatchers::BeFast do
|
11
|
-
describe '#parse' do
|
12
|
-
it 'can parse the overall score' do
|
13
|
-
api_response = File.read(SAMPLE_JSON_RESPONSE)
|
14
|
-
data = RSpec::WebserviceMatchers::BeFast.parse(json: api_response)
|
15
|
-
expect(data[:score]).to eq(85)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
12
|
describe '#be_fast' do
|
20
13
|
it 'performs a Google PageSpeed Insights API query on a fast site' do
|
21
14
|
expect('nonstop.qa').to be_fast
|
@@ -1,7 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
require 'spec_helper'
|
3
3
|
require 'rspec/webservice_matchers'
|
4
|
-
require '
|
4
|
+
require 'web_test/util'
|
5
|
+
|
5
6
|
|
6
7
|
describe 'be_status' do
|
7
8
|
it 'can check 200 for successful resource requests' do
|
@@ -50,7 +51,7 @@ describe 'be_up' do
|
|
50
51
|
end
|
51
52
|
|
52
53
|
it 'is available via a public API' do
|
53
|
-
status =
|
54
|
+
status = WebTest::Util.up?('http://www.website.com/')
|
54
55
|
expect(status).to be true
|
55
56
|
end
|
56
57
|
|
@@ -1,15 +1,16 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
require 'spec_helper'
|
3
|
-
require '
|
3
|
+
require 'web_test/util'
|
4
|
+
|
4
5
|
include RSpec::WebserviceMatchers
|
5
6
|
|
6
7
|
describe '#up?' do
|
7
8
|
it 'follows redirects when necessary' do
|
8
|
-
expect(Util.up?('perm-redirector.com')).to be_truthy
|
9
|
-
expect(Util.up?('temp-redirector.org')).to be_truthy
|
9
|
+
expect(WebTest::Util.up?('perm-redirector.com')).to be_truthy
|
10
|
+
expect(WebTest::Util.up?('temp-redirector.org')).to be_truthy
|
10
11
|
end
|
11
12
|
|
12
13
|
it 'retries timeout errors once' do
|
13
|
-
expect(Util.up?('http://www.timeout-once.com')).to be_truthy
|
14
|
+
expect(WebTest::Util.up?('http://www.timeout-once.com')).to be_truthy
|
14
15
|
end
|
15
16
|
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'web_test/be_fast'
|
3
|
+
|
4
|
+
RSpec.describe WebTest::BeFast do
|
5
|
+
it { is_expected.not_to be_nil }
|
6
|
+
|
7
|
+
describe '#test' do
|
8
|
+
it 'handles a fast site' do
|
9
|
+
result = WebTest::BeFast.test url: 'http://nonstop.qa'
|
10
|
+
expect(result.success?).to be true
|
11
|
+
expect(result.score).to be >= 85
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#parse' do
|
16
|
+
it 'can parse the overall score' do
|
17
|
+
api_response = File.read(SAMPLE_JSON_RESPONSE)
|
18
|
+
data = WebTest::BeFast.parse json: api_response
|
19
|
+
expect(data[:score]).to eq 85
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe WebTest::BeFast::TestResult do
|
24
|
+
it 'requires :success' do
|
25
|
+
expect do
|
26
|
+
WebTest::BeFast::TestResult.new {}
|
27
|
+
end.to raise_error(ArgumentError, /success/i)
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'requires :score' do
|
31
|
+
expect do
|
32
|
+
WebTest::BeFast::TestResult.new { |r| r.success = true }
|
33
|
+
end.to raise_error(ArgumentError, /score/i)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'web_test/be_up'
|
4
|
+
|
5
|
+
RSpec.describe WebTest::BeUp do
|
6
|
+
it { is_expected.not_to be_nil }
|
7
|
+
|
8
|
+
describe '#test' do
|
9
|
+
it 'handles a simple 200' do
|
10
|
+
result = WebTest::BeUp.test url: 'http://www.website.com/'
|
11
|
+
expect(result.success?).to be true
|
12
|
+
expect(result.status_code).to be 200
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'handles a simple 200 as a domain' do
|
16
|
+
result = WebTest::BeUp.test domain: 'www.website.com'
|
17
|
+
expect(result.success?).to be true
|
18
|
+
expect(result.status_code).to be 200
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'handles a 404' do
|
22
|
+
result = WebTest::BeUp.test url: 'http://notfound.com/no.txt'
|
23
|
+
expect(result.success?).to be false
|
24
|
+
expect(result.status_code).to be 404
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe WebTest::BeUp::TestResult do
|
29
|
+
it 'requires :success' do
|
30
|
+
expect do
|
31
|
+
WebTest::BeUp::TestResult.new { |r| r.status_code = 200 }
|
32
|
+
end.to raise_error(ArgumentError)
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'requires :status_code' do
|
36
|
+
expect do
|
37
|
+
WebTest::BeUp::TestResult.new { |r| r.success = true }
|
38
|
+
end.to raise_error(ArgumentError)
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'accepts boolean :success & integer :status_code' do
|
42
|
+
result = WebTest::BeUp::TestResult.new do |r|
|
43
|
+
r.status_code = 404
|
44
|
+
r.success = false
|
45
|
+
end
|
46
|
+
expect(result).to be_an_instance_of(WebTest::BeUp::TestResult)
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'requires boolean :success' do
|
50
|
+
expect do
|
51
|
+
WebTest::BeUp::TestResult.new do |r|
|
52
|
+
r.status_code = 200
|
53
|
+
r.success = 1
|
54
|
+
end
|
55
|
+
end.to raise_error(ArgumentError)
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'requires integer :status_code' do
|
59
|
+
expect do
|
60
|
+
WebTest::BeUp::TestResult.new do |r|
|
61
|
+
r.status_code = '404'
|
62
|
+
r.success = false
|
63
|
+
end
|
64
|
+
end.to raise_error(ArgumentError)
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'cannot have status < 100' do
|
68
|
+
expect do
|
69
|
+
WebTest::BeUp::TestResult.new do |r|
|
70
|
+
r.status_code = -5
|
71
|
+
r.success = false
|
72
|
+
end
|
73
|
+
end.to raise_error(ArgumentError)
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'cannot have status > 510' do
|
77
|
+
expect do
|
78
|
+
WebTest::BeUp::TestResult.new do |r|
|
79
|
+
r.status_code = 511
|
80
|
+
r.success = false
|
81
|
+
end
|
82
|
+
end.to raise_error(ArgumentError)
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'allows status 510' do
|
86
|
+
result = WebTest::BeUp::TestResult.new do |r|
|
87
|
+
r.status_code = 510
|
88
|
+
r.success = false
|
89
|
+
end
|
90
|
+
expect(result).to be_an_instance_of(WebTest::BeUp::TestResult)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
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: 4.
|
4
|
+
version: 4.10.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: 2016-03-
|
11
|
+
date: 2016-03-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -95,7 +95,21 @@ dependencies:
|
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
|
-
name: rspec
|
98
|
+
name: rspec-core
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '3.0'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '3.0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rspec-expectations
|
99
113
|
requirement: !ruby/object:Gem::Requirement
|
100
114
|
requirements:
|
101
115
|
- - "~>"
|
@@ -173,18 +187,21 @@ files:
|
|
173
187
|
- lib/rspec/webservice_matchers/redirect_helpers.rb
|
174
188
|
- lib/rspec/webservice_matchers/redirect_permanently_to.rb
|
175
189
|
- lib/rspec/webservice_matchers/redirect_temporarily_to.rb
|
176
|
-
- lib/rspec/webservice_matchers/util.rb
|
177
190
|
- lib/rspec/webservice_matchers/version.rb
|
191
|
+
- lib/web_test/be_fast.rb
|
192
|
+
- lib/web_test/be_up.rb
|
193
|
+
- lib/web_test/util.rb
|
178
194
|
- rspec-webservice_matchers.gemspec
|
179
195
|
- spec/fixtures/pagespeed.json
|
180
|
-
- spec/rspec/webservice_matchers/be_fast_spec.rb
|
181
|
-
- spec/rspec/webservice_matchers/be_up_spec.rb
|
182
196
|
- spec/rspec/webservice_matchers/page_speed_spec.rb
|
183
|
-
- spec/rspec/webservice_matchers/
|
197
|
+
- spec/rspec/webservice_matchers/protocol_spec.rb
|
184
198
|
- spec/rspec/webservice_matchers/public_api_spec.rb
|
185
199
|
- spec/rspec/webservice_matchers/redirect_spec.rb
|
186
200
|
- spec/rspec/webservice_matchers/ssl_spec.rb
|
187
201
|
- spec/spec_helper.rb
|
202
|
+
- spec/web_test/be_fast_spec.rb
|
203
|
+
- spec/web_test/be_up_spec.rb
|
204
|
+
- spec/web_test/util_spec.rb
|
188
205
|
homepage: https://github.com/dogweather/rspec-webservice_matchers
|
189
206
|
licenses:
|
190
207
|
- MIT
|
@@ -205,17 +222,18 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
205
222
|
version: '0'
|
206
223
|
requirements: []
|
207
224
|
rubyforge_project:
|
208
|
-
rubygems_version: 2.
|
225
|
+
rubygems_version: 2.5.1
|
209
226
|
signing_key:
|
210
227
|
specification_version: 4
|
211
228
|
summary: Black-box web app configuration testing
|
212
229
|
test_files:
|
213
230
|
- spec/fixtures/pagespeed.json
|
214
|
-
- spec/rspec/webservice_matchers/be_fast_spec.rb
|
215
|
-
- spec/rspec/webservice_matchers/be_up_spec.rb
|
216
231
|
- spec/rspec/webservice_matchers/page_speed_spec.rb
|
217
|
-
- spec/rspec/webservice_matchers/
|
232
|
+
- spec/rspec/webservice_matchers/protocol_spec.rb
|
218
233
|
- spec/rspec/webservice_matchers/public_api_spec.rb
|
219
234
|
- spec/rspec/webservice_matchers/redirect_spec.rb
|
220
235
|
- spec/rspec/webservice_matchers/ssl_spec.rb
|
221
236
|
- spec/spec_helper.rb
|
237
|
+
- spec/web_test/be_fast_spec.rb
|
238
|
+
- spec/web_test/be_up_spec.rb
|
239
|
+
- spec/web_test/util_spec.rb
|
@@ -1,97 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
require 'faraday'
|
3
|
-
require 'faraday_middleware'
|
4
|
-
|
5
|
-
TIMEOUT_IN_SECONDS = 5
|
6
|
-
OPEN_TIMEOUT_IN_SECONDS = 5
|
7
|
-
|
8
|
-
module RSpec
|
9
|
-
module WebserviceMatchers
|
10
|
-
module Util
|
11
|
-
def self.error_message(errors)
|
12
|
-
return errors.message if errors.respond_to?(:message)
|
13
|
-
|
14
|
-
errors
|
15
|
-
.map(&:to_s)
|
16
|
-
.join('; ')
|
17
|
-
.capitalize
|
18
|
-
end
|
19
|
-
|
20
|
-
def self.status(url_or_domain_name, follow: false)
|
21
|
-
code = head(url_or_domain_name, follow: follow)[0]
|
22
|
-
return code if code != 405
|
23
|
-
get(url_or_domain_name, follow: follow)[0]
|
24
|
-
end
|
25
|
-
|
26
|
-
def self.head(url_or_domain_name, follow: false)
|
27
|
-
request(:head, url_or_domain_name, follow: follow)
|
28
|
-
end
|
29
|
-
|
30
|
-
def self.get(url_or_domain_name, follow: false)
|
31
|
-
request(:get, url_or_domain_name, follow: follow)
|
32
|
-
end
|
33
|
-
|
34
|
-
def self.request(method, url_or_domain_name, follow: false)
|
35
|
-
url = make_url(url_or_domain_name)
|
36
|
-
response = recheck_on_timeout { connection(follow: follow).send(method, url) }
|
37
|
-
[response.status, response.headers]
|
38
|
-
end
|
39
|
-
|
40
|
-
# @return true if the given page has status 200,
|
41
|
-
# and follow a few redirects if necessary.
|
42
|
-
def self.up?(url_or_domain_name)
|
43
|
-
url = make_url(url_or_domain_name)
|
44
|
-
conn = connection(follow: true)
|
45
|
-
response = recheck_on_timeout { conn.head(url) }
|
46
|
-
response.status == 200
|
47
|
-
end
|
48
|
-
|
49
|
-
def self.valid_cert?(domain_name_or_url)
|
50
|
-
try_ssl_connection(domain_name_or_url)
|
51
|
-
true
|
52
|
-
rescue
|
53
|
-
# Not serving SSL, expired, or incorrect domain name in certificate
|
54
|
-
false
|
55
|
-
end
|
56
|
-
|
57
|
-
def self.try_ssl_connection(domain_name_or_url)
|
58
|
-
url = "https://#{remove_protocol(domain_name_or_url)}"
|
59
|
-
recheck_on_timeout { connection.head(url) }
|
60
|
-
true
|
61
|
-
end
|
62
|
-
|
63
|
-
# private
|
64
|
-
|
65
|
-
def self.connection(follow: false)
|
66
|
-
Faraday.new do |c|
|
67
|
-
c.options[:timeout] = TIMEOUT_IN_SECONDS
|
68
|
-
c.options[:open_timeout] = OPEN_TIMEOUT_IN_SECONDS
|
69
|
-
c.use(FaradayMiddleware::FollowRedirects, limit: 4) if follow
|
70
|
-
c.adapter :net_http
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
# Ensure that the given string is a URL,
|
75
|
-
# making it into one if necessary.
|
76
|
-
def self.make_url(url_or_domain_name)
|
77
|
-
if %r{^https?://} =~ url_or_domain_name
|
78
|
-
url_or_domain_name
|
79
|
-
else
|
80
|
-
"http://#{url_or_domain_name}"
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
# Normalize the input: remove 'http(s)://' if it's there
|
85
|
-
def self.remove_protocol(domain_name_or_url)
|
86
|
-
%r{^https?://(?<name>.+)$} =~ domain_name_or_url
|
87
|
-
name || domain_name_or_url
|
88
|
-
end
|
89
|
-
|
90
|
-
def self.recheck_on_timeout
|
91
|
-
yield
|
92
|
-
rescue Faraday::Error::TimeoutError
|
93
|
-
yield
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
require 'spec_helper'
|
3
|
-
require 'rspec/webservice_matchers/be_fast'
|
4
|
-
include RSpec::WebserviceMatchers
|
5
|
-
|
6
|
-
describe BeFast do
|
7
|
-
describe BeFast::TestResult do
|
8
|
-
it 'requires :success' do
|
9
|
-
expect {
|
10
|
-
BeFast::TestResult.new {}
|
11
|
-
}.to raise_error(ArgumentError, /success/i)
|
12
|
-
end
|
13
|
-
|
14
|
-
it 'requires :score' do
|
15
|
-
expect {
|
16
|
-
BeFast::TestResult.new { |r| r.success = true }
|
17
|
-
}.to raise_error(ArgumentError, /score/i)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
describe '#test' do
|
22
|
-
it 'handles a fast site' do
|
23
|
-
result = BeFast.test url: 'http://nonstop.qa'
|
24
|
-
expect( result.success? ).to be true
|
25
|
-
expect( result.score ).to be >= 85
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
@@ -1,94 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
require 'spec_helper'
|
3
|
-
require 'rspec/webservice_matchers/be_up'
|
4
|
-
|
5
|
-
include RSpec::WebserviceMatchers
|
6
|
-
|
7
|
-
describe BeUp do
|
8
|
-
describe '#test' do
|
9
|
-
it 'handles a simple 200' do
|
10
|
-
result = BeUp.test url: 'http://www.website.com/'
|
11
|
-
expect( result.success? ).to be true
|
12
|
-
expect( result.status_code ).to be 200
|
13
|
-
end
|
14
|
-
|
15
|
-
it 'handles a simple 200 as a domain' do
|
16
|
-
result = BeUp.test domain: 'www.website.com'
|
17
|
-
expect( result.success? ).to be true
|
18
|
-
expect( result.status_code ).to be 200
|
19
|
-
end
|
20
|
-
|
21
|
-
it 'handles a 404' do
|
22
|
-
result = BeUp.test url: 'http://notfound.com/no.txt'
|
23
|
-
expect( result.success? ).to be false
|
24
|
-
expect( result.status_code ).to be 404
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
|
29
|
-
describe BeUp::TestResult do
|
30
|
-
it 'requires :success' do
|
31
|
-
expect {
|
32
|
-
BeUp::TestResult.new { |r| r.status_code = 200 }
|
33
|
-
}.to raise_error(ArgumentError)
|
34
|
-
end
|
35
|
-
|
36
|
-
it 'requires :status_code' do
|
37
|
-
expect {
|
38
|
-
BeUp::TestResult.new { |r| r.success = true }
|
39
|
-
}.to raise_error(ArgumentError)
|
40
|
-
end
|
41
|
-
|
42
|
-
it 'accepts boolean :success & integer :status_code' do
|
43
|
-
result = BeUp::TestResult.new do |r|
|
44
|
-
r.status_code = 404
|
45
|
-
r.success = false
|
46
|
-
end
|
47
|
-
expect( result ).to be_an_instance_of(BeUp::TestResult)
|
48
|
-
end
|
49
|
-
|
50
|
-
it 'requires boolean :success' do
|
51
|
-
expect {
|
52
|
-
BeUp::TestResult.new do |r|
|
53
|
-
r.status_code = 200
|
54
|
-
r.success = 1
|
55
|
-
end
|
56
|
-
}.to raise_error(ArgumentError)
|
57
|
-
end
|
58
|
-
|
59
|
-
it 'requires integer :status_code' do
|
60
|
-
expect {
|
61
|
-
BeUp::TestResult.new do |r|
|
62
|
-
r.status_code = '404'
|
63
|
-
r.success = false
|
64
|
-
end
|
65
|
-
}.to raise_error(ArgumentError)
|
66
|
-
end
|
67
|
-
|
68
|
-
it 'cannot have status < 100' do
|
69
|
-
expect {
|
70
|
-
BeUp::TestResult.new do |r|
|
71
|
-
r.status_code = -5
|
72
|
-
r.success = false
|
73
|
-
end
|
74
|
-
}.to raise_error(ArgumentError)
|
75
|
-
end
|
76
|
-
|
77
|
-
it 'cannot have status > 510' do
|
78
|
-
expect {
|
79
|
-
BeUp::TestResult.new do |r|
|
80
|
-
r.status_code = 511
|
81
|
-
r.success = false
|
82
|
-
end
|
83
|
-
}.to raise_error(ArgumentError)
|
84
|
-
end
|
85
|
-
|
86
|
-
it 'allows status 510' do
|
87
|
-
result = BeUp::TestResult.new do |r|
|
88
|
-
r.status_code = 510
|
89
|
-
r.success = false
|
90
|
-
end
|
91
|
-
expect( result ).to be_an_instance_of(BeUp::TestResult)
|
92
|
-
end
|
93
|
-
end
|
94
|
-
end
|