Skip to content

Commit

Permalink
Merge pull request #6 from nemski/check-forbidden-cacheability
Browse files Browse the repository at this point in the history
Check forbidden cacheability
  • Loading branch information
beegibson committed Jan 18, 2016
2 parents 45eba21 + 59e07ab commit 9aa5290
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 43 deletions.
5 changes: 3 additions & 2 deletions lib/akamai_rspec/matchers/caching.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ module RSpec::Matchers
response = RestClient::Request.responsify response.args[:url] # again to prevent spurious cache miss

not_cached = response.headers[:x_cache] =~ /TCP(\w+)?_MISS/
unless not_cached
if not_cached
true
else
fail("x_cache header does not indicate an origin hit: '#{response.headers[:x_cache]}'")
end
response.code == 200 && not_cached
end
end

Expand Down
8 changes: 8 additions & 0 deletions lib/akamai_rspec/matchers/non_akamai.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,11 @@ def ssl_client_for_verify_cert(tcp_client, addr, url)
response.cookies[cookie]
end
end

RSpec::Matchers.define :be_forbidden do
match do |url|
response = RestClient::Request.responsify url
fail('Response was not forbidden') unless response.code == 403
true
end
end
50 changes: 15 additions & 35 deletions lib/akamai_rspec/matchers/x_cache_headers.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,29 @@
require 'rspec'

X_CACHE_HEADERS = [:x_true_cache_key, :x_cache_key]
module AkamaiRSpec
module Helpers
X_CACHE_HEADERS = [:x_true_cache_key, :x_cache_key]

RSpec::Matchers.define :be_served_from_origin do |contents|
match do |url|
match_fn = lambda { |header, expected| header && header =~ /\/#{expected}\// }
expect(url).to have_matching_x_cache_headers(contents, match_fn)
def x_cache_headers
X_CACHE_HEADERS
end
end
end

RSpec::Matchers.define :have_cp_code do |contents|
RSpec::Matchers.define :be_served_from_origin do |contents|
include AkamaiRSpec::Helpers
match do |url|
match_fn = lambda { |header, expected| header.include?(expected) }
expect(url).to have_matching_x_cache_headers(contents, match_fn)
response = RestClient::Request.responsify url
response.headers.any? { |key, value| x_cache_headers.include?(key) && value =~ /\/#{contents}\// } && \
response.code == 200
end
end

RSpec::Matchers.define :have_matching_x_cache_headers do |contents, match_fn|
RSpec::Matchers.define :have_cp_code do |contents|
include AkamaiRSpec::Helpers
match do |url|
response = RestClient::Request.responsify url
has_x_cache_headers(response)
return true if x_cache_headers_match(response, contents, match_fn)
missing_x_cache_error(response, contents)
expect(response).to be_successful
end
end

def x_cache_headers_match(response, contents, match_fn)
X_CACHE_HEADERS.each do |key|
return true if response.headers[key] && match_fn.call(response.headers[key], contents)
response.headers.any? { |key, value| x_cache_headers.include?(key) && value == contents } && \
response.code == 200
end
false
end

def has_x_cache_headers(response)
unless X_CACHE_HEADERS.inject(false) { |bool, header| bool || response.headers.include?(header) }
fail "Response does not contain the debug headers"
end
end

def missing_x_cache_error(response, contents)
X_CACHE_HEADERS.each do |key|
if (response.headers[key])
fail("#{key} has value '#{response.headers[key]}' which doesn't match '#{contents}'")
end
end
end

7 changes: 6 additions & 1 deletion lib/akamai_rspec/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ def self.responsify(maybe_a_url)
if maybe_a_url.is_a? RestClient::Response
maybe_a_url
else
RestClient.get(maybe_a_url, akamai_debug_headers)
begin
RestClient.get(maybe_a_url, akamai_debug_headers)
rescue RestClient::RequestFailed => exception
# Return the original request
exception.response
end
end
end

Expand Down
20 changes: 20 additions & 0 deletions spec/functional/non_akamai_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,23 @@
expect(DOMAIN).to be_verifiably_secure(false)
end
end

describe 'be_forbidden' do
before(:each) do
stub_status('/success', 200)
stub_status('/notfound', 404)
stub_status('/forbidden', 403)
end

it 'should pass when it gets a 403' do
expect(DOMAIN + '/forbidden').to be_forbidden
end

it 'should fail when it gets 404' do
expect { expect(DOMAIN + '/notfound').to be_forbidden }.to raise_error(RuntimeError)
end

it 'should fail when it gets 200' do
expect { expect(DOMAIN + '/success').to be_forbidden }.to raise_error(RuntimeError)
end
end
10 changes: 5 additions & 5 deletions spec/functional/x_cache_headers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
end

it 'should fail when cp code is wrong' do
expect { expect(DOMAIN + '/correct').to have_cp_code('wrong') }.to raise_error(RuntimeError)
expect { expect(DOMAIN + '/correct').to have_cp_code('wrong') }.to raise_error(RSpec::Expectations::ExpectationNotMetError)
end

it 'should fail when both cache-key headers are not set' do
expect { expect(DOMAIN + '/no-cp').to have_cp_code('wrong') }.to raise_error(RuntimeError)
expect { expect(DOMAIN + '/no-cp').to have_cp_code('wrong') }.to raise_error(RSpec::Expectations::ExpectationNotMetError)
end
end

Expand All @@ -45,16 +45,16 @@

it 'should fail on 300 and correct origin' do
expect { expect(DOMAIN + '/redirect').to be_served_from_origin('originsite.example.com') }
.to raise_error(RuntimeError)
.to raise_error(RSpec::Expectations::ExpectationNotMetError)
end

it 'should fail on 200 and incorrect origin' do
expect { expect(DOMAIN + '/correct').to be_served_from_origin('someothersite.example.com') }
.to raise_error(RuntimeError)
.to raise_error(RSpec::Expectations::ExpectationNotMetError)
end

it 'should fail on 200 and origin that only partially matches' do
expect { expect(DOMAIN + '/correct').to be_served_from_origin('site.example.com') }
.to raise_error(RuntimeError)
.to raise_error(RSpec::Expectations::ExpectationNotMetError)
end
end
11 changes: 11 additions & 0 deletions spec/unit/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,15 @@
expect(RestClient::Request.https_url(path)).to eq("https://#{prod_domain}/")
end
end

describe '#responsify' do
let(:url) { 'nonexistantdomain' }
before do
stub_request(:any, url).to_return(
body: 'abc', status: [500, 'message'])
end
it 'should not raise an exception when a RestClient exception is raised' do
expect { RestClient::Request.responsify(url) }.to_not raise_error
end
end
end

0 comments on commit 9aa5290

Please sign in to comment.