-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from nemski/check-forbidden-cacheability
Check forbidden cacheability
- Loading branch information
Showing
7 changed files
with
68 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters