@@ -516,9 +516,15 @@ class DummyFormatClass
516
516
end
517
517
send ( verb , '/example' )
518
518
expect ( last_response . body ) . to eql verb == 'head' ? '' : verb
519
- # Call it with a method other than the properly constrained one.
520
- send ( used_verb = verbs [ ( verbs . index ( verb ) + 2 ) % verbs . size ] , '/example' )
521
- expect ( last_response . status ) . to eql used_verb == 'options' ? 204 : 405
519
+ # Call it with all methods other than the properly constrained one.
520
+ ( verbs - [ verb ] ) . each do |other_verb |
521
+ send ( other_verb , '/example' )
522
+ expected_rc = if other_verb == 'options' then 204
523
+ elsif other_verb == 'head' && verb == 'get' then 200
524
+ else 405
525
+ end
526
+ expect ( last_response . status ) . to eql expected_rc
527
+ end
522
528
end
523
529
end
524
530
@@ -549,6 +555,7 @@ class DummyFormatClass
549
555
before_validation { raise 'before_validation filter should not run' }
550
556
after_validation { raise 'after_validation filter should not run' }
551
557
after { raise 'after filter should not run' }
558
+ params { requires :only_for_get }
552
559
get
553
560
end
554
561
@@ -573,6 +580,26 @@ class DummyFormatClass
573
580
expect ( last_response . headers [ 'X-Custom-Header' ] ) . to eql 'foo'
574
581
end
575
582
583
+ it 'runs all filters and body with a custom OPTIONS method' do
584
+ subject . namespace :example do
585
+ before { header 'X-Custom-Header-1' , 'foo' }
586
+ before_validation { header 'X-Custom-Header-2' , 'foo' }
587
+ after_validation { header 'X-Custom-Header-3' , 'foo' }
588
+ after { header 'X-Custom-Header-4' , 'foo' }
589
+ options { 'yup' }
590
+ get
591
+ end
592
+
593
+ options '/example'
594
+ expect ( last_response . status ) . to eql 200
595
+ expect ( last_response . body ) . to eql 'yup'
596
+ expect ( last_response . headers [ 'Allow' ] ) . to be_nil
597
+ expect ( last_response . headers [ 'X-Custom-Header-1' ] ) . to eql 'foo'
598
+ expect ( last_response . headers [ 'X-Custom-Header-2' ] ) . to eql 'foo'
599
+ expect ( last_response . headers [ 'X-Custom-Header-3' ] ) . to eql 'foo'
600
+ expect ( last_response . headers [ 'X-Custom-Header-4' ] ) . to eql 'foo'
601
+ end
602
+
576
603
context 'when format is xml' do
577
604
it 'returns a 405 for an unsupported method' do
578
605
subject . format :xml
@@ -594,8 +621,8 @@ class DummyFormatClass
594
621
context 'when accessing env' do
595
622
it 'returns a 405 for an unsupported method' do
596
623
subject . before do
597
- _custom_header_1 = headers [ 'X-Custom-Header' ]
598
- _custom_header_2 = env [ 'HTTP_X_CUSTOM_HEADER' ]
624
+ _customheader1 = headers [ 'X-Custom-Header' ]
625
+ _customheader2 = env [ 'HTTP_X_CUSTOM_HEADER' ]
599
626
end
600
627
subject . get 'example' do
601
628
'example'
@@ -630,7 +657,11 @@ class DummyFormatClass
630
657
631
658
describe 'adds an OPTIONS route that' do
632
659
before do
633
- subject . before { header 'X-Custom-Header' , 'foo' }
660
+ subject . before { header 'X-Custom-Header' , 'foo' }
661
+ subject . before_validation { header 'X-Custom-Header-2' , 'bar' }
662
+ subject . after_validation { header 'X-Custom-Header-3' , 'baz' }
663
+ subject . after { header 'X-Custom-Header-4' , 'bing' }
664
+ subject . params { requires :only_for_get }
634
665
subject . get 'example' do
635
666
'example'
636
667
end
@@ -652,10 +683,22 @@ class DummyFormatClass
652
683
expect ( last_response . headers [ 'Allow' ] ) . to eql 'OPTIONS, GET, HEAD'
653
684
end
654
685
655
- it 'has a X-Custom-Header ' do
686
+ it 'calls before hook ' do
656
687
expect ( last_response . headers [ 'X-Custom-Header' ] ) . to eql 'foo'
657
688
end
658
689
690
+ it 'does not call before_validation hook' do
691
+ expect ( last_response . headers . key? ( 'X-Custom-Header-2' ) ) . to be false
692
+ end
693
+
694
+ it 'does not call after_validation hook' do
695
+ expect ( last_response . headers . key? ( 'X-Custom-Header-3' ) ) . to be false
696
+ end
697
+
698
+ it 'calls after hook' do
699
+ expect ( last_response . headers [ 'X-Custom-Header-4' ] ) . to eq 'bing'
700
+ end
701
+
659
702
it 'has no Content-Type' do
660
703
expect ( last_response . content_type ) . to be_nil
661
704
end
@@ -2555,13 +2598,11 @@ def static
2555
2598
params : {
2556
2599
'param1' => { required : true } ,
2557
2600
'param2' => { required : false }
2558
- }
2559
- } ,
2601
+ } } ,
2560
2602
{ description : 'global description' ,
2561
2603
params : {
2562
2604
'param2' => { required : false }
2563
- }
2564
- }
2605
+ } }
2565
2606
]
2566
2607
end
2567
2608
it 'merges the parameters of the namespace with the parameters of the method' do
@@ -2585,8 +2626,7 @@ def static
2585
2626
params : {
2586
2627
'ns_param' => { required : true , desc : 'namespace parameter' } ,
2587
2628
'method_param' => { required : false , desc : 'method parameter' }
2588
- }
2589
- }
2629
+ } }
2590
2630
]
2591
2631
end
2592
2632
it 'merges the parameters of nested namespaces' do
@@ -2618,8 +2658,7 @@ def static
2618
2658
'ns1_param' => { required : true , desc : 'ns1 param' } ,
2619
2659
'ns2_param' => { required : true , desc : 'ns2 param' } ,
2620
2660
'method_param' => { required : false , desc : 'method param' }
2621
- }
2622
- }
2661
+ } }
2623
2662
]
2624
2663
end
2625
2664
it 'groups nested params and prevents overwriting of params with same name in different groups' do
@@ -2662,8 +2701,7 @@ def static
2662
2701
'root_param' => { required : true , desc : 'root param' } ,
2663
2702
'nested' => { required : true , type : 'Array' } ,
2664
2703
'nested[nested_param]' => { required : true , desc : 'nested param' }
2665
- }
2666
- }
2704
+ } }
2667
2705
]
2668
2706
end
2669
2707
it 'allows to set the type attribute on :group element' do
0 commit comments