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:
Session testing made easy

Just say:

get '/foo' do
  session[:bar] # => 'baz'
end

get_it '/foo', :session => session(:bar => 'baz')
bmizerany (author)
Sun Apr 20 18:03:41 -0700 2008
commit  9c875ffda0d9d12bd54f83606b30601b1b9b6bf7
tree    e3a98979546ea1bee9bf8a4dbebbc04bd3db7875
parent  f80203adb6bc5756bdcf2388c937891c756db5a6
...
52
53
54
55
 
56
57
58
...
810
811
812
813
 
814
815
816
...
52
53
54
 
55
56
57
58
...
810
811
812
 
813
814
815
816
0
@@ -52,7 +52,7 @@
0
     end
0
 
0
     def user_agent
0
- env['HTTP_USER_AGENT']
0
+ @env['HTTP_USER_AGENT']
0
     end
0
 
0
     private
0
@@ -810,7 +810,7 @@
0
     end
0
     
0
     def session
0
- @request.env['rack.session'] || {}
0
+ request.env['rack.session'] ||= {}
0
     end
0
     
0
     private
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
...
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
...
 
 
 
 
 
 
 
 
 
 
1
2
3
4
...
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
0
@@ -1,13 +1,3 @@
0
-class Rack::MockRequest
0
- class << self
0
- alias :env_for_without_env :env_for
0
- def env_for(uri = "", opts = {})
0
- env = { 'HTTP_USER_AGENT' => opts.delete(:agent) }
0
- env_for_without_env(uri, opts).merge(env)
0
- end
0
- end
0
-end
0
-
0
 module Sinatra
0
   
0
   module Test
0
0
@@ -16,12 +6,19 @@
0
 
0
       def easy_env_map
0
         {
0
- :accept => 'HTTP_ACCEPT',
0
- :agent => 'HTTP_AGENT',
0
- :host => 'HTTP_POST'
0
+ :accept => "HTTP_ACCEPT",
0
+ :agent => "HTTP_USER_AGENT",
0
+ :host => "HTTP_HOST",
0
+ :session => "HTTP_COOKIE",
0
+ :cookies => "HTTP_COOKIE"
0
         }
0
       end
0
-
0
+
0
+ def session(data, key = 'rack.session')
0
+ data = data.from_params if data.respond_to?(:from_params)
0
+ "#{Rack::Utils.escape(key)}=#{[Marshal.dump(data)].pack("m*")}"
0
+ end
0
+
0
       def map_easys(params)
0
         easy_env_map.inject(params.dup) do |m, (from, to)|
0
           m[to] = m.delete(from) if m.has_key?(from); m
0
0
0
@@ -30,14 +27,14 @@
0
 
0
       %w(get head post put delete).each do |m|
0
         define_method("#{m}_it") do |path, *args|
0
- request = Rack::MockRequest.new(Sinatra.build_application)
0
           env, input = if args.size == 2
0
             [args.last, args.first]
0
           elsif args.size == 1
0
             data = args.first
0
- data.is_a?(Hash) ? [data.delete(:env), data.to_params] : [nil, data]
0
+ data.is_a?(Hash) ? [map_easys(data.delete(:env) || {}), data.to_params] : [nil, data]
0
           end
0
- @response = request.request(m.upcase, path, {:input => input}.merge(env || {}))
0
+ @request = Rack::MockRequest.new(Sinatra.build_application)
0
+ @response = @request.request(m.upcase, path, {:input => input}.merge(env || {}))
0
         end
0
       end
0
       
...
135
136
137
138
 
139
140
141
142
143
 
144
145
146
147
148
 
149
150
151
...
135
136
137
 
138
139
140
141
142
 
143
144
145
146
147
 
148
149
150
151
0
@@ -135,17 +135,17 @@
0
     get '/', :agent => /Windows/ do
0
       request.env['HTTP_USER_AGENT']
0
     end
0
-
0
+
0
     get_it '/', :env => { :agent => 'Windows' }
0
     should.be.ok
0
     body.should.equal 'Windows'
0
 
0
- get_it '/', :agent => 'Mac'
0
+ get_it '/', :env => { :agent => 'Mac' }
0
     should.not.be.ok
0
 
0
   end
0
 
0
- specify "can filters by agent" do
0
+ specify "can use regex to get parts of user-agent" do
0
     
0
     get '/', :agent => /Windows (NT)/ do
0
       params[:agent].first

Comments

    No one has commented yet.