rspec-webservice_matchers 4.9.0 → 4.12.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/ruby.yml +35 -0
- data/HISTORY.md +13 -0
- data/README.md +11 -15
- data/circle.yml +2 -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 +7 -6
- 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 +2 -1
- data/lib/web_test/be_fast.rb +59 -0
- data/lib/web_test/be_up.rb +25 -0
- data/lib/web_test/util.rb +105 -0
- data/rspec-webservice_matchers.gemspec +10 -9
- data/spec/failure_matchers.rb +16 -0
- data/spec/rspec/webservice_matchers/{protcol_spec.rb → protocol_spec.rb} +5 -8
- data/spec/rspec/webservice_matchers/public_api_spec.rb +6 -5
- data/spec/rspec/webservice_matchers/redirect_spec.rb +19 -18
- data/spec/rspec/webservice_matchers/ssl_spec.rb +72 -47
- data/spec/spec_helper.rb +3 -54
- data/spec/web_mock_config.rb +54 -0
- data/spec/web_test/be_up_spec.rb +93 -0
- data/spec/web_test/util_spec.rb +6 -0
- metadata +53 -35
- 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
- data/spec/rspec/webservice_matchers/page_speed_spec.rb +0 -37
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 617b8cb74f6bc84677c5ead97e558a6b6b9755f9044bcaeccfb3f63cb986b1b9
|
4
|
+
data.tar.gz: 4309003b1b46bf0b06ad2990d984d137c0cfd72028ccacd2e871337a713d347b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5f6253513b26f6bf0adaf28934961912fca38756ddbcbc1cf4e16f70bfdf5c5511a14a353a2989c51a9ef08366fda27a1ef8ea831c9d44e37173363979bdeb87
|
7
|
+
data.tar.gz: 1e03ec8f1aa261bba4dad5a82222eb9a5c224336ead102478f3831068e501c50b5c13bfb628233415d3dc47f9258e729d4c8b196108529561ab692cda9f16efd
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# This workflow uses actions that are not certified by GitHub.
|
2
|
+
# They are provided by a third-party and are governed by
|
3
|
+
# separate terms of service, privacy policy, and support
|
4
|
+
# documentation.
|
5
|
+
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
|
6
|
+
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
|
7
|
+
|
8
|
+
name: Ruby
|
9
|
+
|
10
|
+
on:
|
11
|
+
push:
|
12
|
+
branches: [ master ]
|
13
|
+
pull_request:
|
14
|
+
branches: [ master ]
|
15
|
+
|
16
|
+
jobs:
|
17
|
+
test:
|
18
|
+
|
19
|
+
runs-on: ubuntu-latest
|
20
|
+
strategy:
|
21
|
+
matrix:
|
22
|
+
ruby-version: ['2.4', '2.5', '2.6', '2.7']
|
23
|
+
|
24
|
+
steps:
|
25
|
+
- uses: actions/checkout@v2
|
26
|
+
- name: Set up Ruby
|
27
|
+
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
|
28
|
+
# change this to (see https://github.com/ruby/setup-ruby#versioning):
|
29
|
+
# uses: ruby/setup-ruby@v1
|
30
|
+
uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
|
31
|
+
with:
|
32
|
+
ruby-version: ${{ matrix.ruby-version }}
|
33
|
+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
34
|
+
- name: Run tests
|
35
|
+
run: bundle exec rake
|
data/HISTORY.md
CHANGED
@@ -1,3 +1,16 @@
|
|
1
|
+
4.12.0
|
2
|
+
------
|
3
|
+
Refactoring.
|
4
|
+
Change the Be fast test result for response to be the raw response.
|
5
|
+
|
6
|
+
4.11.0
|
7
|
+
------
|
8
|
+
Adds the full PageSpeed result to the `BeFast::TestResult`.
|
9
|
+
|
10
|
+
4.10.0
|
11
|
+
------
|
12
|
+
Refactoring lower-level code into new namespace.
|
13
|
+
|
1
14
|
4.9.0
|
2
15
|
-----
|
3
16
|
Adds new API for `be_fast`.
|
data/README.md
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
# RSpec::WebserviceMatchers
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
![Ruby](https://github.com/dogweather/rspec-webservice_matchers/workflows/Ruby/badge.svg?branch=master) [![Maintainability](https://api.codeclimate.com/v1/badges/501f791f1ea83905e992/maintainability)](https://codeclimate.com/github/dogweather/rspec-webservice_matchers/maintainability) [![Gem Version](https://badge.fury.io/rb/rspec-webservice_matchers.svg)](https://badge.fury.io/rb/rspec-webservice_matchers)
|
5
4
|
|
6
5
|
A [gem](https://rubygems.org/gems/rspec-webservice_matchers) to black-box test a web server configuration. For example, whether a site's SSL certificate is correctly configured and not expired:
|
7
6
|
|
@@ -9,13 +8,10 @@ A [gem](https://rubygems.org/gems/rspec-webservice_matchers) to black-box test a
|
|
9
8
|
expect('github.com').to have_a_valid_cert
|
10
9
|
```
|
11
10
|
|
12
|
-
It's a tool for doing **Test Driven Devops** (I just made that up). See [
|
11
|
+
It's a tool for doing **Test Driven Devops** (I just made that up). See [my introductory blog post](https://dogsnog.blog/2014/01/16/new-open-source-library-for-test-driven-devops/) for the backstory.
|
13
12
|
|
14
13
|
This library takes a minimalist approach: it simply adds new RSpec matchers. Therefore, you can use your own RSpec writing style; there's no new DSL to learn.
|
15
14
|
|
16
|
-
If you're too busy to install this and code the tests by hand, you can try the [new hosted service which runs these RSpec matchers in the cloud](http://nonstop.qa): [![QA Status](http://nonstop.qa/projects/20/badges/default.svg)](http://nonstop.qa/projects/20-nonstop-qa)
|
17
|
-
|
18
|
-
|
19
15
|
|
20
16
|
|
21
17
|
Installation
|
@@ -28,15 +24,15 @@ What You Get
|
|
28
24
|
------------
|
29
25
|
These new RSpec matchers:
|
30
26
|
|
31
|
-
| Notes
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
27
|
+
| | Notes
|
28
|
+
|-------------------------------|------------------------------------------------
|
29
|
+
|**be_up** | Looks for a 200, but will follow up to four redirects
|
30
|
+
|**be_fast** | Checks for Google [PageSpeed](https://developers.google.com/speed/pagespeed/insights/) score >= 85. Expects the environment variable `WEBSERVICE_MATCHER_INSIGHTS_KEY` to contain a [Google "server" API key](https://developers.google.com/speed/docs/insights/v2/getting-started) with PageSpeed Insights API enabled.
|
31
|
+
|**enforce_https_everywhere** | Passes if the site will _only_ allow SSL connections. See the [EFF project, HTTPS Everywhere](https://www.eff.org/https-everywhere)
|
32
|
+
|**have_a_valid_cert** | Will fail if there's no cert, or it's expired or incorrectly configured
|
33
|
+
|**be_status** | A low-level matcher to explicitly check for a 200, 503, or any other code
|
34
|
+
|**redirect_permanently_to** | Checks for 301 and a correct destination URL
|
35
|
+
|**redirect_temporarily_to** | Checks for 302 or 307 and a correct destination
|
40
36
|
|
41
37
|
|
42
38
|
Example
|
data/circle.yml
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
|
@@ -14,10 +15,10 @@ module RSpec
|
|
14
15
|
include RedirectHelpers
|
15
16
|
error_msg = status = final_protocol = has_valid_cert = nil
|
16
17
|
|
17
|
-
match do |
|
18
|
+
match do |domain_name_or_url|
|
18
19
|
begin
|
19
|
-
status, new_url, final_protocol = get_info(
|
20
|
-
meets_expectations?(status, final_protocol, Util.valid_cert?(new_url))
|
20
|
+
status, new_url, final_protocol = get_info(WebTest::Util.make_domain_name(domain_name_or_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,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'cgi'
|
3
|
+
require 'json'
|
4
|
+
require 'validated_object'
|
5
|
+
require 'web_test/util'
|
6
|
+
|
7
|
+
#
|
8
|
+
# Runs PageSpeed on a URL.
|
9
|
+
# See https://developers.google.com/speed/docs/insights/v2/reference/pagespeedapi/runpagespeed#response
|
10
|
+
#
|
11
|
+
module WebTest
|
12
|
+
module BeFast
|
13
|
+
|
14
|
+
class TestResult < ValidatedObject::Base
|
15
|
+
attr_accessor :success, :score, :response
|
16
|
+
alias success? success
|
17
|
+
|
18
|
+
validates :success, type: Boolean
|
19
|
+
validates :score, inclusion: 0..100
|
20
|
+
validates :response, type: Hash
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
def self.test(url:)
|
25
|
+
response = page_speed(url: url)
|
26
|
+
|
27
|
+
TestResult.new do |r|
|
28
|
+
r.score = response.fetch(:score)
|
29
|
+
r.success = r.score >= 85
|
30
|
+
r.response = response[:raw_response]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.page_speed(url:)
|
35
|
+
url_param = CGI.escape(WebTest::Util.make_url(url))
|
36
|
+
key = ENV['WEBSERVICE_MATCHER_INSIGHTS_KEY']
|
37
|
+
if key.nil?
|
38
|
+
raise 'be_fast requires the WEBSERVICE_MATCHER_INSIGHTS_KEY '\
|
39
|
+
'environment variable to be set to a Google PageSpeed '\
|
40
|
+
'Insights API key.'
|
41
|
+
end
|
42
|
+
endpoint = 'https://www.googleapis.com/pagespeedonline/v2/runPagespeed'
|
43
|
+
api_url = "#{endpoint}?url=#{url_param}&screenshot=false&key=#{key}"
|
44
|
+
parse json: Faraday.get(api_url).body
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.parse(json:)
|
48
|
+
raw_response = JSON.parse(json)
|
49
|
+
unless raw_response.key?('ruleGroups')
|
50
|
+
raise "Couldn't parse the PageSpeed raw_response: #{raw_response.inspect}"
|
51
|
+
end
|
52
|
+
score = raw_response.fetch('ruleGroups').fetch('SPEED').fetch('score')
|
53
|
+
{
|
54
|
+
score: score,
|
55
|
+
raw_response: raw_response
|
56
|
+
}
|
57
|
+
end
|
58
|
+
end
|
59
|
+
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, type: Boolean
|
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
|