class WebStub::Stub

Constants

METHODS

Attributes

callback[RW]
requests[RW]
response_body[R]
response_delay[R]
response_error[R]
response_headers[R]
response_status_code[R]

Public Class Methods

new(method, url) click to toggle source
# File lib/webstub/stub.rb, line 5
def initialize(method, url)
  @request_method = canonicalize_method(method)
  raise ArgumentError, "invalid method name" unless METHODS.include? @request_method

  @requests = 0

  @request_url = canonicalize_url(url)
  @request_headers = nil
  @request_body = nil
  @callback = nil

  @response_body = ""
  @response_delay = 0.0
  @response_error = nil
  @response_headers = {}
  @response_status_code = 200
end

Public Instance Methods

do_callback(headers, body) click to toggle source
# File lib/webstub/stub.rb, line 115
def do_callback(headers, body)
  if @callback
    @callback.call(headers, body)
  end
end
error?() click to toggle source
# File lib/webstub/stub.rb, line 23
def error?
  ! @response_error.nil?
end
matches?(method, url, options={}) click to toggle source
# File lib/webstub/stub.rb, line 27
def matches?(method, url, options={})
  if @request_url != canonicalize_url(url)
    return false
  end

  if @request_method != canonicalize_method(method)
    return false
  end

  if @request_headers
    headers = options[:headers] || {}

    @request_headers.each do |key, value|
      if headers[key] != value
        return false
      end
    end
  end

  if @request_body
    if @request_body != options[:body]
      return false
    end
  end

  true
end
redirects?() click to toggle source
# File lib/webstub/stub.rb, line 58
def redirects?
  @response_status_code.between?(300, 399) && @response_headers["Location"] != nil
end
requested?() click to toggle source
# File lib/webstub/stub.rb, line 62
def requested?
  @requests > 0
end
to_fail(options) click to toggle source
# File lib/webstub/stub.rb, line 72
def to_fail(options)
  if error = options.delete(:error)
    @response_error = error
  elsif code = options.delete(:code)
    @response_error = NSError.errorWithDomain(NSURLErrorDomain, code: code, userInfo: nil)
  else
    raise ArgumentError, "to_fail requires either the code or error option"
  end

  self
end
to_redirect(options) click to toggle source
# File lib/webstub/stub.rb, line 125
def to_redirect(options)
  unless url = options.delete(:url)
    raise ArgumentError, "to_redirect requires the :url option"
  end

  options[:headers] ||= {}
  options[:headers]["Location"] = url
  options[:status_code] = 301

  to_return(options)
end
to_return(options) click to toggle source
# File lib/webstub/stub.rb, line 84
def to_return(options)
  if status_code = options[:status_code]
    @response_status_code = status_code
  end

  if headers = options[:headers]
    @response_headers.merge!(headers)
  end

  if json = options[:json]
    @response_body = json
    @response_headers["Content-Type"] = "application/json"

    if @response_body.is_a?(Hash) || @response_body.is_a?(Array)
      @response_body = JSON.generate(@response_body)
    end
  else
    @response_body = options[:body] || ""

    if content_type = options[:content_type]
      @response_headers["Content-Type"] = content_type
    end
  end

  if delay = options[:delay]
    @response_delay = delay
  end

  self
end
with(options) click to toggle source
# File lib/webstub/stub.rb, line 137
def with(options)
  if body = options[:body]
    @request_body = body

    if @request_body.is_a?(Hash)
      @request_body = @request_body.inject({}) { |h, (k,v)| h[k.to_s] = v; h }
    end
  end

  if headers = options[:headers]
    @request_headers = headers
  end

  self
end
with_callback(&callback) click to toggle source
# File lib/webstub/stub.rb, line 121
def with_callback(&callback)
  @callback = callback
end

Private Instance Methods

canonicalize_method(method) click to toggle source
# File lib/webstub/stub.rb, line 155
def canonicalize_method(method)
  method.to_s.upcase
end
canonicalize_url(url) click to toggle source
# File lib/webstub/stub.rb, line 159
def canonicalize_url(url)
  scheme, authority, hostname, port, path, query, fragment = URI.split(url)

  parts = scheme.downcase
  parts << "://"

  if authority
    parts << authority
    parts << "@"
  end

  parts << hostname.downcase

  if port
    well_known_ports = { "http" => 80, "https" => 443 }
    if well_known_ports[scheme] != port
      parts << ":#{port}"
    end
  end

  if path != "/"
    parts << path
  end

  if query && !query.empty?
    q = query.split("&").sort.join("&")
    parts << "?#{q}"
  end

  if fragment && !fragment.empty?
    parts << "##{fragment}"
  end

  parts
end