Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 648 lines (566 sloc) 28.017 kb
1baeab7 Refactoring generators and adding routing logic
Brian Ploetz authored
1 require 'spec_helper'
2 require 'rspec/rails'
3
4 describe Versionist::Routing do
5 include RSpec::Rails::RequestExampleGroup
780022a Fixing multi-Rails test setup for Travis
Brian Ploetz authored
6
7 before :all do
8 ENV["RAILS_ENV"] = 'test'
9 require File.expand_path("../test-api/config/application", __FILE__)
10 TestApi::Application.initialize!
11 end
1baeab7 Refactoring generators and adding routing logic
Brian Ploetz authored
12
cb187bc Adding documentation to new api generator
Brian Ploetz authored
13 context "#api_version" do
a4daad6 Support for default version
Brian Ploetz authored
14 before :each do
c733cce Issue #3: Deal with route reloading
Brian Ploetz authored
15 Versionist.configuration.clear!
a4daad6 Support for default version
Brian Ploetz authored
16 TestApi::Application.routes.clear!
8cdeae2 Accept header routing specs
Brian Ploetz authored
17 end
18
cb187bc Adding documentation to new api generator
Brian Ploetz authored
19 it "should raise an error when config nil" do
20 lambda {
21 TestApi::Application.routes.draw do
7ad26cc Path versioning strategy and specs
Brian Ploetz authored
22 api_version(nil)
1baeab7 Refactoring generators and adding routing logic
Brian Ploetz authored
23 end
cb187bc Adding documentation to new api generator
Brian Ploetz authored
24 }.should raise_error(ArgumentError, /you must pass a configuration Hash to api_version/)
25 end
1baeab7 Refactoring generators and adding routing logic
Brian Ploetz authored
26
cb187bc Adding documentation to new api generator
Brian Ploetz authored
27 it "should raise an error when config is not a Hash" do
28 lambda {
1baeab7 Refactoring generators and adding routing logic
Brian Ploetz authored
29 TestApi::Application.routes.draw do
7ad26cc Path versioning strategy and specs
Brian Ploetz authored
30 api_version(1)
1baeab7 Refactoring generators and adding routing logic
Brian Ploetz authored
31 end
cb187bc Adding documentation to new api generator
Brian Ploetz authored
32 }.should raise_error(ArgumentError, /you must pass a configuration Hash to api_version/)
33 end
1baeab7 Refactoring generators and adding routing logic
Brian Ploetz authored
34
7ad26cc Path versioning strategy and specs
Brian Ploetz authored
35 it "should raise an error when config doesn't contain :module" do
36 lambda {
37 TestApi::Application.routes.draw do
38 api_version({})
39 end
40 }.should raise_error(ArgumentError, /you must specify :module in configuration Hash passed to api_version/)
41 end
42
cb187bc Adding documentation to new api generator
Brian Ploetz authored
43 it "should raise an error when config doesn't contain any supported strategies" do
44 lambda {
45 TestApi::Application.routes.draw do
7ad26cc Path versioning strategy and specs
Brian Ploetz authored
46 api_version({:module => "v1"})
1baeab7 Refactoring generators and adding routing logic
Brian Ploetz authored
47 end
cb187bc Adding documentation to new api generator
Brian Ploetz authored
48 }.should raise_error(ArgumentError, /you must specify :header, :path, or :parameter in configuration Hash passed to api_version/)
49 end
1baeab7 Refactoring generators and adding routing logic
Brian Ploetz authored
50
a66773a Issue #5: Support for Rails' :defaults hash
Brian Ploetz authored
51 it "should raise an error when config contains a :defaults key which isn't a Hash" do
52 lambda {
53 TestApi::Application.routes.draw do
54 api_version({:module => "v1", :header => "Accept", :value => "application/vnd.mycompany.com-v1", :defaults => 1}) do
55 end
56 end
57 }.should raise_error(ArgumentError, /:defaults must be a Hash/)
58 end
59
8cdeae2 Accept header routing specs
Brian Ploetz authored
60 it "should add the middleware" do
61 TestApi::Application.routes.draw do
7ad26cc Path versioning strategy and specs
Brian Ploetz authored
62 api_version({:module => "v1", :header => "Accept", :value => "application/vnd.mycompany.com-v1"}) do
8cdeae2 Accept header routing specs
Brian Ploetz authored
63 match '/foos.(:format)' => 'foos#index', :via => :get
64 match '/foos_no_format' => 'foos#index', :via => :get
65 resources :bars
66 end
67 match '/foos(:format)' => 'foos#index', :via => :get
68 end
69 TestApi::Application.config.middleware.should include(Versionist::Middleware)
70 end
71
68c0cab Fixing module names in specs. Adding custom header specs.
Brian Ploetz authored
72 {"v1" => "v1", "v2" => "v2", "v2.1" => "v2__1"}.each do |ver, mod|
a33e9ea Issue #4
Brian Ploetz authored
73 # Skip module names with underscores in Rails 3.2
74 # https://github.com/rails/rails/issues/5849
75 next if Rails::VERSION::MAJOR == 3 && Rails::VERSION::MINOR == 2 && mod.include?('_')
68c0cab Fixing module names in specs. Adding custom header specs.
Brian Ploetz authored
76 context ver do
8cdeae2 Accept header routing specs
Brian Ploetz authored
77 before :each do
68c0cab Fixing module names in specs. Adding custom header specs.
Brian Ploetz authored
78 @headers = Hash.new
79 end
80
81 context ":header" do
82 context "Accept" do
83 before :each do
84 TestApi::Application.routes.draw do
7ad26cc Path versioning strategy and specs
Brian Ploetz authored
85 api_version({:module => mod, :header => "Accept", :value => "application/vnd.mycompany.com-#{ver}"}) do
68c0cab Fixing module names in specs. Adding custom header specs.
Brian Ploetz authored
86 match '/foos.(:format)' => 'foos#index', :via => :get
87 match '/foos_no_format' => 'foos#index', :via => :get
88 resources :bars
89 end
90 match '/foos(:format)' => 'foos#index', :via => :get
91 match '*a', :to => 'application#not_found'
92 end
93 end
94
95 it "should not route when header isn't present" do
96 get "/foos.json", nil, @headers
97 assert_response 404
98 end
99
100 it "should not route when header doesn't match" do
101 @headers["HTTP_ACCEPT"] = "application/vnd.mycompany.com-v3"
102 get "/foos.json", nil, @headers
103 assert_response 404
104 end
105
106 it "should route to the correct controller when header matches" do
107 @headers["HTTP_ACCEPT"] = "application/vnd.mycompany.com-#{ver}"
108 get "/foos.json", nil, @headers
109 assert_response 200
110 assert_equal 'application/json', response.content_type
111 assert_equal ver, response.body
112
113 get "/foos.xml", nil, @headers
114 assert_response 200
115 assert_equal 'application/xml', response.content_type
116 assert_equal ver, response.body
117 end
118
119 it "should route to the correct controller when format specified via accept header" do
120 @headers["HTTP_ACCEPT"] = "application/vnd.mycompany.com-#{ver},application/json"
121 get "/foos_no_format", nil, @headers
122 assert_response 200
123 assert_equal 'application/json', response.content_type
124 assert_equal ver, response.body
125
126 @headers["HTTP_ACCEPT"] = "application/xml, application/vnd.mycompany.com-#{ver}"
127 get "/foos_no_format", nil, @headers
128 assert_response 200
129 assert_equal 'application/xml', response.content_type
130 assert_equal ver, response.body
131
132 @headers["HTTP_ACCEPT"] = "application/xml, application/vnd.mycompany.com-#{ver}, application/json"
133 get "/foos_no_format", nil, @headers
134 assert_response 200
135 assert_equal 'application/xml', response.content_type
136 assert_equal ver, response.body
8cdeae2 Accept header routing specs
Brian Ploetz authored
137 end
a4daad6 Support for default version
Brian Ploetz authored
138
139 context ":default => true" do
140 before :each do
141 TestApi::Application.routes.draw do
142 api_version({:module => mod, :header => "Accept", :value => "application/vnd.mycompany.com-#{ver}", :default => true}) do
143 match '/foos.(:format)' => 'foos#index', :via => :get
144 end
145 api_version({:module => "not_default", :header => "Accept", :value => "application/vnd.mycompany.com-not_default"}) do
146 match '/foos.(:format)' => 'foos#index', :via => :get
147 end
148 end
149 end
150
151 it "should route to the default when no version given" do
152 get "/foos.json", nil, @headers
153 assert_response 200
154 assert_equal 'application/json', response.content_type
155 assert_equal ver, response.body
156
157 @headers["HTTP_ACCEPT"] = ""
158 get "/foos.json", nil, @headers
159 assert_response 200
160 assert_equal 'application/json', response.content_type
161 assert_equal ver, response.body
162
163 @headers["HTTP_ACCEPT"] = " "
164 get "/foos.json", nil, @headers
165 assert_response 200
166 assert_equal 'application/json', response.content_type
167 assert_equal ver, response.body
168 end
169
170 it "should not route to the default when another configured version is given" do
171 @headers["HTTP_ACCEPT"] = "application/vnd.mycompany.com-not_default"
172 get "/foos.json", nil, @headers
173 assert_response 200
174 assert_equal 'application/json', response.content_type
175 assert_equal "not_default", response.body
176 end
177 end
a66773a Issue #5: Support for Rails' :defaults hash
Brian Ploetz authored
178
179 context ":defaults" do
180 it "should pass the :defaults hash on to the scope() call" do
181 ActionDispatch::Routing::Mapper.any_instance.should_receive(:scope).with(hash_including(:defaults => {:format => :json}))
182 TestApi::Application.routes.draw do
183 api_version({:module => mod, :header => "Accept", :value => "application/vnd.mycompany.com-#{ver}", :defaults => {:format => :json}}) do
184 match '/foos.(:format)' => 'foos#index', :via => :get
185 match '/foos_no_format' => 'foos#index', :via => :get
186 resources :bars
187 end
188 end
189 end
190 end
cb187bc Adding documentation to new api generator
Brian Ploetz authored
191 end
8cdeae2 Accept header routing specs
Brian Ploetz authored
192
5122579 Issue #1: Change all examples (and tests) for Accept header strategy to ...
Brian Ploetz authored
193 context "Accept with parameters" do
194 before :each do
195 TestApi::Application.routes.draw do
196 api_version({:module => mod, :header => "Accept", :value => "application/vnd.mycompany.com; version=#{ver}"}) do
197 match '/foos.(:format)' => 'foos#index', :via => :get
198 match '/foos_no_format' => 'foos#index', :via => :get
199 resources :bars
200 end
201 match '/foos(:format)' => 'foos#index', :via => :get
202 match '*a', :to => 'application#not_found'
203 end
204 end
205
206 it "should not route when header isn't present" do
207 get "/foos.json", nil, @headers
208 assert_response 404
209 end
210
211 it "should not route when header doesn't match" do
212 @headers["HTTP_ACCEPT"] = "application/vnd.mycompany.com; version=v3"
213 get "/foos.json", nil, @headers
214 assert_response 404
215 end
216
217 it "should route to the correct controller when header matches" do
218 @headers["HTTP_ACCEPT"] = "application/vnd.mycompany.com; version=#{ver}"
219 get "/foos.json", nil, @headers
220 assert_response 200
221 assert_equal 'application/json', response.content_type
222 assert_equal ver, response.body
223
224 get "/foos.xml", nil, @headers
225 assert_response 200
226 assert_equal 'application/xml', response.content_type
227 assert_equal ver, response.body
228 end
229
230 it "should route to the correct controller when format specified via accept header" do
231 @headers["HTTP_ACCEPT"] = "application/vnd.mycompany.com; version=#{ver},application/json"
232 get "/foos_no_format", nil, @headers
233 assert_response 200
234 assert_equal 'application/json', response.content_type
235 assert_equal ver, response.body
236
237 @headers["HTTP_ACCEPT"] = "application/xml, application/vnd.mycompany.com; version=#{ver}"
238 get "/foos_no_format", nil, @headers
239 assert_response 200
240 assert_equal 'application/xml', response.content_type
241 assert_equal ver, response.body
242
243 @headers["HTTP_ACCEPT"] = "application/xml, application/vnd.mycompany.com; version=#{ver}, application/json"
244 get "/foos_no_format", nil, @headers
245 assert_response 200
246 assert_equal 'application/xml', response.content_type
247 assert_equal ver, response.body
248 end
249
250 context ":default => true" do
251 before :each do
252 TestApi::Application.routes.draw do
253 api_version({:module => mod, :header => "Accept", :value => "application/vnd.mycompany.com; version=#{ver}", :default => true}) do
254 match '/foos.(:format)' => 'foos#index', :via => :get
255 end
256 api_version({:module => "not_default", :header => "Accept", :value => "application/vnd.mycompany.com; version=not_default"}) do
257 match '/foos.(:format)' => 'foos#index', :via => :get
258 end
259 end
260 end
261
262 it "should route to the default when no version given" do
263 get "/foos.json", nil, @headers
264 assert_response 200
265 assert_equal 'application/json', response.content_type
266 assert_equal ver, response.body
267
268 @headers["HTTP_ACCEPT"] = ""
269 get "/foos.json", nil, @headers
270 assert_response 200
271 assert_equal 'application/json', response.content_type
272 assert_equal ver, response.body
273
274 @headers["HTTP_ACCEPT"] = " "
275 get "/foos.json", nil, @headers
276 assert_response 200
277 assert_equal 'application/json', response.content_type
278 assert_equal ver, response.body
279 end
280
281 it "should not route to the default when another configured version is given" do
282 @headers["HTTP_ACCEPT"] = "application/vnd.mycompany.com; version=not_default"
283 get "/foos.json", nil, @headers
284 assert_response 200
285 assert_equal 'application/json', response.content_type
286 assert_equal "not_default", response.body
287 end
288 end
289
290 context ":defaults" do
291 it "should pass the :defaults hash on to the scope() call" do
292 ActionDispatch::Routing::Mapper.any_instance.should_receive(:scope).with(hash_including(:defaults => {:format => :json}))
293 TestApi::Application.routes.draw do
294 api_version({:module => mod, :header => "Accept", :value => "application/vnd.mycompany.com; version=#{ver}", :defaults => {:format => :json}}) do
295 match '/foos.(:format)' => 'foos#index', :via => :get
296 match '/foos_no_format' => 'foos#index', :via => :get
297 resources :bars
298 end
299 end
300 end
301 end
302 end
303
68c0cab Fixing module names in specs. Adding custom header specs.
Brian Ploetz authored
304 context "custom header" do
305 before :each do
306 TestApi::Application.routes.draw do
24cea58 Pull Request #1: Removing all occurrences of X-CUSTOM-HEADER from specs/...
Brian Ploetz authored
307 api_version({:module => mod, :header => "API-VERSION", :value => ver}) do
68c0cab Fixing module names in specs. Adding custom header specs.
Brian Ploetz authored
308 match '/foos.(:format)' => 'foos#index', :via => :get
309 match '/foos_no_format' => 'foos#index', :via => :get
310 resources :bars
311 end
312 match '/foos(:format)' => 'foos#index', :via => :get
313 match '*a', :to => 'application#not_found'
314 end
315 end
8cdeae2 Accept header routing specs
Brian Ploetz authored
316
68c0cab Fixing module names in specs. Adding custom header specs.
Brian Ploetz authored
317 it "should not route when header isn't present" do
318 get "/foos.json", nil, @headers
319 assert_response 404
320 end
321
322 it "should not route when header doesn't match" do
24cea58 Pull Request #1: Removing all occurrences of X-CUSTOM-HEADER from specs/...
Brian Ploetz authored
323 @headers["API_VERSION"] = "v3"
68c0cab Fixing module names in specs. Adding custom header specs.
Brian Ploetz authored
324 get "/foos.json", nil, @headers
325 assert_response 404
326 end
327
328 it "should route to the correct controller when header matches" do
24cea58 Pull Request #1: Removing all occurrences of X-CUSTOM-HEADER from specs/...
Brian Ploetz authored
329 @headers["HTTP_API_VERSION"] = ver
68c0cab Fixing module names in specs. Adding custom header specs.
Brian Ploetz authored
330 get "/foos.json", nil, @headers
331 assert_response 200
332 assert_equal 'application/json', response.content_type
333 assert_equal ver, response.body
334
335 get "/foos.xml", nil, @headers
336 assert_response 200
337 assert_equal 'application/xml', response.content_type
338 assert_equal ver, response.body
339 end
340
341 it "should route to the correct controller when format specified via accept header" do
342 @headers["HTTP_ACCEPT"] = "application/json,application/xml"
24cea58 Pull Request #1: Removing all occurrences of X-CUSTOM-HEADER from specs/...
Brian Ploetz authored
343 @headers["HTTP_API_VERSION"] = ver
68c0cab Fixing module names in specs. Adding custom header specs.
Brian Ploetz authored
344 get "/foos_no_format", nil, @headers
345 assert_response 200
346 assert_equal 'application/json', response.content_type
347 assert_equal ver, response.body
348
349 @headers["HTTP_ACCEPT"] = "application/xml,application/json"
24cea58 Pull Request #1: Removing all occurrences of X-CUSTOM-HEADER from specs/...
Brian Ploetz authored
350 @headers["HTTP_API_VERSION"] = ver
68c0cab Fixing module names in specs. Adding custom header specs.
Brian Ploetz authored
351 get "/foos_no_format", nil, @headers
352 assert_response 200
353 assert_equal 'application/xml', response.content_type
354 assert_equal ver, response.body
355 end
a4daad6 Support for default version
Brian Ploetz authored
356
357 context ":default => true" do
358 before :each do
359 TestApi::Application.routes.draw do
24cea58 Pull Request #1: Removing all occurrences of X-CUSTOM-HEADER from specs/...
Brian Ploetz authored
360 api_version({:module => mod, :header => "API-VERSION", :value => ver, :default => true}) do
a4daad6 Support for default version
Brian Ploetz authored
361 match '/foos.(:format)' => 'foos#index', :via => :get
362 end
24cea58 Pull Request #1: Removing all occurrences of X-CUSTOM-HEADER from specs/...
Brian Ploetz authored
363 api_version({:module => "not_default", :header => "API-VERSION", :value => "not_default"}) do
a4daad6 Support for default version
Brian Ploetz authored
364 match '/foos.(:format)' => 'foos#index', :via => :get
365 end
366 end
367 end
368
369 it "should route to the default when no version given" do
370 get "/foos.json", nil, @headers
371 assert_response 200
372 assert_equal 'application/json', response.content_type
373 assert_equal ver, response.body
374
24cea58 Pull Request #1: Removing all occurrences of X-CUSTOM-HEADER from specs/...
Brian Ploetz authored
375 @headers["HTTP_API_VERSION"] = ""
a4daad6 Support for default version
Brian Ploetz authored
376 get "/foos.json", nil, @headers
377 assert_response 200
378 assert_equal 'application/json', response.content_type
379 assert_equal ver, response.body
380
24cea58 Pull Request #1: Removing all occurrences of X-CUSTOM-HEADER from specs/...
Brian Ploetz authored
381 @headers["HTTP_API_VERSION"] = " "
a4daad6 Support for default version
Brian Ploetz authored
382 get "/foos.xml", nil, @headers
383 assert_response 200
384 assert_equal 'application/xml', response.content_type
385 assert_equal ver, response.body
386 end
387
388 it "should not route to the default when another configured version is given" do
24cea58 Pull Request #1: Removing all occurrences of X-CUSTOM-HEADER from specs/...
Brian Ploetz authored
389 @headers["HTTP_API_VERSION"] = "not_default"
a4daad6 Support for default version
Brian Ploetz authored
390 get "/foos.json", nil, @headers
391 assert_response 200
392 assert_equal 'application/json', response.content_type
393 assert_equal "not_default", response.body
394 end
395 end
a66773a Issue #5: Support for Rails' :defaults hash
Brian Ploetz authored
396
397 context ":defaults" do
398 it "should pass the :defaults hash on to the scope() call" do
399 ActionDispatch::Routing::Mapper.any_instance.should_receive(:scope).with(hash_including(:defaults => {:format => :json}))
400 TestApi::Application.routes.draw do
401 api_version({:module => mod, :header => "API-VERSION", :value => ver, :defaults => {:format => :json}}) do
402 match '/foos.(:format)' => 'foos#index', :via => :get
403 match '/foos_no_format' => 'foos#index', :via => :get
404 resources :bars
405 end
406 end
407 end
408 end
68c0cab Fixing module names in specs. Adding custom header specs.
Brian Ploetz authored
409 end
8cdeae2 Accept header routing specs
Brian Ploetz authored
410 end
411
68c0cab Fixing module names in specs. Adding custom header specs.
Brian Ploetz authored
412 context ":path" do
7ad26cc Path versioning strategy and specs
Brian Ploetz authored
413 before :each do
414 TestApi::Application.routes.draw do
415 api_version({:module => mod, :path => "/#{ver}"}) do
416 match '/foos.(:format)' => 'foos#index', :via => :get
417 match '/foos_no_format' => 'foos#index', :via => :get
418 resources :bars
419 end
420 match '/foos(:format)' => 'foos#index', :via => :get
421 match '*a', :to => 'application#not_found'
422 end
423 end
424
425 it "should not route when path isn't present" do
426 get "/foos.json", nil, @headers
427 assert_response 404
428 end
429
430 it "should not route when path doesn't match" do
431 get "/bogus/foos.json", nil, @headers
432 assert_response 404
433 end
434
435 it "should route to the correct controller when path matches" do
436 get "/#{ver}/foos.json", nil, @headers
437 assert_response 200
438 assert_equal 'application/json', response.content_type
439 assert_equal ver, response.body
440
1476593 Issue #24: Strip leading slash from :path if present
Brian Ploetz authored
441 get "/#{ver}/bars.json", nil, @headers
442 assert_response 200
443 assert_equal 'application/json', response.content_type
444 assert_equal ver, response.body
445
7ad26cc Path versioning strategy and specs
Brian Ploetz authored
446 get "/#{ver}/foos.xml", nil, @headers
447 assert_response 200
448 assert_equal 'application/xml', response.content_type
449 assert_equal ver, response.body
1476593 Issue #24: Strip leading slash from :path if present
Brian Ploetz authored
450
451 get "/#{ver}/bars.xml", nil, @headers
452 assert_response 200
453 assert_equal 'application/xml', response.content_type
454 assert_equal ver, response.body
7ad26cc Path versioning strategy and specs
Brian Ploetz authored
455 end
456
457 it "should route to the correct controller when path matches and format specified via accept header" do
458 @headers["HTTP_ACCEPT"] = "application/json,application/xml"
459 get "/#{ver}/foos_no_format", nil, @headers
460 assert_response 200
461 assert_equal 'application/json', response.content_type
462 assert_equal ver, response.body
463
464 @headers["HTTP_ACCEPT"] = "application/xml,application/json"
465 get "/#{ver}/foos_no_format", nil, @headers
466 assert_response 200
467 assert_equal 'application/xml', response.content_type
468 assert_equal ver, response.body
469 end
a4daad6 Support for default version
Brian Ploetz authored
470
471 context ":default => true" do
472 before :each do
473 TestApi::Application.routes.draw do
474 api_version({:module => mod, :path => "/#{ver}", :default => true}) do
475 match '/foos.(:format)' => 'foos#index', :via => :get
1476593 Issue #24: Strip leading slash from :path if present
Brian Ploetz authored
476 resources :bars
a4daad6 Support for default version
Brian Ploetz authored
477 end
478 api_version({:module => "not_default", :path => "/not_default"}) do
479 match '/foos.(:format)' => 'foos#index', :via => :get
1476593 Issue #24: Strip leading slash from :path if present
Brian Ploetz authored
480 resources :bars
a4daad6 Support for default version
Brian Ploetz authored
481 end
482 end
483 end
484
485 it "should route to the default when no version given" do
486 get "/foos.json", nil, @headers
487 assert_response 200
488 assert_equal 'application/json', response.content_type
489 assert_equal ver, response.body
490
1476593 Issue #24: Strip leading slash from :path if present
Brian Ploetz authored
491 get "/#{ver}/bars.json", nil, @headers
492 assert_response 200
493 assert_equal 'application/json', response.content_type
494 assert_equal ver, response.body
495
a4daad6 Support for default version
Brian Ploetz authored
496 get "/foos.xml", nil, @headers
497 assert_response 200
498 assert_equal 'application/xml', response.content_type
499 assert_equal ver, response.body
1476593 Issue #24: Strip leading slash from :path if present
Brian Ploetz authored
500
501 get "/#{ver}/bars.xml", nil, @headers
502 assert_response 200
503 assert_equal 'application/xml', response.content_type
504 assert_equal ver, response.body
a4daad6 Support for default version
Brian Ploetz authored
505 end
506
507 it "should not route to the default when another configured version is given" do
508 get "/not_default/foos.json", nil, @headers
509 assert_response 200
510 assert_equal 'application/json', response.content_type
511 assert_equal "not_default", response.body
512 end
513 end
a66773a Issue #5: Support for Rails' :defaults hash
Brian Ploetz authored
514
515 context ":defaults" do
516 it "should pass the :defaults hash on to the namespace() call" do
1476593 Issue #24: Strip leading slash from :path if present
Brian Ploetz authored
517 ActionDispatch::Routing::Mapper.any_instance.should_receive(:namespace).with("#{ver}", hash_including(:defaults => {:format => :json}))
a66773a Issue #5: Support for Rails' :defaults hash
Brian Ploetz authored
518 TestApi::Application.routes.draw do
519 api_version({:module => mod, :path => "/#{ver}", :defaults => {:format => :json}}) do
520 match '/foos.(:format)' => 'foos#index', :via => :get
521 match '/foos_no_format' => 'foos#index', :via => :get
522 resources :bars
523 end
524 end
525 end
526
527 it "should pass the :defaults hash on to the namespace() call and the scope() call when :default is present" do
1476593 Issue #24: Strip leading slash from :path if present
Brian Ploetz authored
528 ActionDispatch::Routing::Mapper.any_instance.should_receive(:namespace).with("#{ver}", hash_including(:defaults => {:format => :json}))
a66773a Issue #5: Support for Rails' :defaults hash
Brian Ploetz authored
529 ActionDispatch::Routing::Mapper.any_instance.should_receive(:scope).with(hash_including(:defaults => {:format => :json}))
530 TestApi::Application.routes.draw do
531 api_version({:module => mod, :path => "/#{ver}", :default => true, :defaults => {:format => :json}}) do
532 match '/foos.(:format)' => 'foos#index', :via => :get
533 match '/foos_no_format' => 'foos#index', :via => :get
534 resources :bars
535 end
536 end
537 end
538 end
8cdeae2 Accept header routing specs
Brian Ploetz authored
539 end
540
68c0cab Fixing module names in specs. Adding custom header specs.
Brian Ploetz authored
541 context ":parameter" do
5d71f7b Parameter versioning strategy and specs
Brian Ploetz authored
542 before :each do
543 TestApi::Application.routes.draw do
544 api_version({:module => mod, :parameter => "version", :value => ver}) do
545 match '/foos.(:format)' => 'foos#index', :via => :get
546 match '/foos_no_format' => 'foos#index', :via => :get
547 resources :bars
548 end
549 match '/foos(:format)' => 'foos#index', :via => :get
550 match '*a', :to => 'application#not_found'
551 end
552 end
553
554 it "should not route when parameter isn't present" do
555 get "/foos.json", nil, @headers
556 assert_response 404
557 end
558
559 it "should not route when parameter doesn't match" do
560 get "/foos.json?version=3", nil, @headers
561 assert_response 404
562 end
563
564 it "should route to the correct controller when parameter matches" do
565 get "/foos.json?version=#{ver}", nil, @headers
566 assert_response 200
567 assert_equal 'application/json', response.content_type
568 assert_equal ver, response.body
569
570 get "/foos.xml?version=#{ver}", nil, @headers
571 assert_response 200
572 assert_equal 'application/xml', response.content_type
573 assert_equal ver, response.body
574 end
575
576 it "should route to the correct controller when parameter matches and format specified via accept header" do
577 @headers["HTTP_ACCEPT"] = "application/json,application/xml"
578 get "/foos_no_format?version=#{ver}", nil, @headers
579 assert_response 200
580 assert_equal 'application/json', response.content_type
581 assert_equal ver, response.body
582
583 @headers["HTTP_ACCEPT"] = "application/xml,application/json"
584 get "/foos_no_format?version=#{ver}", nil, @headers
585 assert_response 200
586 assert_equal 'application/xml', response.content_type
587 assert_equal ver, response.body
588 end
a4daad6 Support for default version
Brian Ploetz authored
589
590 context ":default => true" do
591 before :each do
592 TestApi::Application.routes.draw do
593 api_version({:module => mod, :parameter => "version", :value => ver, :default => true}) do
594 match '/foos.(:format)' => 'foos#index', :via => :get
595 end
596 api_version({:module => "not_default", :parameter => "version", :value => "not_default"}) do
597 match '/foos.(:format)' => 'foos#index', :via => :get
598 end
599 end
600 end
601
602 it "should route to the default when no version given" do
603 get "/foos.json", nil, @headers
604 assert_response 200
605 assert_equal 'application/json', response.content_type
606 assert_equal ver, response.body
607
608 get "/foos.json?version=", nil, @headers
609 assert_response 200
610 assert_equal 'application/json', response.content_type
611 assert_equal ver, response.body
612 end
613
614 it "should not route to the default when another configured version is given" do
615 get "/foos.json?version=not_default", nil, @headers
616 assert_response 200
617 assert_equal 'application/json', response.content_type
618 assert_equal "not_default", response.body
619 end
620 end
a66773a Issue #5: Support for Rails' :defaults hash
Brian Ploetz authored
621
622 context ":defaults" do
623 it "should pass the :defaults hash on to the scope() call" do
624 ActionDispatch::Routing::Mapper.any_instance.should_receive(:scope).with(hash_including(:defaults => {:format => :json}))
625 TestApi::Application.routes.draw do
626 api_version({:module => mod, :parameter => "version", :value => ver, :defaults => {:format => :json}}) do
627 match '/foos.(:format)' => 'foos#index', :via => :get
628 match '/foos_no_format' => 'foos#index', :via => :get
629 resources :bars
630 end
631 end
632 end
633 end
8cdeae2 Accept header routing specs
Brian Ploetz authored
634 end
1baeab7 Refactoring generators and adding routing logic
Brian Ploetz authored
635 end
cb187bc Adding documentation to new api generator
Brian Ploetz authored
636 end
637 end
c733cce Issue #3: Deal with route reloading
Brian Ploetz authored
638
639 context "route reloading" do
4747f38 Issue #21: Change configuration to be tolerant of route reloading
Brian Ploetz authored
640 it "should handle Rails.application.reload_routes!" do
c733cce Issue #3: Deal with route reloading
Brian Ploetz authored
641 lambda {
4747f38 Issue #21: Change configuration to be tolerant of route reloading
Brian Ploetz authored
642 Rails.application.reload_routes!
c733cce Issue #3: Deal with route reloading
Brian Ploetz authored
643 Rails.application.reload_routes!
644 }.should_not raise_error
645 end
646 end
1baeab7 Refactoring generators and adding routing logic
Brian Ploetz authored
647 end
Something went wrong with that request. Please try again.