Sha256: b67caa106762d6d9b3587d0d0dbba5e7117325cf5787d290ad049253e33e859b

Contents?: true

Size: 1.94 KB

Versions: 6

Compression:

Stored size: 1.94 KB

Contents

require 'test/support/fake_request'

class FakeSinatraCall

  # Mimic's the context that is accessible in a Sinatra' route. Should provide
  # any methods needed to replace using an actual Sinatra app.

  attr_accessor :request, :response, :params, :logger, :router, :session
  attr_accessor :settings

  def initialize(settings = nil)
    @request         = FakeRequest.new
    @response        = FakeResponse.new
    @session         = @request.session
    @params          = @request.params
    @logger          = Deas::NullLogger.new
    @router          = Deas::Router.new
    @template_source = Deas::NullTemplateSource.new

    @content_type = nil
    @status       = 200
    @headers      = {}

    @settings = OpenStruct.new({
      :deas_server_data => Factory.server_data
    }.merge(settings || {}))
  end

  def call(env)
    [@response.status, @response.headers, [@response.body]]
  end

  def halt(*args)
    throw :halt, args
  end

  def redirect(*args)
    halt 302, { 'Location' => args[0] }
  end

  def content_type(*args)
    return @content_type if args.empty?
    opts, value = [
      args.last.kind_of?(Hash) ? args.pop : {},
      args.last
    ]
    opts_value = opts.keys.map{ |k| "#{k}=#{opts[k]}" }.join(';')
    @content_type = [value, opts_value].reject{ |v| v.to_s.empty? }.join(';')
  end

  def status(*args)
    return @status if args.empty?
    @status = args.last
  end

  def headers(*args)
    return @headers if args.empty?
    @headers = args.last
  end

  def send_file(file_path, opts, &block)
    if block
      SendFileArgs.new(file_path, opts, block.call)
    else
      SendFileArgs.new(file_path, opts, nil)
    end
  end
  SendFileArgs = Struct.new(:file_path, :options, :block_call_result)

end

class FakeResponse < Struct.new(:status, :headers, :body)
  def initialize(args = nil)
    args ||= {}
    super(*[
      args[:status]  || Factory.integer,
      args[:headers] || {},
      args[:body]    || [Factory.text]
    ])
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
deas-0.42.0 test/support/fake_sinatra_call.rb
deas-0.41.0 test/support/fake_sinatra_call.rb
deas-0.40.0 test/support/fake_sinatra_call.rb
deas-0.39.2 test/support/fake_sinatra_call.rb
deas-0.39.1 test/support/fake_sinatra_call.rb
deas-0.39.0 test/support/fake_sinatra_call.rb