diff --git a/lib/akamai_rspec/akamai_headers.rb b/lib/akamai_rspec/akamai_headers.rb index 4901239..0037262 100644 --- a/lib/akamai_rspec/akamai_headers.rb +++ b/lib/akamai_rspec/akamai_headers.rb @@ -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' } diff --git a/lib/akamai_rspec/request.rb b/lib/akamai_rspec/request.rb index 2ea8db1..d28ec42 100644 --- a/lib/akamai_rspec/request.rb +++ b/lib/akamai_rspec/request.rb @@ -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 = {}) @@ -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 @@ -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 @@ -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 diff --git a/spec/unit/request_spec.rb b/spec/unit/request_spec.rb index ec3bc2d..bba2baa 100644 --- a/spec/unit/request_spec.rb +++ b/spec/unit/request_spec.rb @@ -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