-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathintegration_test.clj
1779 lines (1513 loc) · 62.3 KB
/
integration_test.clj
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
(ns compojure.api.integration-test
(:require [compojure.api.sweet :refer :all]
[compojure.api.test-utils :refer :all]
[compojure.api.exception :as ex]
[compojure.api.swagger :as swagger]
[compojure.api.core :refer [route-middleware]]
[midje.sweet :refer :all]
[ring.util.http-response :refer :all]
[ring.util.http-predicates :as http]
[schema.core :as s]
[ring.swagger.core :as rsc]
[ring.util.http-status :as status]
[compojure.api.middleware :as mw]
[ring.swagger.middleware :as rsm]
[compojure.api.validator :as validator]
[compojure.api.request :as request]
[compojure.api.routes :as routes]
[clojure.java.io :as io]
[muuntaja.core :as m]
[compojure.api.core :as c])
(:import (java.sql SQLException SQLWarning)
(java.io File)))
;;
;; Data
;;
(s/defschema User {:id s/Int
:name s/Str})
(def pertti {:id 1 :name "Pertti"})
(def invalid-user {:id 1 :name "Jorma" :age 50})
; Headers contain extra keys, so make the schema open
(s/defschema UserHeaders
(assoc User
s/Keyword s/Any))
;;
;; Middleware setup
;;
(def mw* "mw")
(defn middleware*
"This middleware appends given value or 1 to a header in request and response."
([handler] (middleware* handler 1))
([handler value]
(fn
([request]
(let [append #(str % value)
request (update-in request [:headers mw*] append)
response (handler request)]
(update-in response [:headers mw*] append)))
([request respond raise]
(let [append #(str % value)
request (update-in request [:headers mw*] append)]
(handler request #(respond (update-in % [:headers mw*] append)) raise))))))
(defn constant-middleware
"This middleware rewrites all responses with a constant response."
[_ res]
(fn
([_] res)
([_ respond _] (respond res))))
(defn reply-mw*
"Handler which replies with response where a header contains copy
of the headers value from request and 7"
[request]
(-> (ok "true")
(header mw* (str (get-in request [:headers mw*]) "/"))))
(defn middleware-x
"If request has query-param x, presume it's a integer and multiply it by two
before passing request to next handler."
[handler]
(fn
([req]
(handler (update-in req [:query-params "x"] #(* (Integer. %) 2))))
([req respond raise]
(handler (update-in req [:query-params "x"] #(* (Integer. %) 2)) respond raise))))
(defn custom-validation-error-handler [ex data request]
(let [error-body {:custom-error (:uri request)}]
(case (:type data)
::ex/response-validation (not-implemented error-body)
(bad-request error-body))))
(defn custom-exception-handler [key]
(fn [^Exception ex data request]
(ok {key (str ex)})))
(defn custom-error-handler [ex data request]
(ok {:custom-error (:data data)}))
;;
;; Facts
;;
(facts "core routes"
(fact "keyword options"
(let [route (GET "/ping" []
:return String
(ok "kikka"))]
(route {:request-method :get :uri "/ping"}) => (contains {:body "kikka"})))
(fact "map options"
(let [route (GET "/ping" []
{:return String}
(ok "kikka"))]
(route {:request-method :get :uri "/ping"}) => (contains {:body "kikka"})))
(fact "map return"
(let [route (GET "/ping" []
{:body "kikka"})]
(route {:request-method :get :uri "/ping"}) => (contains {:body "kikka"}))))
(facts "middleware ordering"
(let [app (api
{:middleware [[middleware* 0]]}
(route-middleware [[middleware* "a"] [middleware* "b"]]
(context "/middlewares" []
:middleware [(fn [handler] (middleware* handler 1)) [middleware* 2]]
(GET "/simple" req (reply-mw* req))
(route-middleware [#(middleware* % "c") [middleware* "d"]]
(GET "/nested" req (reply-mw* req))
(GET "/nested-declared" req
:middleware [(fn [handler] (middleware* handler "e")) [middleware* "f"]]
(reply-mw* req))))))]
(fact "are applied left-to-right"
(let [[status _ headers] (get* app "/middlewares/simple" {})]
status => 200
(get headers mw*) => "012ab/ba210"))
(fact "are applied left-to-right closest one first"
(let [[status _ headers] (get* app "/middlewares/nested" {})]
status => 200
(get headers mw*) => "012abcd/dcba210"))
(fact "are applied left-to-right for both nested & declared closest one first"
(let [[status _ headers] (get* app "/middlewares/nested-declared" {})]
status => 200
(get headers mw*) => "012abcdef/fedcba210"))))
(facts "context middleware"
(let [app (api
(context "/middlewares" []
:middleware [(fn [h] (fn mw
([r] (ok {:middleware "hello"}))
([r respond _] (respond (mw r)))))]
(GET "/simple" req (reply-mw* req))))]
(fact "is applied even if route is not matched"
(let [[status body] (get* app "/middlewares/non-existing" {})]
status => 200
body => {:middleware "hello"}))))
(facts "middleware - multiple routes"
(let [app (api
(GET "/first" []
(ok {:value "first"}))
(GET "/second" []
:middleware [[constant-middleware (ok {:value "foo"})]]
(ok {:value "second"}))
(GET "/third" []
(ok {:value "third"})))]
(fact "first returns first"
(let [[status body] (get* app "/first" {})]
status => 200
body => {:value "first"}))
(fact "second returns foo"
(let [[status body] (get* app "/second" {})]
status => 200
body => {:value "foo"}))
(fact "third returns third"
(let [[status body] (get* app "/third" {})]
status => 200
body => {:value "third"}))))
(facts "middleware - editing request"
(let [app (api
(GET "/first" []
:query-params [x :- Long]
:middleware [middleware-x]
(ok {:value x})))]
(fact "middleware edits the parameter before route body"
(let [[status body] (get* app "/first?x=5" {})]
status => 200
body => {:value 10}))))
(fact ":body, :query, :headers and :return"
(let [app (api
(context "/models" []
(GET "/pertti" []
:return User
(ok pertti))
(GET "/user" []
:return User
:query [user User]
(ok user))
(GET "/user2" []
:return User
:query [user :- User]
(ok user))
(GET "/invalid-user" []
:return User
(ok invalid-user))
(GET "/not-validated" []
(ok invalid-user))
(POST "/user" []
:return User
:body [user User]
(ok user))
(POST "/user2" []
:return User
:body [user User]
(ok user))
(POST "/user_list" []
:return [User]
:body [users [User]]
(ok users))
(POST "/user_set" []
:return #{User}
:body [users #{User}]
(ok users))
(POST "/user_headers" []
:return User
:headers [user UserHeaders]
(ok (select-keys user [:id :name])))
(POST "/user_headers2" []
:return User
:headers [user :- UserHeaders]
(ok (select-keys user [:id :name])))
(POST "/user_legacy" {user :body-params}
:return User
(ok user))))]
(fact "GET"
(let [[status body] (get* app "/models/pertti")]
status => 200
body => pertti))
(fact "GET with smart destructuring"
(let [[status body] (get* app "/models/user" pertti)]
status => 200
body => pertti))
(fact "GET with smart destructuring & :-"
(let [[status body] (get* app "/models/user2" pertti)]
status => 200
body => pertti))
(fact "POST with smart destructuring"
(let [[status body] (post* app "/models/user" (json-string pertti))]
status => 200
body => pertti))
(fact "POST with smart destructuring & :-"
(let [[status body] (post* app "/models/user2" (json-string pertti))]
status => 200
body => pertti))
(fact "POST with smart destructuring - lists"
(let [[status body] (post* app "/models/user_list" (json-string [pertti]))]
status => 200
body => [pertti]))
(fact "POST with smart destructuring - sets"
(let [[status body] (post* app "/models/user_set" (json-string #{pertti}))]
status => 200
body => [pertti]))
(fact "POST with compojure destructuring"
(let [[status body] (post* app "/models/user_legacy" (json-string pertti))]
status => 200
body => pertti))
(fact "POST with smart destructuring - headers"
(let [[status body] (headers-post* app "/models/user_headers" pertti)]
status => 200
body => pertti))
(fact "POST with smart destructuring - headers & :-"
(let [[status body] (headers-post* app "/models/user_headers2" pertti)]
status => 200
body => pertti))
(fact "Validation of returned data"
(let [[status] (get* app "/models/invalid-user")]
status => 500))
(fact "Routes without a :return parameter aren't validated"
(let [[status body] (get* app "/models/not-validated")]
status => 200
body => invalid-user))
(fact "Invalid json in body causes 400 with error message in json"
(let [[status body] (post* app "/models/user" "{INVALID}")]
status => 400
body => (contains
{:type "compojure.api.exception/request-parsing"
:message (contains "Malformed application/json")
:original (contains "Unexpected character")})))))
(fact ":responses"
(fact "normal cases"
(let [app (api
(swagger-routes)
(GET "/lotto/:x" []
:path-params [x :- Long]
:responses {403 {:schema [String]}
440 {:schema [String]}}
:return [Long]
(case x
1 (ok [1])
2 (ok ["two"])
3 (forbidden ["error"])
4 (forbidden [1])
(not-found {:message "not-found"}))))]
(fact "return case"
(let [[status body] (get* app "/lotto/1")]
status => 200
body => [1]))
(fact "return case, non-matching model"
(let [[status body] (get* app "/lotto/2")]
status => 500
body => (contains {:errors vector?})))
(fact "error case"
(let [[status body] (get* app "/lotto/3")]
status => 403
body => ["error"]))
(fact "error case, non-matching model"
(let [[status body] (get* app "/lotto/4")]
status => 500
body => (contains {:errors vector?})))
(fact "returning non-predefined http-status code works"
(let [[status body] (get* app "/lotto/5")]
body => {:message "not-found"}
status => 404))
(fact "swagger-docs for multiple returns"
(-> app get-spec :paths vals first :get :responses keys set))))
(fact ":responses 200 and :return"
(let [app (api
(GET "/lotto/:x" []
:path-params [x :- Long]
:return {:return String}
:responses {200 {:schema {:value String}}}
(case x
1 (ok {:return "ok"})
2 (ok {:value "ok"}))))]
(fact "return case"
(let [[status body] (get* app "/lotto/1")]
status => 500
body => (contains {:errors {:return "disallowed-key"
:value "missing-required-key"}})))
(fact "return case"
(let [[status body] (get* app "/lotto/2")]
status => 200
body => {:value "ok"}))))
(fact ":responses 200 and :return - other way around"
(let [app (api
(GET "/lotto/:x" []
:path-params [x :- Long]
:responses {200 {:schema {:value String}}}
:return {:return String}
(case x
1 (ok {:return "ok"})
2 (ok {:value "ok"}))))]
(fact "return case"
(let [[status body] (get* app "/lotto/1")]
status => 200
body => {:return "ok"}))
(fact "return case"
(let [[status body] (get* app "/lotto/2")]
status => 500
body => (contains {:errors {:return "missing-required-key"
:value "disallowed-key"}}))))))
(fact ":query-params, :path-params, :header-params , :body-params and :form-params"
(let [app (api
(context "/smart" []
(GET "/plus" []
:query-params [x :- Long y :- Long]
(ok {:total (+ x y)}))
(GET "/multiply/:x/:y" []
:path-params [x :- Long y :- Long]
(ok {:total (* x y)}))
(GET "/power" []
:header-params [x :- Long y :- Long]
(ok {:total (long (Math/pow x y))}))
(POST "/minus" []
:body-params [x :- Long {y :- Long 1}]
(ok {:total (- x y)}))
(POST "/divide" []
:form-params [x :- Long y :- Long]
(ok {:total (/ x y)}))))]
(fact "query-parameters"
(let [[status body] (get* app "/smart/plus" {:x 2 :y 3})]
status => 200
body => {:total 5}))
(fact "path-parameters"
(let [[status body] (get* app "/smart/multiply/2/3")]
status => 200
body => {:total 6}))
(fact "header-parameters"
(let [[status body] (get* app "/smart/power" {} {:x 2 :y 3})]
status => 200
body => {:total 8}))
(fact "form-parameters"
(let [[status body] (form-post* app "/smart/divide" {:x 6 :y 3})]
status => 200
body => {:total 2}))
(fact "body-parameters"
(let [[status body] (post* app "/smart/minus" (json-string {:x 2 :y 3}))]
status => 200
body => {:total -1}))
(fact "default parameters"
(let [[status body] (post* app "/smart/minus" (json-string {:x 2}))]
status => 200
body => {:total 1}))))
(fact "primitive support"
(let [app (api
{:swagger {:spec "/swagger.json"}}
(context "/primitives" []
(GET "/return-long" []
:return Long
(ok 1))
(GET "/long" []
(ok 1))
(GET "/return-string" []
:return String
(ok "kikka"))
(POST "/arrays" []
:return [Long]
:body [longs [Long]]
(ok longs))))]
(fact "when :return is set, longs can be returned"
(let [[status body] (raw-get* app "/primitives/return-long")]
status => 200
body => "1"))
(fact "when :return is not set, longs won't be encoded"
(let [[status body] (raw-get* app "/primitives/long")]
status => 200
body => number?))
(fact "when :return is set, raw strings can be returned"
(let [[status body] (raw-get* app "/primitives/return-string")]
status => 200
body => "\"kikka\""))
(fact "primitive arrays work"
(let [[status body] (raw-post* app "/primitives/arrays" (json-string [1 2 3]))]
status => 200
body => "[1,2,3]"))
(fact "swagger-spec is valid"
(validator/validate app))
(fact "primitive array swagger-docs are good"
(-> app get-spec :paths (get "/primitives/arrays") :post :parameters)
=> [{:description ""
:in "body"
:name ""
:required true
:schema {:items {:format "int64"
:type "integer"}
:type "array"}}]
(-> app get-spec :paths (get "/primitives/arrays") :post :responses :200 :schema)
=> {:items {:format "int64",
:type "integer"},
:type "array"})))
(fact "compojure destructuring support"
(let [app (api
(context "/destructuring" []
(GET "/regular" {{:keys [a]} :params}
(ok {:a a
:b (-> +compojure-api-request+ :params :b)}))
(GET "/regular2" {:as req}
(ok {:a (-> req :params :a)
:b (-> +compojure-api-request+ :params :b)}))
(GET "/vector" [a]
(ok {:a a
:b (-> +compojure-api-request+ :params :b)}))
(GET "/vector2" [:as req]
(ok {:a (-> req :params :a)
:b (-> +compojure-api-request+ :params :b)}))
(GET "/symbol" req
(ok {:a (-> req :params :a)
:b (-> +compojure-api-request+ :params :b)}))
(GET "/integrated" [a] :query-params [b]
(ok {:a a
:b b}))))]
(doseq [uri ["regular" "regular2" "vector" "vector2" "symbol" "integrated"]]
(fact {:midje/description uri}
(let [[status body] (get* app (str "/destructuring/" uri) {:a "a" :b "b"})]
status => 200
body => {:a "a" :b "b"})))))
(fact "counting execution times, issue #19"
(let [execution-times (atom 0)
app (api
(GET "/user" []
:return User
:query [user User]
(swap! execution-times inc)
(ok user)))]
(fact "body is executed one"
@execution-times => 0
(let [[status body] (get* app "/user" pertti)]
status => 200
body => pertti)
@execution-times => 1)))
(fact "swagger-docs"
(let [app (api
{:formats (m/select-formats
m/default-options
["application/json" "application/edn"])}
(swagger-routes)
(GET "/user" []
(continue)))]
(fact "api-listing shows produces & consumes for known types"
(get-spec app) => {:swagger "2.0"
:info {:title "Swagger API"
:version "0.0.1"}
:basePath "/"
:consumes ["application/json" "application/edn"]
:produces ["application/json" "application/edn"]
:definitions {}
:paths {"/user" {:get {:responses {:default {:description ""}}}}}}))
(fact "swagger-routes"
(fact "with defaults"
(let [app (api (swagger-routes))]
(fact "api-docs are mounted to /"
(let [[status body] (raw-get* app "/")]
status => 200
body => #"<title>Swagger UI</title>"))
(fact "spec is mounted to /swagger.json"
(let [[status body] (get* app "/swagger.json")]
status => 200
body => (contains {:swagger "2.0"})))))
(fact "with partial overridden values"
(let [app (api (swagger-routes {:ui "/api-docs"
:data {:info {:title "Kikka"}
:paths {"/ping" {:get {}}}}}))]
(fact "api-docs are mounted"
(let [[status body] (raw-get* app "/api-docs")]
status => 200
body => #"<title>Swagger UI</title>"))
(fact "spec is mounted to /swagger.json"
(let [[status body] (get* app "/swagger.json")]
status => 200
body => (contains
{:swagger "2.0"
:info (contains
{:title "Kikka"})
:paths (contains
{(keyword "/ping") anything})}))))))
(fact "swagger via api-options"
(fact "with defaults"
(let [app (api)]
(fact "api-docs are not mounted"
(let [[status body] (raw-get* app "/")]
status => nil))
(fact "spec is not mounted"
(let [[status body] (get* app "/swagger.json")]
status => nil))))
(fact "with spec"
(let [app (api {:swagger {:spec "/swagger.json"}})]
(fact "api-docs are not mounted"
(let [[status body] (raw-get* app "/")]
status => nil))
(fact "spec is mounted to /swagger.json"
(let [[status body] (get* app "/swagger.json")]
status => 200
body => (contains {:swagger "2.0"}))))))
(fact "with ui"
(let [app (api {:swagger {:ui "/api-docs"}})]
(fact "api-docs are mounted"
(let [[status body] (raw-get* app "/api-docs")]
status => 200
body => #"<title>Swagger UI</title>"))
(fact "spec is not mounted"
(let [[status body] (get* app "/swagger.json")]
status => nil))))
(fact "with ui and spec"
(let [app (api {:swagger {:spec "/swagger.json", :ui "/api-docs"}})]
(fact "api-docs are mounted"
(let [[status body] (raw-get* app "/api-docs")]
status => 200
body => #"<title>Swagger UI</title>"))
(fact "spec is mounted to /swagger.json"
(let [[status body] (get* app "/swagger.json")]
status => 200
body => (contains {:swagger "2.0"}))))))
(facts "swagger-docs with anonymous Return and Body models"
(let [app (api
(swagger-routes)
(POST "/echo" []
:return (s/either {:a String})
:body [_ (s/maybe {:a String})]
identity))]
(fact "api-docs"
(let [spec (get-spec app)]
(let [operation (some-> spec :paths vals first :post)
body-ref (some-> operation :parameters first :schema :$ref)
return-ref (get-in operation [:responses :200 :schema :$ref])]
(fact "generated body-param is found in Definitions"
(find-definition spec body-ref) => truthy)
(fact "generated return-param is found in Definitions"
return-ref => truthy
(find-definition spec body-ref) => truthy))))))
(def Boundary
{:type (s/enum "MultiPolygon" "Polygon" "MultiPoint" "Point")
:coordinates [s/Any]})
(def ReturnValue
{:boundary (s/maybe Boundary)})
(facts "https://github.com/metosin/compojure-api/issues/53"
(let [app (api
(swagger-routes)
(POST "/" []
:return ReturnValue
:body [_ Boundary]
identity))]
(fact "api-docs"
(let [spec (get-spec app)]
(let [operation (some-> spec :paths vals first :post)
body-ref (some-> operation :parameters first :schema :$ref)
return-ref (get-in operation [:responses :200 :schema :$ref])]
(fact "generated body-param is found in Definitions"
(find-definition spec body-ref) => truthy)
(fact "generated return-param is found in Definitions"
return-ref => truthy
(find-definition spec body-ref) => truthy))))))
(s/defschema Urho {:kaleva {:kekkonen {s/Keyword s/Any}}})
(s/defschema Olipa {:kerran {:avaruus {s/Keyword s/Any}}})
; https://github.com/metosin/compojure-api/issues/94
(facts "preserves deeply nested schema names"
(let [app (api
(swagger-routes)
(POST "/" []
:return Urho
:body [_ Olipa]
identity))]
(fact "api-docs"
(let [spec (get-spec app)]
(fact "nested models are discovered correctly"
(-> spec :definitions keys set)
=> #{:Urho :UrhoKaleva :UrhoKalevaKekkonen
:Olipa :OlipaKerran :OlipaKerranAvaruus})))))
(fact "swagger-docs works with the :middleware"
(let [app (api
(swagger-routes)
(GET "/middleware" []
:query-params [x :- String]
:middleware [[constant-middleware (ok 1)]]
(ok 2)))]
(fact "api-docs"
(-> app get-spec :paths vals first)
=> {:get {:parameters [{:description ""
:in "query"
:name "x"
:required true
:type "string"}]
:responses {:default {:description ""}}}})))
(fact "sub-context paths"
(let [response {:ping "pong"}
ok (ok response)
ok? (fn [[status body]]
(and (= status 200)
(= body response)))
app (api
(swagger-routes {:ui nil})
(GET "/" [] ok)
(GET "/a" [] ok)
(context "/b" []
(context "/b1" []
(GET "/" [] ok))
(context "/" []
(GET "/" [] ok)
(GET "/b2" [] ok))))]
(fact "valid routes"
(get* app "/") => ok?
(get* app "/a") => ok?
(get* app "/b/b1") => ok?
(get* app "/b") => ok?
(get* app "/b/b2") => ok?)
(fact "undocumented compojure easter eggs"
(get* app "/b/b1/") => ok?
(get* app "/b/") => ok?
(fact "this is fixed in compojure 1.5.1"
(get* app "/b//") =not=> ok?))
(fact "swagger-docs have trailing slashes removed"
(->> app get-spec :paths keys)
=> ["/" "/a" "/b/b1" "/b" "/b/b2"])))
(fact "formats supported by ring-middleware-format"
(let [app (api
(POST "/echo" []
:body-params [foo :- String]
(ok {:foo foo})))]
(tabular
(facts
(fact {:midje/description (str ?content-type " to json")}
(let [[status body]
(raw-post* app "/echo" ?body ?content-type {:accept "application/json"})]
status => 200
body => "{\"foo\":\"bar\"}"))
(fact {:midje/description (str "json to " ?content-type)}
(let [[status body]
(raw-post* app "/echo" "{\"foo\":\"bar\"}" "application/json" {:accept ?content-type})]
status => 200
body => ?body)))
?content-type ?body
"application/json" "{\"foo\":\"bar\"}"
"application/edn" "{:foo \"bar\"}"
"application/transit+json" "[\"^ \",\"~:foo\",\"bar\"]")))
(fact "multiple routes in context"
(let [app (api
(context "/foo" []
(GET "/bar" [] (ok ["bar"]))
(GET "/baz" [] (ok ["baz"]))))]
(fact "first route works"
(let [[status body] (get* app "/foo/bar")]
status => 200
body => ["bar"]))
(fact "second route works"
(let [[status body] (get* app "/foo/baz")]
status => 200
body => ["baz"]))))
(require '[compojure.api.test-domain :refer [Pizza burger-routes]])
(fact "external deep schemas"
(let [app (api
(swagger-routes)
burger-routes
(POST "/pizza" []
:return Pizza
:body [body Pizza]
(ok body)))]
(fact "direct route with nested named schema works when called"
(let [pizza {:toppings [{:name "cheese"}]}
[status body] (post* app "/pizza" (json-string pizza))]
status => 200
body => pizza))
(fact "defroute*'d route with nested named schema works when called"
(let [burger {:ingredients [{:name "beef"}, {:name "egg"}]}
[status body] (post* app "/burger" (json-string burger))]
status => 200
body => burger))
(fact "generates correct swagger-spec"
(-> app get-spec :definitions keys set) => #{:Topping :Pizza :Burger :Beef})))
(fact "multiple routes with same path & method in same file"
(let [app (api
(swagger-routes)
(GET "/ping" []
:summary "active-ping"
(ok {:ping "active"}))
(GET "/ping" []
:summary "passive-ping"
(ok {:ping "passive"})))]
(fact "first route matches with Compojure"
(let [[status body] (get* app "/ping" {})]
status => 200
body => {:ping "active"}))
(fact "generates correct swagger-spec"
(-> app get-spec :paths vals first :get :summary) => "active-ping")))
(fact "multiple routes with same path & method over context"
(let [app (api
(swagger-routes)
(context "/api" []
(context "/ipa" []
(GET "/ping" []
:summary "active-ping"
(ok {:ping "active"}))))
(context "/api" []
(context "/ipa" []
(GET "/ping" []
:summary "passive-ping"
(ok {:ping "passive"})))))]
(fact "first route matches with Compojure"
(let [[status body] (get* app "/api/ipa/ping" {})]
status => 200
body => {:ping "active"}))
(fact "generates correct swagger-spec"
(-> app get-spec :paths vals first :get :summary) => "active-ping")))
(fact "multiple routes with same overall path (with different path sniplets & method over context"
(let [app (api
(swagger-routes)
(context "/api/ipa" []
(GET "/ping" []
:summary "active-ping"
(ok {:ping "active"})))
(context "/api" []
(context "/ipa" []
(GET "/ping" []
:summary "passive-ping"
(ok {:ping "passive"})))))]
(fact "first route matches with Compojure"
(let [[status body] (get* app "/api/ipa/ping" {})]
status => 200
body => {:ping "active"}))
(fact "generates correct swagger-spec"
(-> app get-spec :paths vals first :get :summary) => "active-ping")))
; https://github.com/metosin/compojure-api/issues/98
; https://github.com/metosin/compojure-api/issues/134
(fact "basePath"
(let [app (api (swagger-routes))]
(fact "no context"
(-> app get-spec :basePath) => "/")
(fact "app-servers with given context"
(against-background (rsc/context anything) => "/v2")
(-> app get-spec :basePath) => "/v2"))
(let [app (api (swagger-routes {:data {:basePath "/serve/from/here"}}))]
(fact "override it"
(-> app get-spec :basePath) => "/serve/from/here"))
(let [app (api (swagger-routes {:data {:basePath "/"}}))]
(fact "can set it to the default"
(-> app get-spec :basePath) => "/")))
(fact "multiple different models with same name"
(fact "schemas with same regexps are not equal"
{:d #"\D"} =not=> {:d #"\D"})
(fact "api-spec with 2 schemas with non-equal contents"
(let [app (api
(swagger-routes)
(GET "/" []
:responses {200 {:schema (s/schema-with-name {:a {:d #"\D"}} "Kikka")}
201 {:schema (s/schema-with-name {:a {:d #"\D"}} "Kikka")}}
identity))]
(fact "api spec doesn't fail (#102)"
(get-spec app) => anything))))
(def over-the-hills-and-far-away
(POST "/" []
:body-params [a :- s/Str]
identity))
(fact "anonymous body models over defined routes"
(let [app (api
(swagger-routes)
over-the-hills-and-far-away)]
(fact "generated model doesn't have namespaced keys"
(-> app get-spec :definitions vals first :properties keys first) => :a)))
(def foo
(GET "/foo" []
(let [foo {:foo "bar"}]
(ok foo))))
(fact "defroutes with local symbol usage with same name (#123)"
(let [app (api
foo)]
(let [[status body] (get* app "/foo")]
status => 200
body => {:foo "bar"})))
(def response-descriptions-routes
(GET "/x" []
:responses {500 {:schema {:code String}
:description "Horror"}}
identity))
(fact "response descriptions"
(let [app (api
(swagger-routes)
response-descriptions-routes)]
(-> app get-spec :paths vals first :get :responses :500 :description) => "Horror"))
(fact "exceptions options with custom validation error handler"
(let [app (api
{:exceptions {:handlers {::ex/request-validation custom-validation-error-handler
::ex/request-parsing custom-validation-error-handler
::ex/response-validation custom-validation-error-handler}}}
(swagger-routes)
(POST "/get-long" []
:body [body {:x Long}]
:return Long
(case (:x body)
1 (ok 1)
(ok "not a number"))))]
(fact "return case, valid request & valid model"
(let [[status body] (post* app "/get-long" "{\"x\": 1}")]
status => 200
body => 1))
(fact "return case, not schema valid request"
(let [[status body] (post* app "/get-long" "{\"x\": \"1\"}")]
status => 400
body => (contains {:custom-error "/get-long"})))
(fact "return case, invalid json request"
(let [[status body] (post* app "/get-long" "{x: 1}")]
status => 400
body => (contains {:custom-error "/get-long"})))
(fact "return case, valid request & invalid model"
(let [[status body] (post* app "/get-long" "{\"x\": 2}")]
status => 501
body => (contains {:custom-error "/get-long"})))))