Sha256: 90193162a432364cae5cddcc843c0940d8087e6db59ba8feb9aa70fe6c107222

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

require 'ostruct'

require 'test/unit'
require 'test/unit/assertions'
require 'rexml/document'

require 'glue'
require 'nitro/test/context'

module Test::Unit
  
class TestCase
  include Nitro

  def reset_context
    @context_config = OpenStruct.new(
      :dispatcher => Nitro::Dispatcher.new(Nitro::Server.map)
    )
    @context = Nitro::Context.new(@context_config)
  end

  # Send a request to the controller. Alternatively you can use
  # the request method helpers (get, post, ...)
  #
  # === Options
  #
  # :uri, :method, :headers/:env, :params, :session
  
  def process(options = {})
    unless options.is_a? Hash
      options = { :uri => options.to_s }
    end

    uri = options[:uri]
    uri = "/#{uri}" unless uri =~ /^\//
   
    reset_context unless @context
    context = @context
    if @last_response_cookies
      @last_response_cookies.each do |cookie|
        context.cookies.merge! cookie.name => cookie.value
      end
    end
    context.params = options[:params] || {}
    context.headers = options[:headers] || options[:env] || {}
    context.headers['REQUEST_URI'] = uri
    context.headers['REQUEST_METHOD'] = options[:method].to_s.upcase
    context.headers['REMOTE_ADDR'] ||= '127.0.0.1'
    if ((:get == options[:method]) and (options[:params]))
      context.headers['QUERY_STRING'] = options[:params].collect {|k,v| "#{k}=#{v}"}.join('&')
    end
    context.cookies.merge! options[:cookies] if options[:cookies]
    context.session.merge! options[:session] if options[:session]
    
    context.render(context.path)
    @last_response_cookies = context.response_cookies
    return context.body
  end

  #--
  # Compile some helpers.
  #++
  
  for m in [:get, :post, :put, :delete, :head]
    eval %{
      def #{m}(options = {})
        unless options.is_a? Hash
          options = { :uri => options.to_s }
        end
        options[:method] = :#{m}
        process(options)
      end 
    }
  end

end

end

# * George Moschovitis <gm@navel.gr>

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nitro-0.31.0 lib/nitro/test/testcase.rb