-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
RpcSpec.hs
1468 lines (1310 loc) · 64.1 KB
/
RpcSpec.hs
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
module Feature.Query.RpcSpec where
import qualified Data.ByteString.Lazy as BL (empty)
import Network.Wai (Application)
import Network.Wai.Test (SResponse (simpleBody, simpleHeaders, simpleStatus))
import Network.HTTP.Types
import Test.Hspec hiding (pendingWith)
import Test.Hspec.Wai
import Test.Hspec.Wai.JSON
import Text.Heredoc
import PostgREST.Config.PgVersion (PgVersion, pgVersion100,
pgVersion109, pgVersion110,
pgVersion112, pgVersion114)
import Protolude hiding (get)
import SpecHelper
spec :: PgVersion -> SpecWith ((), Application)
spec actualPgVersion =
describe "remote procedure call" $ do
context "a proc that returns a set" $ do
context "returns paginated results" $ do
it "using the Range header" $
request methodGet "/rpc/getitemrange?min=2&max=4"
(rangeHdrs (ByteRangeFromTo 1 1)) mempty
`shouldRespondWith` [json| [{"id":4}] |]
{ matchStatus = 200
, matchHeaders = ["Content-Range" <:> "1-1/*"]
}
it "using limit and offset" $ do
post "/rpc/getitemrange?limit=1&offset=1" [json| { "min": 2, "max": 4 } |]
`shouldRespondWith` [json| [{"id":4}] |]
{ matchStatus = 200
, matchHeaders = ["Content-Range" <:> "1-1/*"]
}
get "/rpc/getitemrange?min=2&max=4&limit=1&offset=1"
`shouldRespondWith` [json| [{"id":4}] |]
{ matchStatus = 200
, matchHeaders = ["Content-Range" <:> "1-1/*"]
}
request methodHead "/rpc/getitemrange?min=2&max=4&limit=1&offset=1" mempty mempty
`shouldRespondWith`
""
{ matchStatus = 200
, matchHeaders = [ matchContentTypeJson
, "Content-Range" <:> "1-1/*" ]
}
context "includes total count if requested" $ do
it "using the Range header" $
request methodGet "/rpc/getitemrange?min=2&max=4"
(rangeHdrsWithCount (ByteRangeFromTo 1 1)) ""
`shouldRespondWith` [json| [{"id":4}] |]
{ matchStatus = 206 -- it now knows the response is partial
, matchHeaders = ["Content-Range" <:> "1-1/2"]
}
it "using limit and offset" $ do
request methodPost "/rpc/getitemrange?limit=1&offset=1"
[("Prefer", "count=exact")]
[json| { "min": 2, "max": 4 } |]
`shouldRespondWith` [json| [{"id":4}] |]
{ matchStatus = 206 -- it now knows the response is partial
, matchHeaders = ["Content-Range" <:> "1-1/2"]
}
request methodGet "/rpc/getitemrange?min=2&max=4&limit=1&offset=1"
[("Prefer", "count=exact")] mempty
`shouldRespondWith` [json| [{"id":4}] |]
{ matchStatus = 206
, matchHeaders = ["Content-Range" <:> "1-1/2"]
}
request methodHead "/rpc/getitemrange?min=2&max=4&limit=1&offset=1"
[("Prefer", "count=exact")] mempty
`shouldRespondWith`
""
{ matchStatus = 206
, matchHeaders = [ matchContentTypeJson
, "Content-Range" <:> "1-1/2" ]
}
it "includes exact count if requested" $ do
request methodHead "/rpc/getallprojects"
[("Prefer", "count=exact")] ""
`shouldRespondWith` ""
{ matchStatus = 200
, matchHeaders = ["Content-Range" <:> "0-4/5"]
}
request methodHead "/rpc/getallprojects?select=*,clients!inner(*)&clients.id=eq.1"
[("Prefer", "count=exact")] ""
`shouldRespondWith` ""
{ matchStatus = 200
, matchHeaders = ["Content-Range" <:> "0-1/2"]
}
it "includes exact count of 1 for functions that return a single scalar, domain or composite" $ do
request methodGet "/rpc/add_them?a=3&b=4"
[("Prefer", "count=exact")] ""
`shouldRespondWith` "7"
{ matchStatus = 200
, matchHeaders = ["Content-Range" <:> "0-0/1"]
}
request methodGet "/rpc/ret_domain?val=8"
[("Prefer", "count=exact")] ""
`shouldRespondWith` "8"
{ matchStatus = 200
, matchHeaders = ["Content-Range" <:> "0-0/1"]
}
request methodGet "/rpc/ret_point_2d"
[("Prefer", "count=exact")] ""
`shouldRespondWith`
[json|{"x": 10, "y": 5}|]
{ matchStatus = 200
, matchHeaders = ["Content-Range" <:> "0-0/1"]
}
it "returns proper json" $ do
post "/rpc/getitemrange" [json| { "min": 2, "max": 4 } |] `shouldRespondWith`
[json| [ {"id": 3}, {"id":4} ] |]
{ matchHeaders = [matchContentTypeJson] }
get "/rpc/getitemrange?min=2&max=4" `shouldRespondWith`
[json| [ {"id": 3}, {"id":4} ] |]
{ matchHeaders = [matchContentTypeJson] }
it "returns CSV" $ do
request methodPost "/rpc/getitemrange"
(acceptHdrs "text/csv")
[json| { "min": 2, "max": 4 } |]
`shouldRespondWith` "id\n3\n4"
{ matchStatus = 200
, matchHeaders = ["Content-Type" <:> "text/csv; charset=utf-8"]
}
request methodGet "/rpc/getitemrange?min=2&max=4"
(acceptHdrs "text/csv") ""
`shouldRespondWith` "id\n3\n4"
{ matchStatus = 200
, matchHeaders = ["Content-Type" <:> "text/csv; charset=utf-8"]
}
request methodHead "/rpc/getitemrange?min=2&max=4"
(acceptHdrs "text/csv") ""
`shouldRespondWith`
""
{ matchStatus = 200
, matchHeaders = ["Content-Type" <:> "text/csv; charset=utf-8"]
}
context "ignores Range header when method is different than GET" $ do
it "without limit and offset" $ do
request methodPost "/rpc/getitemrange"
(rangeHdrsWithCount (ByteRangeFromTo 1 1))
[json| { "min": 2, "max": 4 } |]
`shouldRespondWith` [json| [{"id": 3}, {"id": 4}] |]
{ matchStatus = 200
, matchHeaders = ["Content-Range" <:> "0-1/2"]
}
request methodHead "/rpc/getitemrange?min=2&max=4"
(rangeHdrsWithCount (ByteRangeFromTo 1 1)) ""
`shouldRespondWith`
""
{ matchStatus = 200
, matchHeaders = [ matchContentTypeJson
, "Content-Range" <:> "0-1/2" ]
}
it "with limit and offset" $ do
request methodPost "/rpc/getitemrange?limit=2&offset=1"
(rangeHdrsWithCount (ByteRangeFromTo 1 1))
[json| { "min": 2, "max": 5 } |]
`shouldRespondWith` [json| [{"id": 4}, {"id": 5}] |]
{ matchStatus = 206
, matchHeaders = ["Content-Range" <:> "1-2/3"]
}
request methodHead "/rpc/getitemrange?min=2&max=5&limit=2&offset=1"
(rangeHdrsWithCount (ByteRangeFromTo 1 1)) ""
`shouldRespondWith`
""
{ matchStatus = 206
, matchHeaders = [ matchContentTypeJson
, "Content-Range" <:> "1-2/3" ]
}
it "does not throw an invalid range error" $ do
request methodPost "/rpc/getitemrange?limit=2&offset=1"
(rangeHdrsWithCount (ByteRangeFromTo 0 0))
[json| { "min": 2, "max": 5 } |]
`shouldRespondWith` [json| [{"id": 4}, {"id": 5}] |]
{ matchStatus = 206
, matchHeaders = ["Content-Range" <:> "1-2/3"]
}
request methodHead "/rpc/getitemrange?min=2&max=5&limit=2&offset=1"
(rangeHdrsWithCount (ByteRangeFromTo 0 0)) ""
`shouldRespondWith`
""
{ matchStatus = 206
, matchHeaders = [ matchContentTypeJson
, "Content-Range" <:> "1-2/3" ]
}
context "unknown function" $ do
it "returns 404" $
post "/rpc/fakefunc" [json| {} |] `shouldRespondWith` 404
it "should fail with 404 on unknown proc name" $
get "/rpc/fake" `shouldRespondWith` 404
it "should fail with 404 and hint the closest proc on unknown proc name" $
get "/rpc/sayhell" `shouldRespondWith`
[json| {
"hint":"Perhaps you meant to call the function test.sayhello",
"message":"Could not find the function test.sayhell without parameters in the schema cache",
"code":"PGRST202",
"details":"Searched for the function test.sayhell without parameters, but no matches were found in the schema cache."} |]
{ matchStatus = 404
, matchHeaders = [matchContentTypeJson]
}
it "should fail with 404 on unknown proc args" $ do
get "/rpc/sayhello" `shouldRespondWith` 404
get "/rpc/sayhello?any_arg=value" `shouldRespondWith` 404
it "should fail with 404 and hint the closest args on unknown proc args" $
get "/rpc/sayhello?nam=Peter" `shouldRespondWith`
[json| {
"hint":"Perhaps you meant to call the function test.sayhello(name)",
"message":"Could not find the function test.sayhello(nam) in the schema cache",
"code":"PGRST202",
"details":"Searched for the function test.sayhello with parameter nam, but no matches were found in the schema cache."} |]
{ matchStatus = 404
, matchHeaders = [matchContentTypeJson]
}
it "should not ignore unknown args and fail with 404" $
get "/rpc/add_them?a=1&b=2&smthelse=blabla" `shouldRespondWith`
[json| {
"hint":"Perhaps you meant to call the function test.add_them(a, b)",
"message":"Could not find the function test.add_them(a, b, smthelse) in the schema cache",
"code":"PGRST202",
"details":"Searched for the function test.add_them with parameters a, b, smthelse, but no matches were found in the schema cache."} |]
{ matchStatus = 404
, matchHeaders = [matchContentTypeJson]
}
it "should fail with 404 when no json arg is found with prefer single object" $
request methodPost "/rpc/sayhello"
[("Prefer","params=single-object")]
[json|{}|]
`shouldRespondWith`
[json| {
"hint":null,
"message":"Could not find the function test.sayhello in the schema cache",
"code":"PGRST202",
"details":"Searched for the function test.sayhello with a single json/jsonb parameter, but no matches were found in the schema cache."} |]
{ matchStatus = 404
, matchHeaders = [matchContentTypeJson]
}
it "should fail with 404 for overloaded functions with unknown args" $ do
get "/rpc/overloaded?wrong_arg=value" `shouldRespondWith`
[json| {
"hint":null,
"message":"Could not find the function test.overloaded(wrong_arg) in the schema cache",
"code":"PGRST202",
"details":"Searched for the function test.overloaded with parameter wrong_arg, but no matches were found in the schema cache."} |]
{ matchStatus = 404
, matchHeaders = [matchContentTypeJson]
}
get "/rpc/overloaded?a=1&b=2&wrong_arg=value" `shouldRespondWith`
[json| {
"hint":"Perhaps you meant to call the function test.overloaded(a, b, c)",
"message":"Could not find the function test.overloaded(a, b, wrong_arg) in the schema cache",
"code":"PGRST202",
"details":"Searched for the function test.overloaded with parameters a, b, wrong_arg, but no matches were found in the schema cache."} |]
{ matchStatus = 404
, matchHeaders = [matchContentTypeJson]
}
context "ambiguous overloaded functions with same parameters' names but different types" $ do
it "should fail with 300 Multiple Choices without explicit type casts" $
get "/rpc/overloaded_same_args?arg=value" `shouldRespondWith`
[json| {
"hint":"Try renaming the parameters or the function itself in the database so function overloading can be resolved",
"message":"Could not choose the best candidate function between: test.overloaded_same_args(arg => integer), test.overloaded_same_args(arg => xml), test.overloaded_same_args(arg => text, num => integer)",
"code":"PGRST203",
"details":null} |]
{ matchStatus = 300
, matchHeaders = [matchContentTypeJson]
}
it "works when having uppercase identifiers" $ do
get "/rpc/quotedFunction?user=mscott&fullName=Michael Scott&SSN=401-32-XXXX" `shouldRespondWith`
[json|{"user": "mscott", "fullName": "Michael Scott", "SSN": "401-32-XXXX"}|]
{ matchHeaders = [matchContentTypeJson] }
post "/rpc/quotedFunction"
[json|{"user": "dschrute", "fullName": "Dwight Schrute", "SSN": "030-18-XXXX"}|]
`shouldRespondWith`
[json|{"user": "dschrute", "fullName": "Dwight Schrute", "SSN": "030-18-XXXX"}|]
{ matchHeaders = [matchContentTypeJson] }
context "shaping the response returned by a proc" $ do
it "returns a project" $ do
post "/rpc/getproject" [json| { "id": 1} |] `shouldRespondWith`
[json|[{"id":1,"name":"Windows 7","client_id":1}]|]
get "/rpc/getproject?id=1" `shouldRespondWith`
[json|[{"id":1,"name":"Windows 7","client_id":1}]|]
it "can filter proc results" $ do
post "/rpc/getallprojects?id=gt.1&id=lt.5&select=id" [json| {} |] `shouldRespondWith`
[json|[{"id":2},{"id":3},{"id":4}]|]
{ matchHeaders = [matchContentTypeJson] }
get "/rpc/getallprojects?id=gt.1&id=lt.5&select=id" `shouldRespondWith`
[json|[{"id":2},{"id":3},{"id":4}]|]
{ matchHeaders = [matchContentTypeJson] }
it "can limit proc results" $ do
post "/rpc/getallprojects?id=gt.1&id=lt.5&select=id&limit=2&offset=1" [json| {} |]
`shouldRespondWith` [json|[{"id":3},{"id":4}]|]
{ matchStatus = 200
, matchHeaders = ["Content-Range" <:> "1-2/*"] }
get "/rpc/getallprojects?id=gt.1&id=lt.5&select=id&limit=2&offset=1"
`shouldRespondWith` [json|[{"id":3},{"id":4}]|]
{ matchStatus = 200
, matchHeaders = ["Content-Range" <:> "1-2/*"] }
it "select works on the first level" $ do
post "/rpc/getproject?select=id,name" [json| { "id": 1} |] `shouldRespondWith`
[json|[{"id":1,"name":"Windows 7"}]|]
get "/rpc/getproject?id=1&select=id,name" `shouldRespondWith`
[json|[{"id":1,"name":"Windows 7"}]|]
context "foreign entities embedding" $ do
it "can embed if related tables are in the exposed schema" $ do
post "/rpc/getproject?select=id,name,client:clients(id),tasks(id)" [json| { "id": 1} |] `shouldRespondWith`
[json|[{"id":1,"name":"Windows 7","client":{"id":1},"tasks":[{"id":1},{"id":2}]}]|]
{ matchHeaders = [matchContentTypeJson] }
get "/rpc/getproject?id=1&select=id,name,client:clients(id),tasks(id)" `shouldRespondWith`
[json|[{"id":1,"name":"Windows 7","client":{"id":1},"tasks":[{"id":1},{"id":2}]}]|]
{ matchHeaders = [matchContentTypeJson] }
it "cannot embed if the related table is not in the exposed schema" $ do
post "/rpc/single_article?select=*,article_stars(*)" [json|{ "id": 1}|]
`shouldRespondWith` 400
get "/rpc/single_article?id=1&select=*,article_stars(*)"
`shouldRespondWith` 400
it "can embed if the related tables are in a hidden schema but exposed as views" $ do
post "/rpc/single_article?select=id,articleStars(userId)"
[json|{ "id": 2}|]
`shouldRespondWith`
[json|{"id": 2, "articleStars": [{"userId": 3}]}|]
get "/rpc/single_article?id=2&select=id,articleStars(userId)"
`shouldRespondWith`
[json|{"id": 2, "articleStars": [{"userId": 3}]}|]
it "can embed an M2M relationship table" $ do
get "/rpc/getallusers?select=name,tasks(name)&id=gt.1"
`shouldRespondWith` [json|[
{"name":"Michael Scott", "tasks":[{"name":"Design IOS"}, {"name":"Code IOS"}, {"name":"Design OSX"}]},
{"name":"Dwight Schrute","tasks":[{"name":"Design w7"}, {"name":"Design IOS"}]}
]|]
{ matchHeaders = [matchContentTypeJson] }
-- https://github.com/PostgREST/postgrest/issues/2565
get "/rpc/get_yards?select=groups(*)"
`shouldRespondWith` [json|[]|]
{ matchHeaders = [matchContentTypeJson] }
it "can embed an M2M relationship table that has a parent relationship table" $
get "/rpc/getallusers?select=name,tasks(name,project:projects(name))&id=gt.1"
`shouldRespondWith` [json|[
{"name":"Michael Scott","tasks":[
{"name":"Design IOS","project":{"name":"IOS"}},
{"name":"Code IOS","project":{"name":"IOS"}},
{"name":"Design OSX","project":{"name":"OSX"}}
]},
{"name":"Dwight Schrute","tasks":[
{"name":"Design w7","project":{"name":"Windows 7"}},
{"name":"Design IOS","project":{"name":"IOS"}}
]}
]|]
{ matchHeaders = [matchContentTypeJson] }
it "can embed an O2O relationship" $ do
get "/rpc/allcapitals?select=name,country(name)"
`shouldRespondWith` [json|[
{"name":"Kabul","country":{"name":"Afghanistan"}},
{"name":"Algiers","country":{"name":"Algeria"}}]
|]
{ matchHeaders = [matchContentTypeJson] }
get "/rpc/allcountries?select=name,capital(name)"
`shouldRespondWith` [json|[
{"name":"Afghanistan","capital":{"name":"Kabul"}},
{"name":"Algeria","capital":{"name":"Algiers"}}
]|]
{ matchHeaders = [matchContentTypeJson] }
when (actualPgVersion >= pgVersion110) $
it "can embed if rpc returns domain of table type" $ do
post "/rpc/getproject_domain?select=id,name,client:clients(id),tasks(id)"
[json| { "id": 1} |]
`shouldRespondWith`
[json|[{"id":1,"name":"Windows 7","client":{"id":1},"tasks":[{"id":1},{"id":2}]}]|]
get "/rpc/getproject_domain?id=1&select=id,name,client:clients(id),tasks(id)"
`shouldRespondWith`
[json|[{"id":1,"name":"Windows 7","client":{"id":1},"tasks":[{"id":1},{"id":2}]}]|]
context "a proc that returns an empty rowset" $
it "returns empty json array" $ do
post "/rpc/test_empty_rowset" [json| {} |] `shouldRespondWith`
[json| [] |]
{ matchHeaders = [matchContentTypeJson] }
get "/rpc/test_empty_rowset" `shouldRespondWith`
[json| [] |]
{ matchHeaders = [matchContentTypeJson] }
context "proc return types" $ do
context "returns text" $ do
it "returns proper json" $
post "/rpc/sayhello" [json| { "name": "world" } |] `shouldRespondWith`
[json|"Hello, world"|]
{ matchHeaders = [matchContentTypeJson] }
it "can handle unicode" $
post "/rpc/sayhello" [json| { "name": "¥" } |] `shouldRespondWith`
[json|"Hello, ¥"|]
{ matchHeaders = [matchContentTypeJson] }
it "returns array" $
post "/rpc/ret_array" [json|{}|] `shouldRespondWith`
[json|[1, 2, 3]|]
{ matchHeaders = [matchContentTypeJson] }
it "returns setof integers" $
post "/rpc/ret_setof_integers"
[json|{}|]
`shouldRespondWith`
[json|[1,2,3]|]
it "returns enum value" $
post "/rpc/ret_enum" [json|{ "val": "foo" }|] `shouldRespondWith`
[json|"foo"|]
{ matchHeaders = [matchContentTypeJson] }
it "returns domain value" $
post "/rpc/ret_domain" [json|{ "val": "8" }|] `shouldRespondWith`
[json|8|]
{ matchHeaders = [matchContentTypeJson] }
it "returns range" $
post "/rpc/ret_range" [json|{ "low": 10, "up": 20 }|] `shouldRespondWith`
[json|"[10,20)"|]
{ matchHeaders = [matchContentTypeJson] }
it "returns row of scalars" $
post "/rpc/ret_scalars" [json|{}|] `shouldRespondWith`
[json|[{"a":"scalars", "b":"foo", "c":1, "d":"[10,20)"}]|]
{ matchHeaders = [matchContentTypeJson] }
it "returns composite type in exposed schema" $
post "/rpc/ret_point_2d"
[json|{}|]
`shouldRespondWith`
[json|{"x": 10, "y": 5}|]
it "cannot return composite type in hidden schema" $
post "/rpc/ret_point_3d" [json|{}|] `shouldRespondWith` 401
when (actualPgVersion >= pgVersion110) $
it "returns domain of composite type" $
post "/rpc/ret_composite_domain"
[json|{}|]
`shouldRespondWith`
[json|{"x": 10, "y": 5}|]
it "returns single row from table" $
post "/rpc/single_article?select=id"
[json|{"id": 2}|]
`shouldRespondWith`
[json|{"id": 2}|]
it "returns 204, no Content-Type header and no content for void" $
post "/rpc/ret_void"
[json|{}|]
`shouldRespondWith`
""
{ matchStatus = 204
, matchHeaders = [matchHeaderAbsent hContentType]
}
it "returns null for an integer with null value" $
post "/rpc/ret_null"
[json|{}|]
`shouldRespondWith`
[json|null|]
when (actualPgVersion >= pgVersion110) $ do
it "returns a record type" $ do
post "/rpc/returns_record"
""
`shouldRespondWith`
[json|{"id":1,"name":"Windows 7","client_id":1}|]
post "/rpc/returns_record_params"
[json|{"id":1, "name": "Windows%"}|]
`shouldRespondWith`
[json|{"id":1,"name":"Windows 7","client_id":1}|]
it "returns a setof record type" $ do
post "/rpc/returns_setof_record"
""
`shouldRespondWith`
[json|[{"id":1,"name":"Windows 7","client_id":1},{"id":2,"name":"Windows 10","client_id":1}]|]
post "/rpc/returns_setof_record_params"
[json|{"id":1,"name":"Windows%"}|]
`shouldRespondWith`
[json|[{"id":1,"name":"Windows 7","client_id":1},{"id":2,"name":"Windows 10","client_id":1}]|]
context "different types when overloaded" $ do
it "returns composite type" $
post "/rpc/ret_point_overloaded"
[json|{"x": 1, "y": 2}|]
`shouldRespondWith`
[json|{"x": 1, "y": 2}|]
it "returns json scalar with prefer single object" $
request methodPost "/rpc/ret_point_overloaded" [("Prefer","params=single-object")]
[json|{"x": 1, "y": 2}|]
`shouldRespondWith`
[json|{"x": 1, "y": 2}|]
{ matchHeaders = [matchContentTypeJson] }
context "proc argument types" $ do
-- different syntax for array needed for pg<10
when (actualPgVersion < pgVersion100) $
it "accepts a variety of arguments (Postgres < 10)" $
post "/rpc/varied_arguments"
[json| {
"double": 3.1,
"varchar": "hello",
"boolean": true,
"date": "20190101",
"money": 0,
"enum": "foo",
"arr": "{a,b,c}",
"integer": 43,
"json": {"some key": "some value"},
"jsonb": {"another key": [1, 2, "3"]}
} |]
`shouldRespondWith`
[json| {
"double": 3.1,
"varchar": "hello",
"boolean": true,
"date": "2019-01-01",
"money": "$0.00",
"enum": "foo",
"arr": ["a", "b", "c"],
"integer": 43,
"json": {"some key": "some value"},
"jsonb": {"another key": [1, 2, "3"]}
} |]
{ matchHeaders = [matchContentTypeJson] }
when (actualPgVersion >= pgVersion100) $
it "accepts a variety of arguments (Postgres >= 10)" $
post "/rpc/varied_arguments"
[json| {
"double": 3.1,
"varchar": "hello",
"boolean": true,
"date": "20190101",
"money": 0,
"enum": "foo",
"arr": ["a", "b", "c"],
"integer": 43,
"json": {"some key": "some value"},
"jsonb": {"another key": [1, 2, "3"]}
} |]
`shouldRespondWith`
[json| {
"double": 3.1,
"varchar": "hello",
"boolean": true,
"date": "2019-01-01",
"money": "$0.00",
"enum": "foo",
"arr": ["a", "b", "c"],
"integer": 43,
"json": {"some key": "some value"},
"jsonb": {"another key": [1, 2, "3"]}
} |]
{ matchHeaders = [matchContentTypeJson] }
it "accepts a variety of arguments with GET" $
-- without JSON / JSONB here, because passing those via query string is useless - they just become a "json string" all the time
get "/rpc/varied_arguments?double=3.1&varchar=hello&boolean=true&date=20190101&money=0&enum=foo&arr=%7Ba,b,c%7D&integer=43"
`shouldRespondWith`
[json| {
"double": 3.1,
"varchar": "hello",
"boolean": true,
"date": "2019-01-01",
"money": "$0.00",
"enum": "foo",
"arr": ["a", "b", "c"],
"integer": 43,
"json": {},
"jsonb": {}
} |]
{ matchHeaders = [matchContentTypeJson] }
it "accepts a variety of arguments from an html form" $
request methodPost "/rpc/varied_arguments"
[("Content-Type", "application/x-www-form-urlencoded")]
"double=3.1&varchar=hello&boolean=true&date=20190101&money=0&enum=foo&arr=%7Ba,b,c%7D&integer=43"
`shouldRespondWith`
[json| {
"double": 3.1,
"varchar": "hello",
"boolean": true,
"date": "2019-01-01",
"money": "$0.00",
"enum": "foo",
"arr": ["a", "b", "c"],
"integer": 43,
"json": {},
"jsonb": {}
} |]
{ matchHeaders = [matchContentTypeJson] }
it "parses embedded JSON arguments as JSON" $
post "/rpc/json_argument"
[json| { "arg": { "key": 3 } } |]
`shouldRespondWith`
[json|"object"|]
{ matchHeaders = [matchContentTypeJson] }
when (actualPgVersion < pgVersion100) $
it "parses quoted JSON arguments as JSON (Postgres < 10)" $
post "/rpc/json_argument"
[json| { "arg": "{ \"key\": 3 }" } |]
`shouldRespondWith`
[json|"object"|]
{ matchHeaders = [matchContentTypeJson] }
when ((actualPgVersion >= pgVersion109 && actualPgVersion < pgVersion110)
|| actualPgVersion >= pgVersion114) $
it "parses quoted JSON arguments as JSON string (from Postgres 10.9, 11.4)" $
post "/rpc/json_argument"
[json| { "arg": "{ \"key\": 3 }" } |]
`shouldRespondWith`
[json|"string"|]
{ matchHeaders = [matchContentTypeJson] }
context "improper input" $ do
it "rejects unknown content type even if payload is good" $ do
request methodPost "/rpc/sayhello"
(acceptHdrs "audio/mpeg3") [json| { "name": "world" } |]
`shouldRespondWith` 406
request methodGet "/rpc/sayhello?name=world"
(acceptHdrs "audio/mpeg3") ""
`shouldRespondWith` 406
it "rejects malformed json payload" $ do
p <- request methodPost "/rpc/sayhello"
(acceptHdrs "application/json") "sdfsdf"
liftIO $ do
simpleStatus p `shouldBe` badRequest400
isErrorFormat (simpleBody p) `shouldBe` True
it "treats simple plpgsql raise as invalid input" $ do
p <- post "/rpc/problem" "{}"
liftIO $ do
simpleStatus p `shouldBe` badRequest400
isErrorFormat (simpleBody p) `shouldBe` True
it "treats plpgsql assert as internal server error" $ do
p <- post "/rpc/assert" "{}"
liftIO $ do
simpleStatus p `shouldBe` internalServerError500
isErrorFormat (simpleBody p) `shouldBe` True
context "unsupported method" $ do
it "DELETE fails" $
request methodDelete "/rpc/sayhello" [] ""
`shouldRespondWith`
[json|{"message":"Cannot use the DELETE method on RPC","code":"PGRST101","details":null,"hint":null}|]
{ matchStatus = 405
, matchHeaders = [matchContentTypeJson]
}
it "PATCH fails" $
request methodPatch "/rpc/sayhello" [] ""
`shouldRespondWith` 405
it "executes the proc exactly once per request" $ do
-- callcounter is persistent even with rollback, because it uses a sequence
-- reset counter first to make test repeatable
request methodPost "/rpc/reset_sequence"
[("Prefer", "tx=commit")]
[json|{"name": "callcounter_count", "value": 1}|]
`shouldRespondWith`
""
{ matchStatus = 204
, matchHeaders = [ matchHeaderAbsent hContentType ]
}
-- now the test
post "/rpc/callcounter"
[json|{}|]
`shouldRespondWith`
[json|1|]
post "/rpc/callcounter"
[json|{}|]
`shouldRespondWith`
[json|2|]
context "a proc that receives no parameters" $ do
it "interprets empty string as empty json object on a post request" $
post "/rpc/noparamsproc" BL.empty `shouldRespondWith`
[json| "Return value of no parameters procedure." |]
{ matchHeaders = [matchContentTypeJson] }
it "interprets empty string as a function with no args on a get request" $
get "/rpc/noparamsproc" `shouldRespondWith`
[json| "Return value of no parameters procedure." |]
{ matchHeaders = [matchContentTypeJson] }
it "returns proper output when having the same return col name as the proc name" $ do
post "/rpc/test" [json|{}|] `shouldRespondWith`
[json|[{"test":"hello","value":1}]|] { matchHeaders = [matchContentTypeJson] }
get "/rpc/test" `shouldRespondWith`
[json|[{"test":"hello","value":1}]|] { matchHeaders = [matchContentTypeJson] }
context "procs with OUT/INOUT params" $ do
it "returns an object result when there is a single OUT param" $ do
get "/rpc/single_out_param?num=5"
`shouldRespondWith`
[json|{"num_plus_one":6}|]
get "/rpc/single_json_out_param?a=1&b=two"
`shouldRespondWith`
[json|{"my_json": {"a": 1, "b": "two"}}|]
it "returns an object result when there is a single INOUT param" $
get "/rpc/single_inout_param?num=2"
`shouldRespondWith`
[json|{"num":3}|]
it "returns an object result when there are many OUT params" $
get "/rpc/many_out_params"
`shouldRespondWith`
[json|{"my_json":{"a": 1, "b": "two"},"num":3,"str":"four"}|]
it "returns an object result when there are many INOUT params" $
get "/rpc/many_inout_params?num=1&str=two&b=false"
`shouldRespondWith`
[json|{"num":1,"str":"two","b":false}|]
context "procs with TABLE return" $ do
it "returns an object result when there is a single-column TABLE return type" $
get "/rpc/single_column_table_return"
`shouldRespondWith`
[json|[{"a": "A"}]|]
it "returns an object result when there is a multi-column TABLE return type" $
get "/rpc/multi_column_table_return"
`shouldRespondWith`
[json|[{"a": "A", "b": "B"}]|]
context "procs with VARIADIC params" $ do
when (actualPgVersion < pgVersion100) $
it "works with POST (Postgres < 10)" $
post "/rpc/variadic_param"
[json| { "v": "{hi,hello,there}" } |]
`shouldRespondWith`
[json|["hi", "hello", "there"]|]
when (actualPgVersion >= pgVersion100) $ do
it "works with POST (Postgres >= 10)" $
post "/rpc/variadic_param"
[json| { "v": ["hi", "hello", "there"] } |]
`shouldRespondWith`
[json|["hi", "hello", "there"]|]
context "works with GET and repeated params" $ do
it "n=0 (through DEFAULT)" $
get "/rpc/variadic_param"
`shouldRespondWith`
[json|[]|]
it "n=1" $
get "/rpc/variadic_param?v=hi"
`shouldRespondWith`
[json|["hi"]|]
it "n>1" $
get "/rpc/variadic_param?v=hi&v=there"
`shouldRespondWith`
[json|["hi", "there"]|]
context "works with POST and repeated params from html form" $ do
it "n=0 (through DEFAULT)" $
request methodPost "/rpc/variadic_param"
[("Content-Type", "application/x-www-form-urlencoded")]
""
`shouldRespondWith`
[json|[]|]
it "n=1" $
request methodPost "/rpc/variadic_param"
[("Content-Type", "application/x-www-form-urlencoded")]
"v=hi"
`shouldRespondWith`
[json|["hi"]|]
it "n>1" $
request methodPost "/rpc/variadic_param"
[("Content-Type", "application/x-www-form-urlencoded")]
"v=hi&v=there"
`shouldRespondWith`
[json|["hi", "there"]|]
it "returns last value for repeated params without VARIADIC" $
get "/rpc/sayhello?name=ignored&name=world"
`shouldRespondWith`
[json|"Hello, world"|]
when (actualPgVersion >= pgVersion100) $
it "returns last value for repeated non-variadic params in function with other VARIADIC arguments" $
get "/rpc/sayhello_variadic?name=ignored&name=world&v=unused"
`shouldRespondWith`
[json|"Hello, world"|]
it "can handle procs with args that have a DEFAULT value" $ do
get "/rpc/many_inout_params?num=1&str=two"
`shouldRespondWith`
[json| {"num":1,"str":"two","b":true}|]
get "/rpc/three_defaults?b=4"
`shouldRespondWith`
[json|8|]
it "can map a RAISE error code and message to a http status" $
get "/rpc/raise_pt402"
`shouldRespondWith` [json|{ "hint": "Upgrade your plan", "details": "Quota exceeded", "code": "PT402", "message": "Payment Required" }|]
{ matchStatus = 402
, matchHeaders = [matchContentTypeJson]
}
it "defaults to status 500 if RAISE code is PT not followed by a number" $
get "/rpc/raise_bad_pt"
`shouldRespondWith`
[json|{"hint": null, "details": null, "code": "PT40A", "message": "Wrong"}|]
{ matchStatus = 500
, matchHeaders = [ matchContentTypeJson ]
}
context "expects a single json object" $ do
it "does not expand posted json into parameters" $
request methodPost "/rpc/singlejsonparam"
[("prefer","params=single-object")] [json| { "p1": 1, "p2": "text", "p3" : {"obj":"text"} } |] `shouldRespondWith`
[json| { "p1": 1, "p2": "text", "p3" : {"obj":"text"} } |]
{ matchHeaders = [matchContentTypeJson] }
it "accepts parameters from an html form" $
request methodPost "/rpc/singlejsonparam"
[("Prefer","params=single-object"),("Content-Type", "application/x-www-form-urlencoded")]
("integer=7&double=2.71828&varchar=forms+are+fun&" <>
"boolean=false&date=1900-01-01&money=$3.99&enum=foo") `shouldRespondWith`
[json| { "integer": "7", "double": "2.71828", "varchar" : "forms are fun"
, "boolean":"false", "date":"1900-01-01", "money":"$3.99", "enum":"foo" } |]
{ matchHeaders = [matchContentTypeJson] }
it "works with GET" $
request methodGet "/rpc/singlejsonparam?p1=1&p2=text" [("Prefer","params=single-object")] ""
`shouldRespondWith` [json|{ "p1": "1", "p2": "text"}|]
{ matchHeaders = [matchContentTypeJson] }
context "should work with an overloaded function" $ do
it "overloaded()" $
get "/rpc/overloaded"
`shouldRespondWith`
[json|[1,2,3]|]
it "overloaded(json) single-object" $
request methodPost "/rpc/overloaded"
[("Prefer","params=single-object")]
[json|[{"x": 1, "y": "first"}, {"x": 2, "y": "second"}]|]
`shouldRespondWith`
[json|[{"x": 1, "y": "first"}, {"x": 2, "y": "second"}]|]
it "overloaded(int, int)" $
get "/rpc/overloaded?a=1&b=2" `shouldRespondWith` [str|3|]
it "overloaded(text, text, text)" $
get "/rpc/overloaded?a=1&b=2&c=3" `shouldRespondWith` [json|"123"|]
it "overloaded_html_form()" $
request methodPost "/rpc/overloaded_html_form"
[("Content-Type", "application/x-www-form-urlencoded")]
""
`shouldRespondWith`
[json|[1,2,3]|]
it "overloaded_html_form(json) single-object" $
request methodPost "/rpc/overloaded_html_form"
[("Content-Type", "application/x-www-form-urlencoded"), ("Prefer","params=single-object")]
"a=1&b=2&c=3"
`shouldRespondWith`
[json|{"a": "1", "b": "2", "c": "3"}|]
it "overloaded_html_form(int, int)" $
request methodPost "/rpc/overloaded_html_form"
[("Content-Type", "application/x-www-form-urlencoded")]
"a=1&b=2"
`shouldRespondWith`
[str|3|]
it "overloaded_html_form(text, text, text)" $
request methodPost "/rpc/overloaded_html_form"
[("Content-Type", "application/x-www-form-urlencoded")]
"a=1&b=2&c=3"
`shouldRespondWith`
[json|"123"|]
-- https://github.com/PostgREST/postgrest/issues/1672
context "embedding overloaded functions with the same signature except for the last param with a default value" $ do
it "overloaded_default(text default)" $ do
request methodPost "/rpc/overloaded_default?select=id,name,users(name)"
[("Content-Type", "application/json")]
[json|{}|]
`shouldRespondWith`
[json|[{"id": 2, "name": "Code w7", "users": [{"name": "Angela Martin"}]}] |]
it "overloaded_default(int)" $
request methodPost "/rpc/overloaded_default"
[("Content-Type", "application/json")]
[json|{"must_param":1}|]
`shouldRespondWith`
[json|{"val":1}|]
it "overloaded_default(int, text default)" $ do
request methodPost "/rpc/overloaded_default?select=id,name,users(name)"
[("Content-Type", "application/json")]
[json|{"a":4}|]
`shouldRespondWith`
[json|[{"id": 5, "name": "Design IOS", "users": [{"name": "Michael Scott"}, {"name": "Dwight Schrute"}]}] |]
it "overloaded_default(int, int)" $
request methodPost "/rpc/overloaded_default"
[("Content-Type", "application/json")]
[json|{"a":2,"must_param":4}|]
`shouldRespondWith`
[json|{"a":2,"val":4}|]
context "only for POST rpc" $ do
it "gives a parse filter error if GET style proc args are specified" $
post "/rpc/sayhello?name=John" [json|{name: "John"}|] `shouldRespondWith` 400
it "ignores json keys not included in ?columns" $
post "/rpc/sayhello?columns=name"
[json|{"name": "John", "smth": "here", "other": "stuff", "fake_id": 13}|]
`shouldRespondWith`
[json|"Hello, John"|]
{ matchHeaders = [matchContentTypeJson] }
it "only takes the first object in case of array of objects payload" $
post "/rpc/add_them"
[json|[
{"a": 1, "b": 2},
{"a": 4, "b": 6},
{"a": 100, "b": 200} ]|]
`shouldRespondWith` "3"
{ matchHeaders = [matchContentTypeJson] }
context "HTTP request env vars" $ do
it "custom header is set" $
request methodPost "/rpc/get_guc_value"
[("Custom-Header", "test")]
[json| { "prefix": "request.headers", "name": "custom-header" } |]
`shouldRespondWith`
[json|"test"|]
{ matchStatus = 200
, matchHeaders = [ matchContentTypeJson ]
}
it "standard header is set" $
request methodPost "/rpc/get_guc_value"
[("Origin", "http://example.com")]
[json| { "prefix": "request.headers", "name": "origin" } |]
`shouldRespondWith`
[json|"http://example.com"|]
{ matchStatus = 200
, matchHeaders = [ matchContentTypeJson ]
}
it "current role is available as GUC claim" $
request methodPost "/rpc/get_guc_value" []
[json| { "prefix": "request.jwt.claims", "name": "role" } |]
`shouldRespondWith`
[json|"postgrest_test_anonymous"|]
{ matchStatus = 200
, matchHeaders = [ matchContentTypeJson ]
}
it "single cookie ends up as claims" $
request methodPost "/rpc/get_guc_value" [("Cookie","acookie=cookievalue")]