forked from ruby-grape/grape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpoint_spec.rb
1129 lines (976 loc) · 34.5 KB
/
endpoint_spec.rb
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require 'spec_helper'
describe Grape::Endpoint do
subject { Class.new(Grape::API) }
def app
subject
end
describe '.before_each' do
after { Grape::Endpoint.before_each(nil) }
it 'is settable via block' do
block = ->(_endpoint) { 'noop' }
Grape::Endpoint.before_each(&block)
expect(Grape::Endpoint.before_each.first).to eq(block)
end
it 'is settable via reference' do
block = ->(_endpoint) { 'noop' }
Grape::Endpoint.before_each block
expect(Grape::Endpoint.before_each.first).to eq(block)
end
it 'is able to override a helper' do
subject.get('/') { current_user }
expect { get '/' }.to raise_error(NameError)
Grape::Endpoint.before_each do |endpoint|
allow(endpoint).to receive(:current_user).and_return('Bob')
end
get '/'
expect(last_response.body).to eq('Bob')
Grape::Endpoint.before_each(nil)
expect { get '/' }.to raise_error(NameError)
end
it 'is able to stack helper' do
subject.get('/') do
authenticate_user!
current_user
end
expect { get '/' }.to raise_error(NameError)
Grape::Endpoint.before_each do |endpoint|
allow(endpoint).to receive(:current_user).and_return('Bob')
end
Grape::Endpoint.before_each do |endpoint|
allow(endpoint).to receive(:authenticate_user!).and_return(true)
end
get '/'
expect(last_response.body).to eq('Bob')
Grape::Endpoint.before_each(nil)
expect { get '/' }.to raise_error(NameError)
end
end
describe '#initialize' do
it 'takes a settings stack, options, and a block' do
p = proc {}
expect do
Grape::Endpoint.new(Grape::Util::InheritableSetting.new, {
path: '/',
method: :get
}, &p)
end.not_to raise_error
end
end
it 'sets itself in the env upon call' do
subject.get('/') { 'Hello world.' }
get '/'
expect(last_request.env['api.endpoint']).to be_kind_of(Grape::Endpoint)
end
describe '#status' do
it 'is callable from within a block' do
subject.get('/home') do
status 206
'Hello'
end
get '/home'
expect(last_response.status).to eq(206)
expect(last_response.body).to eq('Hello')
end
it 'is set as default to 200 for get' do
memoized_status = nil
subject.get('/home') do
memoized_status = status
'Hello'
end
get '/home'
expect(last_response.status).to eq(200)
expect(memoized_status).to eq(200)
expect(last_response.body).to eq('Hello')
end
it 'is set as default to 201 for post' do
memoized_status = nil
subject.post('/home') do
memoized_status = status
'Hello'
end
post '/home'
expect(last_response.status).to eq(201)
expect(memoized_status).to eq(201)
expect(last_response.body).to eq('Hello')
end
end
describe '#header' do
it 'is callable from within a block' do
subject.get('/hey') do
header 'X-Awesome', 'true'
'Awesome'
end
get '/hey'
expect(last_response.headers['X-Awesome']).to eq('true')
end
end
describe '#headers' do
before do
subject.get('/headers') do
headers.to_json
end
end
it 'includes request headers' do
get '/headers'
expect(JSON.parse(last_response.body)).to eq(
'Host' => 'example.org',
'Cookie' => ''
)
end
it 'includes additional request headers' do
get '/headers', nil, 'HTTP_X_GRAPE_CLIENT' => '1'
expect(JSON.parse(last_response.body)['X-Grape-Client']).to eq('1')
end
it 'includes headers passed as symbols' do
env = Rack::MockRequest.env_for('/headers')
env['HTTP_SYMBOL_HEADER'.to_sym] = 'Goliath passes symbols'
body = subject.call(env)[2].body.first
expect(JSON.parse(body)['Symbol-Header']).to eq('Goliath passes symbols')
end
end
describe '#cookies' do
it 'is callable from within a block' do
subject.get('/get/cookies') do
cookies['my-awesome-cookie1'] = 'is cool'
cookies['my-awesome-cookie2'] = {
value: 'is cool too',
domain: 'my.example.com',
path: '/',
secure: true
}
cookies[:cookie3] = 'symbol'
cookies['cookie4'] = 'secret code here'
end
get('/get/cookies')
expect(last_response.headers['Set-Cookie'].split("\n").sort).to eql [
'cookie3=symbol',
'cookie4=secret+code+here',
'my-awesome-cookie1=is+cool',
'my-awesome-cookie2=is+cool+too; domain=my.example.com; path=/; secure'
]
end
it 'sets browser cookies and does not set response cookies' do
subject.get('/username') do
cookies[:username]
end
get('/username', {}, 'HTTP_COOKIE' => 'username=mrplum; sandbox=true')
expect(last_response.body).to eq('mrplum')
expect(last_response.headers['Set-Cookie']).to be_nil
end
it 'sets and update browser cookies' do
subject.get('/username') do
cookies[:sandbox] = true if cookies[:sandbox] == 'false'
cookies[:username] += '_test'
end
get('/username', {}, 'HTTP_COOKIE' => 'username=user; sandbox=false')
expect(last_response.body).to eq('user_test')
expect(last_response.headers['Set-Cookie']).to match(/username=user_test/)
expect(last_response.headers['Set-Cookie']).to match(/sandbox=true/)
end
it 'deletes cookie' do
subject.get('/test') do
sum = 0
cookies.each do |name, val|
sum += val.to_i
cookies.delete name
end
sum
end
get '/test', {}, 'HTTP_COOKIE' => 'delete_this_cookie=1; and_this=2'
expect(last_response.body).to eq('3')
cookies = Hash[last_response.headers['Set-Cookie'].split("\n").map do |set_cookie|
cookie = CookieJar::Cookie.from_set_cookie 'http://localhost/test', set_cookie
[cookie.name, cookie]
end]
expect(cookies.size).to eq(2)
%w(and_this delete_this_cookie).each do |cookie_name|
cookie = cookies[cookie_name]
expect(cookie).not_to be_nil
expect(cookie.value).to eq('deleted')
expect(cookie.expired?).to be true
end
end
it 'deletes cookies with path' do
subject.get('/test') do
sum = 0
cookies.each do |name, val|
sum += val.to_i
cookies.delete name, path: '/test'
end
sum
end
get('/test', {}, 'HTTP_COOKIE' => 'delete_this_cookie=1; and_this=2')
expect(last_response.body).to eq('3')
cookies = Hash[last_response.headers['Set-Cookie'].split("\n").map do |set_cookie|
cookie = CookieJar::Cookie.from_set_cookie 'http://localhost/test', set_cookie
[cookie.name, cookie]
end]
expect(cookies.size).to eq(2)
%w(and_this delete_this_cookie).each do |cookie_name|
cookie = cookies[cookie_name]
expect(cookie).not_to be_nil
expect(cookie.value).to eq('deleted')
expect(cookie.path).to eq('/test')
expect(cookie.expired?).to be true
end
end
end
describe '#declared' do
before do
subject.params do
requires :first
optional :second
optional :third, default: 'third-default'
optional :nested, type: Hash do
optional :fourth
end
end
end
it 'does not work in a before filter' do
subject.before do
declared(params)
end
subject.get('/declared') { declared(params) }
expect { get('/declared') }.to raise_error(
Grape::DSL::InsideRoute::MethodNotYetAvailable)
end
it 'has as many keys as there are declared params' do
inner_params = nil
subject.get '/declared' do
inner_params = declared(params).keys
''
end
get '/declared?first=present'
expect(last_response.status).to eq(200)
expect(inner_params.size).to eq(4)
end
it 'has a optional param with default value all the time' do
inner_params = nil
subject.get '/declared' do
inner_params = declared(params)
''
end
get '/declared?first=one'
expect(last_response.status).to eq(200)
expect(inner_params[:third]).to eql('third-default')
end
it 'builds nested params' do
inner_params = nil
subject.get '/declared' do
inner_params = declared(params)
''
end
get '/declared?first=present&nested[fourth]=1'
expect(last_response.status).to eq(200)
expect(inner_params[:nested].keys.size).to eq 1
end
it 'builds nested params when given array' do
subject.get '/dummy' do
end
subject.params do
requires :first
optional :second
optional :third, default: 'third-default'
optional :nested, type: Array do
optional :fourth
end
end
inner_params = nil
subject.get '/declared' do
inner_params = declared(params)
''
end
get '/declared?first=present&nested[][fourth]=1&nested[][fourth]=2'
expect(last_response.status).to eq(200)
expect(inner_params[:nested].size).to eq 2
end
it 'builds nested params' do
inner_params = nil
subject.get '/declared' do
inner_params = declared(params)
''
end
get '/declared?first=present&nested[fourth]=1'
expect(last_response.status).to eq(200)
expect(inner_params[:nested].keys.size).to eq 1
end
context 'sets nested array when the param is missing' do
it 'to be array when include_missing is true' do
inner_params = nil
subject.get '/declared' do
inner_params = declared(params, include_missing: true)
''
end
get '/declared?first=present'
expect(last_response.status).to eq(200)
expect(inner_params[:nested]).to be_a(Array)
end
it 'to be nil when include_missing is false' do
inner_params = nil
subject.get '/declared' do
inner_params = declared(params, include_missing: false)
''
end
get '/declared?first=present'
expect(last_response.status).to eq(200)
expect(inner_params[:nested]).to be_nil
end
end
it 'filters out any additional params that are given' do
inner_params = nil
subject.get '/declared' do
inner_params = declared(params)
''
end
get '/declared?first=one&other=two'
expect(last_response.status).to eq(200)
expect(inner_params.key?(:other)).to eq false
end
it 'stringifies if that option is passed' do
inner_params = nil
subject.get '/declared' do
inner_params = declared(params, stringify: true)
''
end
get '/declared?first=one&other=two'
expect(last_response.status).to eq(200)
expect(inner_params['first']).to eq 'one'
end
it 'does not include missing attributes if that option is passed' do
subject.get '/declared' do
error! 400, 'expected nil' if declared(params, include_missing: false)[:second]
''
end
get '/declared?first=one&other=two'
expect(last_response.status).to eq(200)
end
it 'includes attributes with value that evaluates to false' do
subject.params do
requires :first
optional :boolean
end
subject.post '/declared' do
error!('expected false', 400) if declared(params, include_missing: false)[:boolean] != false
''
end
post '/declared', MultiJson.dump(first: 'one', boolean: false), 'CONTENT_TYPE' => 'application/json'
expect(last_response.status).to eq(201)
end
it 'includes attributes with value that evaluates to nil' do
subject.params do
requires :first
optional :second
end
subject.post '/declared' do
error!('expected nil', 400) unless declared(params, include_missing: false)[:second].nil?
''
end
post '/declared', MultiJson.dump(first: 'one', second: nil), 'CONTENT_TYPE' => 'application/json'
expect(last_response.status).to eq(201)
end
it 'does not include missing attributes when there are nested hashes' do
subject.get '/dummy' do
end
subject.params do
requires :first
optional :second
optional :third, default: nil
optional :nested, type: Hash do
optional :fourth, default: nil
optional :fifth, default: nil
requires :nested_nested, type: Hash do
optional :sixth, default: 'sixth-default'
optional :seven, default: nil
end
end
end
inner_params = nil
subject.get '/declared' do
inner_params = declared(params, include_missing: false)
''
end
get '/declared?first=present&nested[fourth]=&nested[nested_nested][sixth]=sixth'
expect(last_response.status).to eq(200)
expect(inner_params[:first]).to eq 'present'
expect(inner_params[:nested].keys).to eq %w(fourth fifth nested_nested)
expect(inner_params[:nested][:fourth]).to eq ''
expect(inner_params[:nested][:nested_nested].keys).to eq %w(sixth seven)
expect(inner_params[:nested][:nested_nested][:sixth]).to eq 'sixth'
end
end
describe '#declared; call from child namespace' do
before do
subject.format :json
subject.namespace :something do
params do
requires :id, type: Integer
end
resource ':id' do
params do
requires :foo
optional :bar
end
get do
{
params: params,
declared_params: declared(params)
}
end
params do
requires :happy
optional :days
end
get '/test' do
{
params: params,
declared_params: declared(params, include_parent_namespaces: false)
}
end
end
end
end
it 'should include params defined in the parent namespace' do
get '/something/123', foo: 'test', extra: 'hello'
expect(last_response.status).to eq 200
json = JSON.parse(last_response.body, symbolize_names: true)
expect(json[:params][:id]).to eq 123
expect(json[:declared_params].keys).to match_array [:foo, :bar, :id]
end
it 'does not include params defined in the parent namespace with include_parent_namespaces: false' do
get '/something/123/test', happy: 'test', extra: 'hello'
expect(last_response.status).to eq 200
json = JSON.parse(last_response.body, symbolize_names: true)
expect(json[:params][:id]).to eq 123
expect(json[:declared_params].keys).to match_array [:happy, :days]
end
end
describe '#params' do
it 'is available to the caller' do
subject.get('/hey') do
params[:howdy]
end
get '/hey?howdy=hey'
expect(last_response.body).to eq('hey')
end
it 'parses from path segments' do
subject.get('/hey/:id') do
params[:id]
end
get '/hey/12'
expect(last_response.body).to eq('12')
end
it 'deeply converts nested params' do
subject.get '/location' do
params[:location][:city]
end
get '/location?location[city]=Dallas'
expect(last_response.body).to eq('Dallas')
end
context 'with special requirements' do
it 'parses email param with provided requirements for params' do
subject.get('/:person_email', requirements: { person_email: /.*/ }) do
params[:person_email]
end
get '/someone@example.com'
expect(last_response.body).to eq('someone@example.com')
get 'someone@example.com.pl'
expect(last_response.body).to eq('someone@example.com.pl')
end
it 'parses many params with provided regexps' do
subject.get('/:person_email/test/:number', requirements: { person_email: /someone@(.*).com/, number: /[0-9]/ }) do
params[:person_email] << params[:number]
end
get '/someone@example.com/test/1'
expect(last_response.body).to eq('someone@example.com1')
get '/someone@testing.wrong/test/1'
expect(last_response.status).to eq(404)
get 'someone@test.com/test/wrong_number'
expect(last_response.status).to eq(404)
get 'someone@test.com/wrong_middle/1'
expect(last_response.status).to eq(404)
end
context 'namespace requirements' do
before :each do
subject.namespace :outer, requirements: { person_email: /abc@(.*).com/ } do
get('/:person_email') do
params[:person_email]
end
namespace :inner, requirements: { number: /[0-9]/, person_email: /someone@(.*).com/ } do
get '/:person_email/test/:number' do
params[:person_email] << params[:number]
end
end
end
end
it 'parse email param with provided requirements for params' do
get '/outer/abc@example.com'
expect(last_response.body).to eq('abc@example.com')
end
it "should override outer namespace's requirements" do
get '/outer/inner/someone@testing.wrong/test/1'
expect(last_response.status).to eq(404)
get '/outer/inner/someone@testing.com/test/1'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('someone@testing.com1')
end
end
end
context 'from body parameters' do
before(:each) do
subject.post '/request_body' do
params[:user]
end
subject.put '/request_body' do
params[:user]
end
end
it 'converts JSON bodies to params' do
post '/request_body', MultiJson.dump(user: 'Bobby T.'), 'CONTENT_TYPE' => 'application/json'
expect(last_response.body).to eq('Bobby T.')
end
it 'does not convert empty JSON bodies to params' do
put '/request_body', '', 'CONTENT_TYPE' => 'application/json'
expect(last_response.body).to eq('')
end
it 'converts XML bodies to params' do
post '/request_body', '<user>Bobby T.</user>', 'CONTENT_TYPE' => 'application/xml'
expect(last_response.body).to eq('Bobby T.')
end
it 'converts XML bodies to params' do
put '/request_body', '<user>Bobby T.</user>', 'CONTENT_TYPE' => 'application/xml'
expect(last_response.body).to eq('Bobby T.')
end
it 'does not include parameters not defined by the body' do
subject.post '/omitted_params' do
error! 400, 'expected nil' if params[:version]
params[:user]
end
post '/omitted_params', MultiJson.dump(user: 'Bob'), 'CONTENT_TYPE' => 'application/json'
expect(last_response.status).to eq(201)
expect(last_response.body).to eq('Bob')
end
end
it 'responds with a 406 for an unsupported content-type' do
subject.format :json
# subject.content_type :json, "application/json"
subject.put '/request_body' do
params[:user]
end
put '/request_body', '<user>Bobby T.</user>', 'CONTENT_TYPE' => 'application/xml'
expect(last_response.status).to eq(406)
expect(last_response.body).to eq('{"error":"The requested content-type \'application/xml\' is not supported."}')
end
context 'content type with params' do
before do
subject.format :json
subject.content_type :json, 'application/json; charset=utf-8'
subject.post do
params[:data]
end
post '/', MultiJson.dump(data: { some: 'payload' }), 'CONTENT_TYPE' => 'application/json'
end
it 'should not response with 406 for same type without params' do
expect(last_response.status).not_to be 406
end
it 'should response with given content type in headers' do
expect(last_response.headers['Content-Type']).to eq 'application/json; charset=utf-8'
end
end
context 'precedence' do
before do
subject.format :json
subject.namespace '/:id' do
get do
{
params: params[:id]
}
end
post do
{
params: params[:id]
}
end
put do
{
params: params[:id]
}
end
end
end
it 'route string params have higher precedence than body params' do
post '/123', { id: 456 }.to_json
expect(JSON.parse(last_response.body)['params']).to eq '123'
put '/123', { id: 456 }.to_json
expect(JSON.parse(last_response.body)['params']).to eq '123'
end
it 'route string params have higher precedence than URL params' do
get '/123?id=456'
expect(JSON.parse(last_response.body)['params']).to eq '123'
post '/123?id=456'
expect(JSON.parse(last_response.body)['params']).to eq '123'
end
end
end
describe '#error!' do
it 'accepts a message' do
subject.get('/hey') do
error! 'This is not valid.'
'This is valid.'
end
get '/hey'
expect(last_response.status).to eq(500)
expect(last_response.body).to eq('This is not valid.')
end
it 'accepts a code' do
subject.get('/hey') do
error! 'Unauthorized.', 401
end
get '/hey'
expect(last_response.status).to eq(401)
expect(last_response.body).to eq('Unauthorized.')
end
it 'accepts an object and render it in format' do
subject.get '/hey' do
error!({ 'dude' => 'rad' }, 403)
end
get '/hey.json'
expect(last_response.status).to eq(403)
expect(last_response.body).to eq('{"dude":"rad"}')
end
it 'accepts a frozen object' do
subject.get '/hey' do
error!({ 'dude' => 'rad' }.freeze, 403)
end
get '/hey.json'
expect(last_response.status).to eq(403)
expect(last_response.body).to eq('{"dude":"rad"}')
end
it 'can specifiy headers' do
subject.get '/hey' do
error!({ 'dude' => 'rad' }, 403, 'X-Custom' => 'value')
end
get '/hey.json'
expect(last_response.status).to eq(403)
expect(last_response.headers['X-Custom']).to eq('value')
end
it 'sets the status code for the endpoint' do
memoized_endpoint = nil
subject.get '/hey' do
memoized_endpoint = self
error!({ 'dude' => 'rad' }, 403, 'X-Custom' => 'value')
end
get '/hey.json'
expect(memoized_endpoint.status).to eq(403)
end
end
describe '#redirect' do
it 'redirects to a url with status 302' do
subject.get('/hey') do
redirect '/ha'
end
get '/hey'
expect(last_response.status).to eq 302
expect(last_response.headers['Location']).to eq '/ha'
expect(last_response.body).to eq 'This resource has been moved temporarily to /ha.'
end
it 'has status code 303 if it is not get request and it is http 1.1' do
subject.post('/hey') do
redirect '/ha'
end
post '/hey', {}, 'HTTP_VERSION' => 'HTTP/1.1'
expect(last_response.status).to eq 303
expect(last_response.headers['Location']).to eq '/ha'
expect(last_response.body).to eq 'An alternate resource is located at /ha.'
end
it 'support permanent redirect' do
subject.get('/hey') do
redirect '/ha', permanent: true
end
get '/hey'
expect(last_response.status).to eq 301
expect(last_response.headers['Location']).to eq '/ha'
expect(last_response.body).to eq 'This resource has been moved permanently to /ha.'
end
it 'allows for an optional redirect body override' do
subject.get('/hey') do
redirect '/ha', body: 'test body'
end
get '/hey'
expect(last_response.body).to eq 'test body'
end
end
it 'does not persist params between calls' do
subject.post('/new') do
params[:text]
end
post '/new', text: 'abc'
expect(last_response.body).to eq('abc')
post '/new', text: 'def'
expect(last_response.body).to eq('def')
end
it 'resets all instance variables (except block) between calls' do
subject.helpers do
def memoized
@memoized ||= params[:howdy]
end
end
subject.get('/hello') do
memoized
end
get '/hello?howdy=hey'
expect(last_response.body).to eq('hey')
get '/hello?howdy=yo'
expect(last_response.body).to eq('yo')
end
it 'allows explicit return calls' do
subject.get('/home') do
return 'Hello'
end
get '/home'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('Hello')
end
describe '.generate_api_method' do
it 'raises NameError if the method name is already in use' do
expect do
Grape::Endpoint.generate_api_method('version', &proc {})
end.to raise_error(NameError)
end
it 'raises ArgumentError if a block is not given' do
expect do
Grape::Endpoint.generate_api_method('GET without a block method')
end.to raise_error(ArgumentError)
end
it 'returns a Proc' do
expect(Grape::Endpoint.generate_api_method('GET test for a proc', &proc {})).to be_a Proc
end
end
context 'filters' do
describe 'before filters' do
it 'runs the before filter if set' do
subject.before { env['before_test'] = 'OK' }
subject.get('/before_test') { env['before_test'] }
get '/before_test'
expect(last_response.body).to eq('OK')
end
end
describe 'after filters' do
it 'overrides the response body if it sets it' do
subject.after { body 'after' }
subject.get('/after_test') { 'during' }
get '/after_test'
expect(last_response.body).to eq('after')
end
it 'does not override the response body with its return' do
subject.after { 'after' }
subject.get('/after_test') { 'body' }
get '/after_test'
expect(last_response.body).to eq('body')
end
end
it 'allows adding to response with present' do
subject.format :json
subject.before { present :before, 'before' }
subject.before_validation { present :before_validation, 'before_validation' }
subject.after_validation { present :after_validation, 'after_validation' }
subject.after { present :after, 'after' }
subject.get :all_filters do
present :endpoint, 'endpoint'
end
get '/all_filters'
json = JSON.parse(last_response.body)
expect(json.keys).to match_array %w(before before_validation after_validation endpoint after)
end
context 'when terminating the response with error!' do
it 'breaks normal call chain' do
called = []
subject.before { called << 'before' }
subject.before_validation { called << 'before_validation' }
subject.after_validation { error! :oops, 500 }
subject.after { called << 'after' }
subject.get :error_filters do
called << 'endpoint'
''
end
get '/error_filters'
expect(last_response.status).to eql 500
expect(called).to match_array %w(before before_validation)
end
it 'allows prior and parent filters of same type to run' do
called = []
subject.before { called << 'parent' }
subject.namespace :parent do
before { called << 'prior' }
before { error! :oops, 500 }
before { called << 'subsequent' }
get :hello do
called << :endpoint
'Hello!'
end
end
get '/parent/hello'
expect(last_response.status).to eql 500
expect(called).to match_array %w(parent prior)
end
end
end
context 'anchoring' do
verbs = %w(post get head delete put options patch)
verbs.each do |verb|
it "allows for the anchoring option with a #{verb.upcase} method" do
subject.send(verb, '/example', anchor: true) do
verb
end
send(verb, '/example/and/some/more')
expect(last_response.status).to eql 404
end
it "anchors paths by default for the #{verb.upcase} method" do
subject.send(verb, '/example') do
verb
end
send(verb, '/example/and/some/more')
expect(last_response.status).to eql 404
end
it "responds to /example/and/some/more for the non-anchored #{verb.upcase} method" do
subject.send(verb, '/example', anchor: false) do
verb
end
send(verb, '/example/and/some/more')
expect(last_response.status).to eql verb == 'post' ? 201 : 200
expect(last_response.body).to eql verb == 'head' ? '' : verb
end
end
end
context 'request' do
it 'is set to the url requested' do
subject.get('/url') do
request.url
end
get '/url'
expect(last_response.body).to eq('http://example.org/url')
end
['v1', :v1].each do |version|
it "should include version #{version}" do
subject.version version, using: :path
subject.get('/url') do
request.url
end
get "/#{version}/url"
expect(last_response.body).to eq("http://example.org/#{version}/url")
end