Skip to content

Commit

Permalink
Initial tests :D
Browse files Browse the repository at this point in the history
  • Loading branch information
beegibson committed Jul 19, 2015
1 parent 4d72e7f commit 3bbe931
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 41 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ gem 'rspec'
gem 'rake'
gem 'byebug'
gem 'json'
gem 'webmock'
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
GEM
remote: https://rubygems.org/
specs:
addressable (2.3.8)
byebug (4.0.5)
columnize (= 0.9.0)
columnize (0.9.0)
crack (0.4.2)
safe_yaml (~> 1.0.0)
diff-lcs (1.2.5)
json (1.8.1)
mime-types (2.4.3)
Expand All @@ -25,6 +28,10 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.2.0)
rspec-support (3.2.2)
safe_yaml (1.0.4)
webmock (1.21.0)
addressable (>= 2.3.6)
crack (>= 0.3.2)

PLATFORMS
ruby
Expand All @@ -35,3 +42,4 @@ DEPENDENCIES
rake
rest-client (~> 1.7.2)
rspec
webmock
11 changes: 2 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@ Use rspec to test your akamai configuration.
## How to use

### Basic configuration
To use, you must configure you domain:
```
RestClient::Request.domain("www.example.com")
```
Where my-domain does not include '.edgesuite.net' and '.edgesuite-staging.net', because the library
adds them automatically.

If you need to set both the staging and prod domains manually, you can use:
To use the requests outside of matchers, you must configure your domain:

```
RestClient::Request.prod_domain("www.example.com.edgesuite.net")
Expand Down Expand Up @@ -110,7 +103,7 @@ Expect the response to contain the specified cookie
response = RestClient::Request::http_get(url, headers)
response = RestClient::Request::https_get(url, headers)
```
Makes request to your base url that you set during initial configuration + ```url`` supplied in call.
Makes request to your base url that you set during initial configuration + ```url``` supplied in call.

# Contributions
We would be very thankful for any contributions, particularly documentation or tests.
Expand Down
2 changes: 1 addition & 1 deletion lib/akamai_rspec/akamai_headers.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module AkamaiHeaders
def akamai_debug_headers
def self.akamai_debug_headers
{
pragma: 'akamai-x-cache-on, akamai-x-cache-remote-on, akamai-x-check-cacheable, akamai-x-get-cache-key, akamai-x-get-extracted-values, akamai-x-get-nonces, akamai-x-get-ssl-client-session-id, akamai-x-get-true-cache-key, akamai-x-serial-no'
}
Expand Down
8 changes: 1 addition & 7 deletions lib/akamai_rspec/matchers/caching.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module RSpec::Matchers

RSpec::Matchers.define :be_tier_distributed do
match do |url|
response = request_cache_miss(url)
response = RestClient::Request.request_cache_miss(url)
tiered = !response.headers[:x_cache_remote].nil?
fail('No X-Cache-Remote header in response') unless tiered
response.code == 200 && tiered
Expand All @@ -52,9 +52,3 @@ def x_check_cacheable(response, should_be_cacheable)
fail("X-Check-Cacheable header is: #{x_check_cacheable} expected #{should_be_cacheable}")
end
end

def request_cache_miss(url)
url += url.include?('?') ? '&' : '?'
url += SecureRandom.hex
RestClient.get(url, akamai_debug_headers)
end
15 changes: 0 additions & 15 deletions lib/akamai_rspec/matchers/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,6 @@
end
end

def request_cache_miss(url)
url += url.include?('?') ? '&' : '?'
url += SecureRandom.hex
RestClient.get(url, akamai_debug_headers)
end

RSpec::Matchers.define :be_tier_distributed do
match do |url|
response = request_cache_miss(url)
tiered = !response.headers[:x_cache_remote].nil?
fail('No X-Cache-Remote header in response') unless tiered
response.code == 200 && tiered
end
end

RSpec::Matchers.define :have_cp_code do |cpcode|
match do |response_or_url|
response = RestClient::Request.responsify response_or_url
Expand Down
19 changes: 10 additions & 9 deletions lib/akamai_rspec/request.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'rest-client'
require_relative 'akamai_headers'
require_relative './akamai_headers'

module RestClient
class Request
Expand Down Expand Up @@ -29,11 +29,6 @@ def self.prod_domain(domain)
@@akamai_prod_domain = domain
end

def self.domain(domain)
@@akamai_prod_domain = domain + ".edgesuite.net"
@@akamai_stg_domain = domain + ".edgesuite-staging.net"
end

def self.http_url(url)
url = "/#{url}" unless url.start_with?('/')
"http://#{domain}#{url}"
Expand All @@ -46,7 +41,7 @@ def self.https_url(url)

# Define the Host header and join the Akamai headers
def self.options
akamai_debug_headers
AkamaiHeaders.akamai_debug_headers
end

# Make requests to the right network
Expand All @@ -64,7 +59,7 @@ def self.do_get(url, options, cookies = {}, is_secure)
else
base_url = http_url(url)
end
headers = options.merge(akamai_debug_headers).merge(cookies)
headers = options.merge(AkamaiHeaders.akamai_debug_headers).merge(cookies)
do_get_no_ssl(base_url, headers) { |response, _, _| response }
end

Expand All @@ -81,8 +76,14 @@ 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)
RestClient.get(maybe_a_url, AkamaiHeaders.akamai_debug_headers)
end
end

def self.request_cache_miss(url)
url += url.include?('?') ? '&' : '?'
url += SecureRandom.hex
RestClient.get(url, AkamaiHeaders.akamai_debug_headers)
end
end
end
78 changes: 78 additions & 0 deletions spec/matchers/caching_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
require 'spec_helper'

describe "be_cacheable" do
before(:each) do
stub_headers("/cacheable", {"X-Check-Cacheable" => "YES" })
stub_headers("/not_cacheable", {"X-Check-Cacheable" => "NO" })
end

it "should succeed when cacheable" do
expect(DOMAIN + "/cacheable").to be_cacheable
end

it "should fail when not cacheable" do
expect{expect(DOMAIN + "/not_cacheable").to be_cacheable}.to raise_error(RuntimeError)
end
end

describe "have_no_cache_set" do
before(:each) do
stub_headers("/cacheable", {"X-Check-Cacheable" => "YES" })
stub_headers("/not_cacheable", {"Cache-control" => "no-cache" })
end

it "should succeed when not cacheable" do
expect(DOMAIN + "/not_cacheable").to have_no_cache_set
end

it "should fail when cacheable" do
expect{expect(DOMAIN + "/cacheable").to have_no_cache_set}.to raise_error(RuntimeError)
end
end

describe "not_be_cached" do
before(:each) do
stub_headers("/cacheable_but_miss", { "X-Check-Cacheable" => "YES", "X-Cache" => "TCP_MISS" })
stub_headers("/not_cacheable", { "X-Check-Cacheable" => "NO", "X-Cache" => "TCP_MISS" })
stub_headers("/cacheable_and_cached", { "X-Check-Cacheable" => "YES", "X-Cache" => "TCP_HIT" })
stub_headers("/not_cacheable_but_cached", { "X-Check-Cacheable" => "NO", "X-Cache" => "TCP_HIT" })
end

it "should succeed when not cacheable" do
expect(DOMAIN + "/not_cacheable").to not_be_cached
end

it "should fail when cacheable but missed" do
expect{expect(DOMAIN + "/cacheable_but_miss").to not_be_cached}.to raise_error(RuntimeError)
end

it "should fail when supposedly not cacheable but cached anyway" do
expect{expect(DOMAIN + "/not_cacheable_but_cached").to not_be_cached}.to raise_error(RuntimeError)
end

it "should fail when cacheable and cached" do
expect{expect(DOMAIN + "/cacheable_and_cached").to not_be_cached}.to raise_error(RuntimeError)
end
end

describe "be_tier_distributed" do
before(:each) do
cacheable_uri = Addressable::Template.new DOMAIN + "/cacheable?{random}"
stub_request(:any, cacheable_uri).to_return( :body => "abc", :headers => {"X_Cache_Remote" => "TCP_MISS" })
not_cacheable_uri = Addressable::Template.new DOMAIN + "/not_cacheable?{random}"
stub_request(:any, not_cacheable_uri).to_return( :body => "abc", :headers => {"Cache-control" => "no-cache" })
end

it "should succeed when it is remote cached" do
expect(DOMAIN + "/cacheable").to be_tier_distributed
end

it "should fail when not remotely cached" do
expect{expect(DOMAIN + "/not_cacheable").to be_tier_distributed}.to raise_error(RuntimeError)
end
end

def stub_headers(url, headers)
stub_request(:any, DOMAIN + url).to_return(
:body => "abc", :headers => headers)
end
6 changes: 6 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'rspec'
require 'webmock/rspec'
require 'akamai_rspec'

DOMAIN = "www.example.com.edgesuite.net"
RestClient::Request.prod_domain(DOMAIN)

0 comments on commit 3bbe931

Please sign in to comment.