class Kentico::Kontent::Delivery::Tests::FakeResponder

Constants

CONTINUATION_HEADER_1
CONTINUATION_HEADER_2
CONTINUATION_HEADER_3
IS_SECURE

Public Class Methods

get_response(query, url, headers) click to toggle source
# File lib/delivery/tests/fake_responder.rb, line 16
def get_response(query, url, headers)
  @query = query
  if IS_SECURE && !(
    headers['Authorization'] == "Bearer #{query.secure_key}" ||
    headers['Authorization'] == "Bearer #{query.preview_key}"
  )
    return respond_401
  end

  url =
    if @query.should_preview
      url["https://preview-deliver.kontent.ai/#{query.project_id}".length...url.length]
    else
      url["https://deliver.kontent.ai/#{query.project_id}".length...url.length]
    end

  return respond_429 if @query.code_name == '429'

  qs = url.contains('?') ? url.split('?')[1] : nil

  return respond_feed if query.query_type.eql? Kentico::Kontent::Delivery::QUERY_TYPE_ITEMS_FEED

  return respond_filtering qs unless qs.nil? # e.g. /items/about_us?skip=0&limit=5

  respond_generic url # Didn't match other clauses, so response should be located in corresponding filepath
end
respond_401() click to toggle source
# File lib/delivery/tests/fake_responder.rb, line 90
def respond_401
  path = Pathname.new(File.dirname(__FILE__) + '/401.json')
  Kentico::Kontent::Delivery::Responses::ResponseBase.new 401, '', '', path.read if path.exist?
end
respond_429() click to toggle source
# File lib/delivery/tests/fake_responder.rb, line 85
def respond_429
  path = Pathname.new(File.dirname(__FILE__) + '/429.json')
  Kentico::Kontent::Delivery::Responses::ResponseBase.new 429, '', '', path.read if path.exist?
end
respond_feed() click to toggle source
# File lib/delivery/tests/fake_responder.rb, line 51
def respond_feed
  if @query.continuation_exists?
    if @query.continuation_token.include? '#RT:1#'
      headers = {Kentico::Kontent::Delivery::DeliveryQuery::HEADER_CONTINUATION => CONTINUATION_HEADER_2}
      path = Pathname.new(File.dirname(__FILE__) + '/items_feed/articles_feed_2.json')
    else
      headers = ''
      path = Pathname.new(File.dirname(__FILE__) + '/items_feed/articles_feed_3.json')
    end
  else
    headers = {Kentico::Kontent::Delivery::DeliveryQuery::HEADER_CONTINUATION => CONTINUATION_HEADER_1}
    path = Pathname.new(File.dirname(__FILE__) + '/items_feed/articles_feed_1.json')
  end

  OpenStruct.new(
    headers: headers,
    body: path.read
  )
end
respond_filtering(query) click to toggle source
# File lib/delivery/tests/fake_responder.rb, line 71
def respond_filtering(query)
  path =
    case CGI.unescape query
    when 'includeTotalCount=1'
      Pathname.new(File.dirname(__FILE__) + '/filtering/items_with_count.json')
    when 'skip=0&limit=5'
      Pathname.new(File.dirname(__FILE__) + '/filtering/pagination.json')
    end
  OpenStruct.new(
    headers: '',
    body: path.read
  )
end
respond_generic(url) click to toggle source
# File lib/delivery/tests/fake_responder.rb, line 43
def respond_generic(url)
  path = Pathname.new(File.dirname(__FILE__) + "/generic#{url}.json")
  OpenStruct.new(
    headers: '',
    body: path.read
  )
end