public
Description: master merb branch
Homepage: http://www.merbivore.com
Clone URL: git://github.com/wycats/merb.git
merb / merb-core / spec / public / test / mock_request_helper_spec.rb
100644 270 lines (214 sloc) 9.543 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
require File.expand_path(File.join(File.dirname(__FILE__), "spec_helper"))
 
Merb.start :environment => 'test', :log_level => :fatal
 
Dir[File.join(File.dirname(__FILE__), "controllers/**/*.rb")].each do |f|
  require f
end
 
describe Merb::Test::RequestHelper do
  
  describe Merb::Test::RequestHelper::CookieJar do
    
    it "should update its values from a request object" do
      cookie_jar = Merb::Test::RequestHelper::CookieJar.new
      cookie_jar.should be_empty
      request = fake_request
      request.cookies[:foo] = "bar+baz" # escaped by default
      cookie_jar.update_from_request request
      cookie_jar[:foo].should == 'bar baz'
    end
    
  end
  
  describe "#dispatch_to" do
 
    before(:all) do
      @controller_klass = Merb::Test::DispatchController
    end
 
    it "should dispatch to the given controller and action" do
      Merb::Test::ControllerAssertionMock.should_receive(:called).with(:index)
 
      dispatch_to(@controller_klass, :index)
    end
 
    it "should dispatch to the given controller and action with params" do
      Merb::Test::ControllerAssertionMock.should_receive(:called).with(:show)
 
      controller = dispatch_to(@controller_klass, :show, :name => "Fred")
      controller.params[:name].should == "Fred"
    end
 
    it "should dispatch to the given controller and action with the query string merged into the params" do
      Merb::Test::ControllerAssertionMock.should_receive(:called).with(:show)
      controller = dispatch_to(@controller_klass, :show, {:name => "Fred"}, {'QUERY_STRING' => "last_name=Jones&age=42"} )
      
      controller.params[:name].should == "Fred"
      controller.params[:last_name].should == "Jones"
      controller.params[:age].should == "42"
    end
 
    it "should not hit the router to match its route" do
      Merb::Router.should_not_receive(:match)
      dispatch_to(@controller_klass, :index)
    end
    
    it "merges :controller into params" do
      controller = dispatch_to(@controller_klass, :show, :name => "Fred")
      
      controller.params[:controller].should == @controller_klass.name.to_const_path
    end
    
    it "merges :action into params" do
      controller = dispatch_to(@controller_klass, :show, :name => "Fred")
      
      controller.params[:action].should == "show"
    end
 
    it "should support setting request.raw_post" do
      controller = dispatch_to(@controller_klass, :show, {}, {:post_body => 'some XML'})
      controller.request.raw_post.should == 'some XML'
    end
  end
  
  describe "#dispatch_with_basic_authentication_to" do
 
    before(:all) do
      @controller_klass = Merb::Test::DispatchController
    end
 
    it "should dispatch to the given controller and action" do
      Merb::Test::ControllerAssertionMock.should_receive(:called).with(:index)
 
      dispatch_with_basic_authentication_to(@controller_klass, :index, "Fred", "secret")
    end
 
    it "should dispatch to the given controller and action with authentication token" do
      Merb::Test::ControllerAssertionMock.should_receive(:called).with(:show)
 
      controller = dispatch_with_basic_authentication_to(@controller_klass, :show, "Fred", "secret")
 
      controller.request.env["X_HTTP_AUTHORIZATION"].should == "Basic #{Base64.encode64("Fred:secret")}"
    end
    
    it "should dispatch to the given controller and action with authentication token and params" do
      Merb::Test::ControllerAssertionMock.should_receive(:called).with(:show)
 
      controller = dispatch_with_basic_authentication_to(@controller_klass, :show, "Fred", "secret", :name => "Fred")
 
      controller.request.env["X_HTTP_AUTHORIZATION"].should == "Basic #{Base64.encode64("Fred:secret")}"
      controller.params[:name].should == "Fred"
    end
 
    it "should not hit the router to match its route" do
      Merb::Router.should_not_receive(:match)
      dispatch_with_basic_authentication_to(@controller_klass, :index, "Fred", "secret")
    end
  end
 
  describe "#get" do
    before(:each) do
      Merb::Router.prepare do
        resources :spec_helper_controller
        match("/:controller/:action/:custom").to(:controller => ":controller")
      end
    end
 
    it "should perform the index action when used with a get" do
      Merb::Test::ControllerAssertionMock.should_receive(:called).with(:index)
      get("/spec_helper_controller")
    end
 
    it "should perform the index action and have params available" do
      Merb::Test::ControllerAssertionMock.should_receive(:called).with(:index)
      controller = get("/spec_helper_controller", :name => "Harry")
      controller.params[:name].should == "Harry"
    end
    
    it "should perform the index action and have params available from the query string" do
      Merb::Test::ControllerAssertionMock.should_receive(:called).with(:index)
      controller = get("/spec_helper_controller?last_name=Oswald&age=25", :name => "Harry")
      controller.params[:name].should == "Harry"
      controller.params[:last_name].should == "Oswald"
      controller.params[:age].should == "25"
    end
 
    it "should evaluate in the context of the controller in the block" do
      get("/spec_helper_controller") do |controller|
        controller.class.should == SpecHelperController
      end
    end
 
    it "should allow for custom router params" do
      controller = get("/spec_helper_controller/index/my_custom_stuff")
      controller.params[:custom].should == "my_custom_stuff"
    end
 
    it "should get the show action" do
      Merb::Test::ControllerAssertionMock.should_receive(:called).with(:show)
      controller = get("/spec_helper_controller/my_id")
      controller.params[:id].should == "my_id"
    end
  end
 
  describe "#post" do
    before(:each) do
      Merb::Router.prepare do
        resources :spec_helper_controller
      end
    end
 
    it "should post to the create action" do
      Merb::Test::ControllerAssertionMock.should_receive(:called).with(:create)
      post("/spec_helper_controller")
    end
 
    it "should post to the create action with params" do
      Merb::Test::ControllerAssertionMock.should_receive(:called).with(:create)
      controller = post("/spec_helper_controller", :name => "Harry")
      controller.params[:name].should == "Harry"
    end
  end
 
  describe "#put" do
    before(:each) do
      Merb::Router.prepare do
        resources :spec_helper_controller
      end
    end
    it "should put to the update action" do
      Merb::Test::ControllerAssertionMock.should_receive(:called).with(:update)
      put("/spec_helper_controller/1")
    end
 
    it "should put to the update action with params" do
      Merb::Test::ControllerAssertionMock.should_receive(:called).with(:update)
      controller = put("/spec_helper_controller/my_id", :name => "Harry")
      controller.params[:name].should == "Harry"
      controller.params[:id].should == "my_id"
    end
  end
 
  describe "#delete" do
    before(:each) do
      Merb::Router.prepare do
        resources :spec_helper_controller
      end
    end
    it "should put to the update action" do
      Merb::Test::ControllerAssertionMock.should_receive(:called).with(:destroy)
      delete("/spec_helper_controller/1")
    end
 
    it "should put to the update action with params" do
      Merb::Test::ControllerAssertionMock.should_receive(:called).with(:destroy)
      controller = delete("/spec_helper_controller/my_id", :name => "Harry")
      controller.params[:name].should == "Harry"
      controller.params[:id].should == "my_id"
    end
  end
  
  describe "#request" do
    before(:each) do
      Merb::Router.prepare do
        namespace :namespaced do |namespaced|
          namespaced.resources :spec_helper_controller
        end
      end
    end
    
    it "should support setting request.raw_post" do
      controller = mock_request("/namespaced/spec_helper_controller", {}, {:post_body => 'some XML'})
      controller.request.raw_post.should == 'some XML'
    end
 
    it "should get namespaced index action" do
      Merb::Test::ControllerAssertionMock.should_receive(:called).with(:index)
      controller = mock_request("/namespaced/spec_helper_controller")
      controller.class.should == Namespaced::SpecHelperController
    end
 
    it "should make the post body available in the request on deferred routing" do
      Merb::Router.prepare do
        match('/xmlrpc').defer_to do |request, params|
          request.raw_post.should == 'XMLRPC request body'
          {:controller => 'spec_helper_controller', :action => :index}
        end
      end
 
      mock_request('/xmlrpc', {}, {:post_body => 'XMLRPC request body'})
    end
  end
  
end
 
module Merb::Test::RequestHelper
  describe FakeRequest, ".new(env = {}, req = StringIO.new)" do
    it "should create request with default enviroment, minus rack.input" do
      @mock = FakeRequest.new
      @mock.env.except('rack.input').should == FakeRequest::DEFAULT_ENV
    end
 
    it "should override default env values passed in HTTP format" do
      @mock = FakeRequest.new('HTTP_ACCEPT' => 'nothing')
      @mock.env['HTTP_ACCEPT'].should == 'nothing'
    end
 
    it "should override default env values passed in symbol format" do
      @mock = FakeRequest.new(:http_accept => 'nothing')
      @mock.env['HTTP_ACCEPT'].should == 'nothing'
    end
 
    it "should set rack input to an empty StringIO" do
      @mock = FakeRequest.new
      @mock.env['rack.input'].should be_kind_of(StringIO)
      @mock.env['rack.input'].read.should == ''
    end
  end
end