Skip to content

Commit

Permalink
Add basic net/http request class
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Robinson committed Jan 19, 2016
1 parent 9aa5290 commit 08e2de5
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 5 deletions.
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
67 changes: 63 additions & 4 deletions lib/akamai_rspec/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def self.https_url(url)
end

def self.options
akamai_debug_headers
AkamaiHeaders.akamai_debug_headers
end

def self.http_get(url, options, cookies = {})
Expand All @@ -52,7 +52,7 @@ def self.https_get(url, options, cookies = {})
end

def self.get_with_debug_headers(url, options, cookies = {})
headers = options.merge(akamai_debug_headers).merge(cookies)
headers = options.merge(options).merge(cookies)
do_get_no_verify(url, headers) { |response, _, _| response }
end

Expand All @@ -70,7 +70,7 @@ def self.responsify(maybe_a_url)
maybe_a_url
else
begin
RestClient.get(maybe_a_url, akamai_debug_headers)
RestClient.get(maybe_a_url, options)
rescue RestClient::RequestFailed => exception
# Return the original request
exception.response
Expand All @@ -81,7 +81,66 @@ def self.responsify(maybe_a_url)
def self.request_cache_miss(url)
url += url.include?('?') ? '&' : '?'
url += SecureRandom.hex
RestClient.get(url, akamai_debug_headers)
RestClient.get(url, options)
end
end
end

module AkamaiRSpec
class Request
def self.stg_domain=(domain)
@@akamai_stg_domain = domain
end

def self.prod_domain=(domain)
@@akamai_prod_domain = domain
end

def initialize(env)
@env = env
@domain = case env.downcase
when 'staging'
@@akamai_stg_domain
else
@@akamai_prod_domain
end
end

def get(url)
uri = parse_url(url)

req = build_request(uri, headers)

req['Host'] = uri.hostname
uri.hostname = @domain

Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(req, nil) { |http_response| http_response }
end
end

def parse_url(url)
uri = URI.parse(url)
if uri.hostname.nil?
raise URI::InvalidURIError.new("bad URI(no host provided): #{url}")
end

uri
end

def headers
AkamaiHeaders.akamai_debug_headers.inject({}) do |result, (key, value)|
key = key.to_s.capitalize
result[key] = value.to_s
result
end
end

def build_request(uri, headers)
req = Net::HTTP::Get.new(uri)
headers.each { |key, value| req.send(:[]=, key, value) }

req
end
end
end
29 changes: 29 additions & 0 deletions spec/unit/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,32 @@
end
end
end

describe AkamaiRSpec::Request do
let(:stg_domain) { 'www.example.com.edgesuite-staging.net' }
let(:prod_domain) { 'www.example.com.edgesuite.net' }
let(:network) { 'prod' }
before do
AkamaiRSpec::Request.stg_domain = stg_domain
AkamaiRSpec::Request.prod_domain = prod_domain
end

subject { described_class.new(network).get('http://examples.com') }

describe '#get' do
context 'prod domain' do
it 'queries the right domain' do
expect(Net::HTTP).to receive(:start).with(prod_domain, anything)
subject
end
end

context 'stating domain' do
let(:network) { 'staging' }
it 'quereis the right domain' do
expect(Net::HTTP).to receive(:start).with(stg_domain, anything)
subject
end
end
end
end

0 comments on commit 08e2de5

Please sign in to comment.