class Stella::Session

Attributes

base_uri[RW]
events[R]
exception[RW]
handler[R]
header[RW]
headers[RW]
http_auth[R]
http_client[RW]
http_method[RW]
param[RW]
params[RW]
previous_doc[R]
redirect_uri[RW]
req[R]
res[R]
response[R]
response_handler[R]
rt[R]
session[R]
uri[RW]
vars[R]

Public Class Methods

new(base_uri=nil) click to toggle source
# File lib/stella/client.rb, line 235
def initialize(base_uri=nil)
  @base_uri = base_uri
  @vars = indifferent_hash
  @base_uri &&= Addressable::URI.parse(@base_uri) if String === @base_uri
  @events = SelectableArray.new
end

Public Instance Methods

clear_previous_request() click to toggle source
# File lib/stella/client.rb, line 379
def clear_previous_request
  [:doc, :location, :res, :req, :rt, :params, :headers, :cookie, :form, :response_handler, :http_method, :exception].each do |n|
    instance_variable_set :"@#{n}", nil
  end
end
content_type?(guess) click to toggle source
# File lib/stella/client.rb, line 350
def content_type? guess
  guess = Regexp.new guess unless Regexp === guess
  guess.match((res.header['Content-Type'] || []).first)
end
cookies()
Alias for: cookie
current_event() click to toggle source
# File lib/stella/client.rb, line 241
def current_event
  @events.last
end
doc() click to toggle source
# File lib/stella/client.rb, line 296
def doc
  return @doc unless @doc.nil?
  return nil if @res.content.nil? || @res.content.empty?
  str = RUBY_VERSION >= "1.9.0" ? @res.content.force_encoding("UTF-8") : @res.content
  # NOTE: It's important to parse the document on every 
  # request because this container is available for the
  # entire life of a usecase. 
  @doc = case (@res.header['Content-Type'] || []).first
  when /text\/html/
    Nokogiri::HTML(str)
  when /text\/xml/
    Nokogiri::XML(str)
  when /text\/yaml/
    YAML.load(str)
  when /application\/json/
    Yajl::Parser.parse(str)
  end
  @doc.replace indifferent_params(@doc) if Hash === @doc
  @doc
end
error(msg=nil) click to toggle source
# File lib/stella/client.rb, line 391
def error(msg=nil); raise RequestError.new(msg); end
fail(msg=nil) click to toggle source
# File lib/stella/client.rb, line 390
def fail(msg=nil); raise UsecaseFail.new(msg); end
find_response_handler(status) click to toggle source
# File lib/stella/client.rb, line 359
def find_response_handler(status)
  return if response_handler.nil?
  key = response_handler.keys.select { |range| range.member?(status) }.first
  response_handler[key] if key
end
follow(uri=nil,&blk) click to toggle source
# File lib/stella/client.rb, line 393
def follow(uri=nil,&blk); raise ForcedRedirect.new(uri,&blk); end
form() click to toggle source
# File lib/stella/client.rb, line 316
def form
  return @form unless @form.nil?
  return nil if doc.nil?
  return nil unless content_type?('text/html')
  @form = indifferent_hash
  forms = doc.css('form')
  forms.each_with_index do |html,idx|
    name = html['id'] || html['name'] || html['class']
    Stella.ld [:form, idx, name].inspect
    form = indifferent_hash
    # Store form attributes in keys prefixed with an underscore.
    html.each { |att,val| form["_#{att}"] = val }
    # Store input name and values in the form hash.
    html.css('input').each do |input|
      form[input['name']] = input['value']
    end
    # Store the form by the name and index in the document
    @form[name] = @form[idx] = form
  end
  @form
end
Also aliased as: forms
forms()
Alias for: form
generate_request(event_id) click to toggle source
# File lib/stella/client.rb, line 283
def generate_request(event_id)
  @res = http_client.send(@http_method.to_s.downcase, @uri, params, headers)
  @req = @res.request
  @events << event_id
  @res
end
handle_response() click to toggle source
# File lib/stella/client.rb, line 354
def handle_response
  return unless response_handler?
  instance_exec(&find_response_handler(@res.status))
  @previous_doc = doc
end
location() click to toggle source
# File lib/stella/client.rb, line 289
def location
  @location ||= Addressable::URI.parse(@res.header['location'].first || '')
  @location
end
prepare_request(uc, rt) click to toggle source
# File lib/stella/client.rb, line 246
def prepare_request uc, rt
  clear_previous_request
  @rt = rt
  @vars.merge! uc.class.session || {}
  registered_classes = []
  if uc.class.testplan
    @vars.merge! uc.class.testplan.class.session || {} 
    registered_classes = uc.class.testplan.class.registered_classes || []
  end
  registered_classes.push *(uc.class.registered_classes || [])
  registered_classes.each do |klass| 
    self.extend klass unless self.kind_of?(klass)
  end
  @http_method, @params, @headers = rt.http_method, rt.params, rt.headers
  @http_auth = uc.http_auth
  instance_exec(&rt.callback) unless rt.callback.nil?
  @uri = if @redirect_uri
    @params = {}
    @headers = {}
    @http_method = :get
    if @redirect_uri.scheme
      tmp = [@redirect_uri.scheme, '://', @redirect_uri.host].join
      tmp << ":#{@redirect_uri.port}" unless [80,443].member?(@redirect_uri.port)
      @base_uri = Addressable::URI.parse(tmp)
    end
    build_uri @redirect_uri
  else
    build_uri @rt.uri
  end
  if !http_auth.nil? && !http_auth.empty?
    Stella.ld " HTTP AUTH: #{http_auth.inspect}"
    http_auth[:domain] ||= '%s://%s:%d%s' % [base_uri.scheme, base_uri.host, base_uri.port, '/'] 
    http_client.set_auth http_auth[:domain], http_auth[:user], http_auth[:pass]
    Stella.ld "   #{http_client.www_auth.inspect}"
  end
  @redirect_uri = nil  # one time deal
end
quit(msg=nil) click to toggle source
# File lib/stella/client.rb, line 389
def quit(msg=nil); raise TestplanQuit.new(msg); end
redirect?() click to toggle source
# File lib/stella/client.rb, line 293
def redirect?
  @res && (300..399).member?(@res.status)
end
repeat(t=1) click to toggle source
# File lib/stella/client.rb, line 392
def repeat(t=1); raise RepeatRequest.new(t); end
response_handler?() click to toggle source
# File lib/stella/client.rb, line 364
def response_handler?
  status = (@res.status || 0).to_i
  !find_response_handler(status).nil?
end
status() click to toggle source
# File lib/stella/client.rb, line 384
def status
  @res.status
end
wait(t) click to toggle source
# File lib/stella/client.rb, line 388
def wait(t); sleep t; end

Private Instance Methods

build_uri(reqtempl) click to toggle source
# File lib/stella/client.rb, line 396
def build_uri(reqtempl)
  uri = reqtempl.clone # need to clone b/c we modify uri in scan.
  reqtempl.to_s.scan(/([:\$])([a-z_]+)/i) do |inst|
    val = find_replacement_value(inst[1])
    Stella.ld " FOUND VAR: #{inst[0]}#{inst[1]} (value: #{val})"
    if val.nil?
      raise Stella::UsecaseError, "no value for #{inst[0]}#{inst[1]} in '#{@rt.uri}'"
    end
    re = Regexp.new "\\#{inst[0]}#{inst[1]}"
    uri.gsub! re, val.to_s unless val.nil?
  end
  uri = Stella.canonical_uri(uri)
  if base_uri
    uri.scheme = base_uri.scheme
    uri.host = base_uri.host if uri.host.to_s.empty?
    uri.port = base_uri.port if uri.port.to_s.empty?
  end
  uri
end
find_replacement_value(name) click to toggle source

Testplan URIs can contain variables in the form :varname. This method looks at the request parameters and then at the usecase's resource hash for a replacement value. If not found, returns nil.

# File lib/stella/client.rb, line 419
def find_replacement_value(name)
  if @params.has_key?(name.to_sym)
    @params.delete name.to_sym
  elsif vars.has_key?(name.to_s) || vars.has_key?(name.to_s.to_sym)
    vars[name.to_s] || vars[name.to_s.to_sym]
  elsif Stella::Testplan.global?(name)
    Stella::Testplan.global(name)
  end
end
indifferent_hash() click to toggle source

Creates a Hash with indifferent access.

# File lib/stella/client.rb, line 447
def indifferent_hash
  Hash.new {|hash,key| hash[key.to_s] if Symbol === key }
end
indifferent_params(params) click to toggle source
# File lib/stella/client.rb, line 428
def indifferent_params(params)
  if params.is_a?(Hash)
    params = indifferent_hash.merge(params)
    params.each do |key, value|
      next unless value.is_a?(Hash) || value.is_a?(Array)
      params[key] = indifferent_params(value)
    end
  elsif params.is_a?(Array)
    params.collect! do |value|
      if value.is_a?(Hash) || value.is_a?(Array)
        indifferent_params(value)
      else
        value
      end
    end
  end
end