rspec-webservice_matchers 4.10.0 → 4.11.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/version.rb +1 -1
- data/lib/web_test/be_fast.rb +27 -16
- data/spec/failure_matchers.rb +16 -0
- data/spec/rspec/webservice_matchers/page_speed_spec.rb +0 -3
- data/spec/rspec/webservice_matchers/protocol_spec.rb +0 -4
- data/spec/rspec/webservice_matchers/redirect_spec.rb +2 -2
- data/spec/rspec/webservice_matchers/ssl_spec.rb +52 -47
- data/spec/spec_helper.rb +3 -54
- data/spec/web_mock_config.rb +54 -0
- data/spec/web_test/be_fast_spec.rb +11 -1
- metadata +7 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3a087dde9dd0b72fcfc3677cea0c7c3b7e706404
|
4
|
+
data.tar.gz: 30488dffd2af4fbe03a7d1906c97601506732a23
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: abe119853f955ad508086e23ffe2df8761591816bc8ee05b42972b549e5a895fc8506310080d3e422f5ee338ec114091b510efb5643cecaea00edffe22d624e0
|
7
|
+
data.tar.gz: 1ec4dff0037ef5a129e18edecfd198264bd2f195b668f5a6cd5a7b0bfb32afe138748f232d1d786a4a2dd3f699e3de3ed292b0362b0d9e5fb3677ad7bfc47f68
|
data/HISTORY.md
CHANGED
data/lib/web_test/be_fast.rb
CHANGED
@@ -4,27 +4,34 @@ require 'json'
|
|
4
4
|
require 'validated_object'
|
5
5
|
require 'web_test/util'
|
6
6
|
|
7
|
+
#
|
8
|
+
# Runs PageSpeed on a URL.
|
9
|
+
# See https://developers.google.com/speed/docs/insights/v2/reference/pagespeedapi/runpagespeed#response
|
10
|
+
#
|
7
11
|
module WebTest
|
8
12
|
module BeFast
|
13
|
+
|
9
14
|
class TestResult < ValidatedObject::Base
|
10
|
-
attr_accessor :success, :score
|
15
|
+
attr_accessor :success, :score, :response
|
11
16
|
alias success? success
|
12
17
|
|
13
|
-
validates :success,
|
14
|
-
validates :score,
|
18
|
+
validates :success, inclusion: [true, false]
|
19
|
+
validates :score, inclusion: 0..100
|
20
|
+
validates :response, type: Hash
|
15
21
|
end
|
16
22
|
|
17
23
|
|
18
|
-
def self.
|
19
|
-
response =
|
20
|
-
|
21
|
-
|
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
|
22
31
|
end
|
23
|
-
score = response.fetch('ruleGroups').fetch('SPEED').fetch('score')
|
24
|
-
{ score: score }
|
25
32
|
end
|
26
33
|
|
27
|
-
def self.
|
34
|
+
def self.page_speed(url:)
|
28
35
|
url_param = CGI.escape(WebTest::Util.make_url(url))
|
29
36
|
key = ENV['WEBSERVICE_MATCHER_INSIGHTS_KEY']
|
30
37
|
if key.nil?
|
@@ -34,15 +41,19 @@ module WebTest
|
|
34
41
|
end
|
35
42
|
endpoint = 'https://www.googleapis.com/pagespeedonline/v2/runPagespeed'
|
36
43
|
api_url = "#{endpoint}?url=#{url_param}&screenshot=false&key=#{key}"
|
37
|
-
|
38
|
-
BeFast.parse(json: response.body).fetch(:score)
|
44
|
+
parse json: Faraday.get(api_url).body
|
39
45
|
end
|
40
46
|
|
41
|
-
def self.
|
42
|
-
|
43
|
-
|
44
|
-
|
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}"
|
45
51
|
end
|
52
|
+
score = raw_response.fetch('ruleGroups').fetch('SPEED').fetch('score')
|
53
|
+
{
|
54
|
+
score: score,
|
55
|
+
raw_response: raw_response
|
56
|
+
}
|
46
57
|
end
|
47
58
|
end
|
48
59
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module RSpec
|
2
|
+
# Matchers to help test RSpec matchers
|
3
|
+
module Matchers
|
4
|
+
def fail
|
5
|
+
raise_error(RSpec::Expectations::ExpectationNotMetError)
|
6
|
+
end
|
7
|
+
|
8
|
+
def fail_with(message)
|
9
|
+
raise_error(RSpec::Expectations::ExpectationNotMetError, message)
|
10
|
+
end
|
11
|
+
|
12
|
+
def fail_matching(regex)
|
13
|
+
raise_error(RSpec::Expectations::ExpectationNotMetError, regex)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -1,13 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
# Specs for all of the PageSpeed code and matchers.
|
3
|
-
|
4
3
|
require 'spec_helper'
|
5
4
|
require 'rspec/webservice_matchers'
|
6
5
|
require 'web_test/util'
|
7
6
|
|
8
7
|
|
9
|
-
SAMPLE_JSON_RESPONSE = 'spec/fixtures/pagespeed.json'
|
10
|
-
|
11
8
|
describe RSpec::WebserviceMatchers::BeFast do
|
12
9
|
describe '#be_fast' do
|
13
10
|
it 'performs a Google PageSpeed Insights API query on a fast site' do
|
@@ -35,7 +35,7 @@ describe 'redirect_permanently_to' do
|
|
35
35
|
|
36
36
|
it 'gives a good error message when the hostname is bad' do
|
37
37
|
expect {
|
38
|
-
expect('
|
38
|
+
expect('not-a-domain.com').to redirect_permanently_to 'http://the-wrong-site.com/'
|
39
39
|
}.to fail_matching(/not known/i)
|
40
40
|
end
|
41
41
|
end
|
@@ -73,7 +73,7 @@ describe 'redirect_temporarily_to' do
|
|
73
73
|
|
74
74
|
it 'gives a good error message when the hostname is bad' do
|
75
75
|
expect {
|
76
|
-
expect('
|
76
|
+
expect('not-a-domain.com').to redirect_temporarily_to 'www.nowhere.com'
|
77
77
|
}.to fail_matching(/not known/i)
|
78
78
|
end
|
79
79
|
end
|
@@ -2,61 +2,66 @@
|
|
2
2
|
require 'spec_helper'
|
3
3
|
require 'rspec/webservice_matchers'
|
4
4
|
|
5
|
-
describe '
|
6
|
-
|
7
|
-
|
8
|
-
# TODO: set up a test server for this. (?)
|
9
|
-
expect('www.eff.org').to have_a_valid_cert
|
10
|
-
end
|
5
|
+
describe 'SSL tests' do
|
6
|
+
before(:each) { WebMock.allow_net_connect! }
|
7
|
+
after(:each) { WebMock.disable_net_connect! }
|
11
8
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
9
|
+
describe 'have_a_valid_cert matcher' do
|
10
|
+
it 'passes when SSL is properly configured' do
|
11
|
+
# EFF created the HTTPS Everywhere movement
|
12
|
+
# TODO: set up a test server for this. (?)
|
13
|
+
expect('www.eff.org').to have_a_valid_cert
|
14
|
+
end
|
17
15
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
16
|
+
it 'fails if the server is not serving SSL at all' do
|
17
|
+
expect do
|
18
|
+
expect('www.psu.edu').to have_a_valid_cert
|
19
|
+
end.to fail_matching(/443/)
|
20
|
+
end
|
23
21
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
22
|
+
it 'provides a relevant error message' do
|
23
|
+
expect do
|
24
|
+
expect('www.psu.edu').to have_a_valid_cert
|
25
|
+
end.to fail_matching(/(unreachable)|(no route to host)|(connection refused)/i)
|
26
|
+
end
|
29
27
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
end
|
28
|
+
it "provides a relevant error message when the domain name doesn't exist" do
|
29
|
+
expect do
|
30
|
+
expect('sdfgkljhsdfghjkhsdfgj.edu').to have_a_valid_cert
|
31
|
+
end.to fail_matching(/not known/i)
|
32
|
+
end
|
36
33
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
end
|
34
|
+
it "provides a good error message when it's a redirect" do
|
35
|
+
expect do
|
36
|
+
# Can't figure out how to do this with WebMock.
|
37
|
+
expect('bloc.io').to have_a_valid_cert
|
38
|
+
end.to fail_matching(/redirect/i)
|
39
|
+
end
|
44
40
|
|
45
|
-
#
|
46
|
-
|
47
|
-
|
48
|
-
expect('
|
41
|
+
# TODO: Find a good way to test this.
|
42
|
+
# it 'provides a good error message if the request times out' do
|
43
|
+
# expect {
|
44
|
+
# expect('www.myapp.com').to have_a_valid_cert
|
45
|
+
# }.to fail_matching(/(timeout)|(execution expired)/)
|
46
|
+
# end
|
49
47
|
end
|
50
48
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
49
|
+
# See https://www.eff.org/https-everywhere
|
50
|
+
describe 'enforce_https_everywhere' do
|
51
|
+
it 'passes when http requests are redirected to valid https urls' do
|
52
|
+
expect('eff.org').to enforce_https_everywhere
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'provides a relevant error message' do
|
56
|
+
expect do
|
57
|
+
expect('www.psu.edu').to enforce_https_everywhere
|
58
|
+
end.to fail_matching(/200/)
|
59
|
+
end
|
56
60
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
+
it "provides a relevant error message when the domain name doesn't exist" do
|
62
|
+
expect do
|
63
|
+
expect('asdhfjkalsdhfjklasdfhjkasdhfl.com').to enforce_https_everywhere
|
64
|
+
end.to fail_matching(/connection failed/i)
|
65
|
+
end
|
61
66
|
end
|
62
67
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,56 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
require '
|
2
|
+
require 'web_mock_config'
|
3
|
+
require 'failure_matchers'
|
3
4
|
|
4
|
-
|
5
|
-
config.before(:each) do
|
6
|
-
WebMock.stub_request :any, 'http://a-page.com/a/page.txt'
|
7
|
-
WebMock.stub_request :any, 'www.website.com'
|
8
|
-
WebMock.stub_request(:any, /notfound.com/).to_return(status: 404)
|
9
|
-
WebMock.stub_request(:any, 'outoforder.com').to_return(status: 503)
|
10
|
-
|
11
|
-
# A host which doesn't support HEAD
|
12
|
-
WebMock.stub_request(:head, 'appengine.com').to_return(status: 405)
|
13
|
-
WebMock.stub_request(:get, 'appengine.com').to_return(status: 200)
|
14
|
-
|
15
|
-
WebMock.stub_request(:any, 'perm-redirector.com')
|
16
|
-
.to_return(status: 301, headers: { Location: 'http://www.website.com/' })
|
17
|
-
|
18
|
-
WebMock.stub_request(:any, 'temp-redirector.org')
|
19
|
-
.to_return(status: 302, headers: { Location: 'http://a-page.com/a/page.txt' })
|
20
|
-
|
21
|
-
WebMock.stub_request(:any, 'temp-307-redirector.net')
|
22
|
-
.to_return(status: 307, headers: { Location: 'http://a-page.com/a/page.txt' })
|
23
|
-
|
24
|
-
# Timeout scenarios
|
25
|
-
WebMock.stub_request(:any, 'www.timeout.com').to_timeout
|
26
|
-
WebMock.stub_request(:any, 'www.timeout-once.com').to_timeout.then.to_return(body: 'abc')
|
27
|
-
|
28
|
-
# Insights API
|
29
|
-
key = ENV['WEBSERVICE_MATCHER_INSIGHTS_KEY']
|
30
|
-
WebMock.stub_request(:get, "https://www.googleapis.com/pagespeedonline/v2/runPagespeed?key=#{key}&screenshot=false&url=http://nonstop.qa")
|
31
|
-
.with(headers: { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => 'Faraday v0.9.2' })
|
32
|
-
.to_return(
|
33
|
-
status: 200,
|
34
|
-
body: IO.read('spec/fixtures/pagespeed.json'),
|
35
|
-
headers: {})
|
36
|
-
|
37
|
-
WebMock.allow_net_connect!
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
module RSpec
|
42
|
-
# Matchers to help test RSpec matchers
|
43
|
-
module Matchers
|
44
|
-
def fail
|
45
|
-
raise_error(RSpec::Expectations::ExpectationNotMetError)
|
46
|
-
end
|
47
|
-
|
48
|
-
def fail_with(message)
|
49
|
-
raise_error(RSpec::Expectations::ExpectationNotMetError, message)
|
50
|
-
end
|
51
|
-
|
52
|
-
def fail_matching(regex)
|
53
|
-
raise_error(RSpec::Expectations::ExpectationNotMetError, regex)
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
5
|
+
SAMPLE_PAGESPEED_JSON_RESPONSE = 'spec/fixtures/pagespeed.json'
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'faraday'
|
3
|
+
require 'webmock/rspec'
|
4
|
+
|
5
|
+
RSpec.configure do |config|
|
6
|
+
config.before(:each) do
|
7
|
+
# HOSTS WHICH RETURN ERRORS
|
8
|
+
WebMock.stub_request(:any, /notfound.com/).to_return(status: 404)
|
9
|
+
WebMock.stub_request(:any, 'outoforder.com').to_return(status: 503)
|
10
|
+
WebMock.stub_request(:any, 'not-a-domain.com')
|
11
|
+
.to_raise(Faraday::ConnectionFailed.new('Failed to open TCP ' \
|
12
|
+
'connection to asdhfjkahsdfadfd.com:80 ' \
|
13
|
+
'(getaddrinfo: nodename nor servname provided, ' \
|
14
|
+
'or not known)'))
|
15
|
+
|
16
|
+
# FUNCTIONING WEB PAGES
|
17
|
+
WebMock.stub_request :any, 'http://a-page.com/a/page.txt'
|
18
|
+
WebMock.stub_request :any, 'www.website.com'
|
19
|
+
|
20
|
+
# A HOST WHICH DOESN'T SUPPORT HEAD
|
21
|
+
WebMock.stub_request(:head, 'appengine.com').to_return(status: 405)
|
22
|
+
WebMock.stub_request(:get, 'appengine.com').to_return(status: 200)
|
23
|
+
|
24
|
+
# FUNCTIONING REDIRECTS
|
25
|
+
WebMock.stub_request(:head, 'perm-redirector.com')
|
26
|
+
.to_return(status: 301,
|
27
|
+
body: '',
|
28
|
+
headers: { Location: 'http://www.website.com/' })
|
29
|
+
|
30
|
+
WebMock.stub_request(:any, 'temp-redirector.org')
|
31
|
+
.to_return(status: 302,
|
32
|
+
headers: { Location: 'http://a-page.com/a/page.txt' })
|
33
|
+
|
34
|
+
WebMock.stub_request(:any, 'temp-307-redirector.net')
|
35
|
+
.to_return(status: 307,
|
36
|
+
headers: { Location: 'http://a-page.com/a/page.txt' })
|
37
|
+
|
38
|
+
# TIMEOUT SCENARIOS
|
39
|
+
WebMock.stub_request(:any, 'www.timeout.com').to_timeout
|
40
|
+
WebMock.stub_request(:any, 'www.timeout-once.com').to_timeout
|
41
|
+
.then.to_return(body: 'abc')
|
42
|
+
|
43
|
+
# PageSpeed Insights API
|
44
|
+
key = ENV['WEBSERVICE_MATCHER_INSIGHTS_KEY']
|
45
|
+
WebMock.stub_request(:get,
|
46
|
+
'https://www.googleapis.com/pagespeedonline/v2/' \
|
47
|
+
"runPagespeed?key=#{key}&screenshot=false" \
|
48
|
+
'&url=http://nonstop.qa')
|
49
|
+
.to_return(
|
50
|
+
status: 200,
|
51
|
+
body: IO.read('spec/fixtures/pagespeed.json'),
|
52
|
+
headers: {})
|
53
|
+
end
|
54
|
+
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
require 'spec_helper'
|
2
3
|
require 'web_test/be_fast'
|
3
4
|
|
4
5
|
RSpec.describe WebTest::BeFast do
|
@@ -14,7 +15,7 @@ RSpec.describe WebTest::BeFast do
|
|
14
15
|
|
15
16
|
describe '#parse' do
|
16
17
|
it 'can parse the overall score' do
|
17
|
-
api_response = File.read(
|
18
|
+
api_response = File.read(SAMPLE_PAGESPEED_JSON_RESPONSE)
|
18
19
|
data = WebTest::BeFast.parse json: api_response
|
19
20
|
expect(data[:score]).to eq 85
|
20
21
|
end
|
@@ -32,5 +33,14 @@ RSpec.describe WebTest::BeFast do
|
|
32
33
|
WebTest::BeFast::TestResult.new { |r| r.success = true }
|
33
34
|
end.to raise_error(ArgumentError, /score/i)
|
34
35
|
end
|
36
|
+
|
37
|
+
it 'requires :response' do
|
38
|
+
expect do
|
39
|
+
WebTest::BeFast::TestResult.new { |r|
|
40
|
+
r.success = true
|
41
|
+
r.score = 90
|
42
|
+
}
|
43
|
+
end.to raise_error(ArgumentError, /response/i)
|
44
|
+
end
|
35
45
|
end
|
36
46
|
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.11.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-
|
11
|
+
date: 2016-04-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -192,6 +192,7 @@ files:
|
|
192
192
|
- lib/web_test/be_up.rb
|
193
193
|
- lib/web_test/util.rb
|
194
194
|
- rspec-webservice_matchers.gemspec
|
195
|
+
- spec/failure_matchers.rb
|
195
196
|
- spec/fixtures/pagespeed.json
|
196
197
|
- spec/rspec/webservice_matchers/page_speed_spec.rb
|
197
198
|
- spec/rspec/webservice_matchers/protocol_spec.rb
|
@@ -199,6 +200,7 @@ files:
|
|
199
200
|
- spec/rspec/webservice_matchers/redirect_spec.rb
|
200
201
|
- spec/rspec/webservice_matchers/ssl_spec.rb
|
201
202
|
- spec/spec_helper.rb
|
203
|
+
- spec/web_mock_config.rb
|
202
204
|
- spec/web_test/be_fast_spec.rb
|
203
205
|
- spec/web_test/be_up_spec.rb
|
204
206
|
- spec/web_test/util_spec.rb
|
@@ -222,11 +224,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
222
224
|
version: '0'
|
223
225
|
requirements: []
|
224
226
|
rubyforge_project:
|
225
|
-
rubygems_version: 2.
|
227
|
+
rubygems_version: 2.6.2
|
226
228
|
signing_key:
|
227
229
|
specification_version: 4
|
228
230
|
summary: Black-box web app configuration testing
|
229
231
|
test_files:
|
232
|
+
- spec/failure_matchers.rb
|
230
233
|
- spec/fixtures/pagespeed.json
|
231
234
|
- spec/rspec/webservice_matchers/page_speed_spec.rb
|
232
235
|
- spec/rspec/webservice_matchers/protocol_spec.rb
|
@@ -234,6 +237,7 @@ test_files:
|
|
234
237
|
- spec/rspec/webservice_matchers/redirect_spec.rb
|
235
238
|
- spec/rspec/webservice_matchers/ssl_spec.rb
|
236
239
|
- spec/spec_helper.rb
|
240
|
+
- spec/web_mock_config.rb
|
237
241
|
- spec/web_test/be_fast_spec.rb
|
238
242
|
- spec/web_test/be_up_spec.rb
|
239
243
|
- spec/web_test/util_spec.rb
|