class Soaspec::Baseline

Used for defining parameters for recording and asserting against a baseline. This does not take into account different payloads in the request, only for where a response varies by suburls or query parameters

Constants

ALLOWED_FORMATS

@return [Array] List of allowed formats

Attributes

folder[RW]

@return [String] Folder where baselines are recorded and retrieved

description[W]

@return [String] Name of file including folders describing baseline

exchange[RW]

@return [Exchange] Exchange object to save/assert baseline for

format[RW]

@return [Symbol] Format in which baseline is stored. Either :raw or :hash

Public Class Methods

new(exchange, format) click to toggle source

@param [Exchange] exchange Exchange object to baseline response for @param [Symbol] format Format of baseline

# File lib/soaspec/baseline.rb, line 26
def initialize(exchange, format)
  self.exchange = exchange
  self.format = format
  return if ALLOWED_FORMATS.include? format

  raise ArgumentError, "Expected format #{format} to be " \
      "either #{ALLOWED_FORMATS}"
end

Public Instance Methods

actual_content() click to toggle source

@return [String, Hash] Actual response from API

# File lib/soaspec/baseline.rb, line 55
def actual_content
  if format == :hash
    exchange.to_hash
  elsif format == :raw
    exchange.pretty_response_body.strip
  else
    raise NotImplementedError, "Format #{format} is not #{ALLOWED_FORMATS}"
  end
end
content_to_save() click to toggle source

Content to save as a baseline

# File lib/soaspec/baseline.rb, line 50
def content_to_save
  format == :raw ? exchange.pretty_response_body : YAML.dump(exchange.to_hash)
end
description() click to toggle source

@return [String] Name of file including folders describing baseline

# File lib/soaspec/baseline.rb, line 78
def description
  @description || exchange.request_parameters.description
end
file() click to toggle source

@return [String] File where baseline is stored

# File lib/soaspec/baseline.rb, line 71
def file
  File.join(self.class.folder,
            exchange.exchange_handler.to_s.snakecase,
            "#{description}.#{format == :raw ? exchange.format : 'yml'}")
end
matches?() click to toggle source

Compare baseline with expected result. This will create baseline if not created @return [Boolean] Whether response matches baseline

# File lib/soaspec/baseline.rb, line 38
def matches?
  if File.exist?(file)
    actual_content == read_baseline
  else
    create_file filename: file, content: content_to_save
    raise Soaspec::BaselineError,
          "Created baseline at #{file}. Inspect file to ensure it is
           correct and rerun to ensure baseline is stable"
  end
end
read_baseline() click to toggle source

@return [String] Result of reading baseline

# File lib/soaspec/baseline.rb, line 66
def read_baseline
  format == :raw ? File.read(file).strip : YAML.load_file(file)
end