public
Fork of bmizerany/sinatra
Description: Classy web-development dressed in a DSL
Homepage: http://sinatrarb.com
Clone URL: git://github.com/JackDanger/sinatra.git
Search Repo:
bmizerany (author)
Sun Apr 20 18:03:41 -0700 2008
commit  9c875ffda0d9d12bd54f83606b30601b1b9b6bf7
tree    e3a98979546ea1bee9bf8a4dbebbc04bd3db7875
parent  f80203adb6bc5756bdcf2388c937891c756db5a6
sinatra / lib / sinatra / test / methods.rb
100644 54 lines (42 sloc) 1.395 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
module Sinatra
  
  module Test
    
    module Methods
 
      def easy_env_map
        {
          :accept => "HTTP_ACCEPT",
          :agent => "HTTP_USER_AGENT",
          :host => "HTTP_HOST",
          :session => "HTTP_COOKIE",
          :cookies => "HTTP_COOKIE"
        }
      end
      
      def session(data, key = 'rack.session')
        data = data.from_params if data.respond_to?(:from_params)
        "#{Rack::Utils.escape(key)}=#{[Marshal.dump(data)].pack("m*")}"
      end
          
      def map_easys(params)
        easy_env_map.inject(params.dup) do |m, (from, to)|
          m[to] = m.delete(from) if m.has_key?(from); m
        end
      end
 
      %w(get head post put delete).each do |m|
        define_method("#{m}_it") do |path, *args|
          env, input = if args.size == 2
            [args.last, args.first]
          elsif args.size == 1
            data = args.first
            data.is_a?(Hash) ? [map_easys(data.delete(:env) || {}), data.to_params] : [nil, data]
          end
          @request = Rack::MockRequest.new(Sinatra.build_application)
          @response = @request.request(m.upcase, path, {:input => input}.merge(env || {}))
        end
      end
      
      def follow!
        get_it(@response.location)
      end
 
      def method_missing(name, *args)
        @response.send(name, *args) rescue super
      end
      
    end
 
  end
  
end