-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
option_test.go
909 lines (741 loc) · 28.3 KB
/
option_test.go
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
package fuego_test
import (
"errors"
"log/slog"
"net/http"
"net/http/httptest"
"testing"
"github.com/getkin/kin-openapi/openapi3"
"github.com/stretchr/testify/require"
"github.com/thejerf/slogassert"
"github.com/go-fuego/fuego"
"github.com/go-fuego/fuego/option"
"github.com/go-fuego/fuego/param"
)
// dummyMiddleware sets the X-Test header on the request and the X-Test-Response header on the response.
func dummyMiddleware(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Header.Set("X-Test", "test")
w.Header().Set("X-Test-Response", "response")
handler.ServeHTTP(w, r)
})
}
func helloWorld(ctx fuego.ContextNoBody) (string, error) {
return "hello world", nil
}
type ReqBody struct {
A string
B int
}
type Resp struct {
Message string `json:"message"`
}
func dummyController(_ fuego.ContextWithBody[ReqBody]) (Resp, error) {
return Resp{Message: "hello world"}, nil
}
// orderMiddleware sets the X-Test-Order Header on the request and
// X-Test-Response header on the response. It is
// used to test the order execution of our middleware
func orderMiddleware(s string) func(http.Handler) http.Handler {
return func(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Header.Add("X-Test-Order", s)
w.Header().Set("X-Test-Response", "response")
handler.ServeHTTP(w, r)
})
}
}
func TestPerRouteMiddleware(t *testing.T) {
s := fuego.NewServer()
fuego.Get(s, "/withMiddleware", func(ctx fuego.ContextNoBody) (string, error) {
return "withmiddleware", nil
}, fuego.OptionMiddleware(dummyMiddleware))
fuego.Get(s, "/withoutMiddleware", func(ctx fuego.ContextNoBody) (string, error) {
return "withoutmiddleware", nil
})
t.Run("withMiddleware", func(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/withMiddleware", nil)
w := httptest.NewRecorder()
s.Mux.ServeHTTP(w, r)
require.Equal(t, "withmiddleware", w.Body.String())
require.Equal(t, "response", w.Header().Get("X-Test-Response"))
})
t.Run("withoutMiddleware", func(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/withoutMiddleware", nil)
w := httptest.NewRecorder()
s.Mux.ServeHTTP(w, r)
require.Equal(t, "withoutmiddleware", w.Body.String())
require.Equal(t, "", w.Header().Get("X-Test-Response"))
})
}
func TestUse(t *testing.T) {
t.Run("base", func(t *testing.T) {
s := fuego.NewServer()
fuego.Use(s, orderMiddleware("First!"))
fuego.Get(s, "/test", func(ctx fuego.ContextNoBody) (string, error) {
return "test", nil
})
r := httptest.NewRequest(http.MethodGet, "/test", nil)
r.Header.Set("X-Test-Order", "Start!")
w := httptest.NewRecorder()
s.Mux.ServeHTTP(w, r)
require.Equal(t, []string{"Start!", "First!"}, r.Header["X-Test-Order"])
})
t.Run("multiple uses of Use", func(t *testing.T) {
s := fuego.NewServer()
fuego.Use(s, orderMiddleware("First!"))
fuego.Use(s, orderMiddleware("Second!"))
fuego.Get(s, "/test", func(ctx fuego.ContextNoBody) (string, error) {
return "test", nil
})
r := httptest.NewRequest(http.MethodGet, "/test", nil)
r.Header.Set("X-Test-Order", "Start!")
w := httptest.NewRecorder()
s.Mux.ServeHTTP(w, r)
require.Equal(t, []string{"Start!", "First!", "Second!"}, r.Header["X-Test-Order"])
})
t.Run("variadic use of Use", func(t *testing.T) {
s := fuego.NewServer()
fuego.Use(s, orderMiddleware("First!"))
fuego.Use(s, orderMiddleware("Second!"), orderMiddleware("Third!"))
fuego.Get(s, "/test", func(ctx fuego.ContextNoBody) (string, error) {
return "test", nil
})
r := httptest.NewRequest(http.MethodGet, "/test", nil)
r.Header.Set("X-Test-Order", "Start!")
w := httptest.NewRecorder()
s.Mux.ServeHTTP(w, r)
require.Equal(t, []string{"Start!", "First!", "Second!", "Third!"}, r.Header["X-Test-Order"])
})
t.Run("variadic use of Route Get", func(t *testing.T) {
s := fuego.NewServer()
fuego.Use(s, orderMiddleware("First!"))
fuego.Use(s, orderMiddleware("Second!"), orderMiddleware("Third!"))
fuego.Get(s, "/test", func(ctx fuego.ContextNoBody) (string, error) {
return "test", nil
},
fuego.OptionMiddleware(orderMiddleware("Fourth!")),
fuego.OptionMiddleware(orderMiddleware("Fifth!")),
)
r := httptest.NewRequest(http.MethodGet, "/test", nil)
r.Header.Set("X-Test-Order", "Start!")
w := httptest.NewRecorder()
s.Mux.ServeHTTP(w, r)
require.Equal(t, []string{"Start!", "First!", "Second!", "Third!", "Fourth!", "Fifth!"}, r.Header["X-Test-Order"])
})
}
type ans struct{}
func TestOptions(t *testing.T) {
t.Run("warn if param is not found in openAPI config but called in controller (possibly typo)", func(t *testing.T) {
handler := slogassert.New(t, slog.LevelWarn, nil)
s := fuego.NewServer(
fuego.WithLogHandler(handler),
)
fuego.Get(s, "/correct", func(c fuego.ContextNoBody) (ans, error) {
c.QueryParam("quantity")
return ans{}, nil
},
fuego.OptionQuery("quantity", "some description"),
fuego.OptionQueryInt("number", "some description", param.Example("3", 3)),
fuego.OptionQueryBool("is_active", "some description"),
)
fuego.Get(s, "/typo", func(c fuego.ContextNoBody) (ans, error) {
c.QueryParam("quantityy-with-a-typo")
return ans{}, nil
},
fuego.OptionQuery("quantity", "some description"),
)
t.Run("correct param", func(t *testing.T) {
r := httptest.NewRequest("GET", "/correct", nil)
w := httptest.NewRecorder()
s.Mux.ServeHTTP(w, r)
// all log messages have been accounted for
handler.AssertEmpty()
})
t.Run("typo param", func(t *testing.T) {
r := httptest.NewRequest("GET", "/typo", nil)
w := httptest.NewRecorder()
s.Mux.ServeHTTP(w, r)
handler.AssertMessage("query parameter not expected in OpenAPI spec")
// all log messages have been accounted for
handler.AssertEmpty()
})
})
}
func TestHeader(t *testing.T) {
t.Run("Declare a header parameter for the route", func(t *testing.T) {
s := fuego.NewServer()
fuego.Get(s, "/test", helloWorld,
fuego.OptionHeader("X-Test", "test header", param.Required(), param.Example("test", "My Header"), param.Default("test")),
)
r := httptest.NewRequest(http.MethodGet, "/test", nil)
w := httptest.NewRecorder()
s.Mux.ServeHTTP(w, r)
require.Equal(t, "hello world", w.Body.String())
})
}
func TestOpenAPI(t *testing.T) {
t.Run("Declare a openapi parameters for the route", func(t *testing.T) {
s := fuego.NewServer()
route := fuego.Get(s, "/test", helloWorld,
fuego.OptionSummary("test summary"),
fuego.OptionDescription("test description"),
fuego.OptionTags("first-tag", "second-tag"),
fuego.OptionDeprecated(),
fuego.OptionOperationID("test-operation-id"),
)
require.Equal(t, "test summary", route.Operation.Summary)
require.Equal(t, "#### Controller: \n\n`github.com/go-fuego/fuego_test.helloWorld`\n\n---\n\ntest description", route.Operation.Description)
require.Equal(t, []string{"first-tag", "second-tag"}, route.Operation.Tags)
require.True(t, route.Operation.Deprecated)
})
}
func TestGroup(t *testing.T) {
paramsGroup := fuego.GroupOptions(
fuego.OptionHeader("X-Test", "test header", param.Required(), param.Example("test", "My Header"), param.Default("test")),
fuego.OptionQuery("name", "Filter by name", param.Example("cat name", "felix"), param.Nullable()),
fuego.OptionCookie("session", "Session cookie", param.Example("session", "1234"), param.Nullable()),
)
t.Run("Declare a group parameter for the route", func(t *testing.T) {
s := fuego.NewServer()
route := fuego.Get(s, "/test", helloWorld, paramsGroup)
require.NotNil(t, route)
require.NotNil(t, route.Params)
require.Len(t, route.Params, 3)
require.Equal(t, "test header", route.Params["X-Test"].Description)
require.Equal(t, "My Header", route.Operation.Parameters.GetByInAndName("header", "X-Test").Examples["test"].Value.Value)
})
}
func TestQuery(t *testing.T) {
t.Run("panics if example is not the correct type", func(t *testing.T) {
s := fuego.NewServer()
require.Panics(t, func() {
fuego.Get(s, "/test", helloWorld,
fuego.OptionQueryInt("age", "Filter by age (in years)", param.Example("3 years old", "3 but string"), param.Nullable()),
)
})
require.Panics(t, func() {
fuego.Get(s, "/test", helloWorld,
fuego.OptionQueryBool("is_active", "Filter by active status", param.Example("true", 3), param.Nullable()),
)
})
})
t.Run("panics if default value is not the correct type", func(t *testing.T) {
s := fuego.NewServer()
require.Panics(t, func() {
fuego.Get(s, "/test", helloWorld,
fuego.OptionQuery("name", "Filter by name", param.Default(3), param.Nullable()),
)
})
})
}
func TestPath(t *testing.T) {
t.Run("Path parameter is automatically declared for the route", func(t *testing.T) {
s := fuego.NewServer()
fuego.Get(s, "/test/{id}", helloWorld)
require.Equal(t, "id", s.OpenAPI.Description().Paths.Find("/test/{id}").Get.Parameters.GetByInAndName("path", "id").Name)
require.Equal(t, "", s.OpenAPI.Description().Paths.Find("/test/{id}").Get.Parameters.GetByInAndName("path", "id").Description)
})
t.Run("Declare explicitly an existing path parameter for the route", func(t *testing.T) {
s := fuego.NewServer()
fuego.Get(s, "/test/{id}", helloWorld,
fuego.OptionPath("id", "some id", param.Example("123", "123"), param.Nullable()),
)
require.Equal(t, "id", s.OpenAPI.Description().Paths.Find("/test/{id}").Get.Parameters.GetByInAndName("path", "id").Name)
require.Equal(t, "some id", s.OpenAPI.Description().Paths.Find("/test/{id}").Get.Parameters.GetByInAndName("path", "id").Description)
require.Equal(t, true, s.OpenAPI.Description().Paths.Find("/test/{id}").Get.Parameters.GetByInAndName("path", "id").Required, "path parameter is forced to be required")
})
t.Run("Declare explicitly a non-existing path parameter for the route panics", func(t *testing.T) {
s := fuego.NewServer()
require.Panics(t, func() {
fuego.Get(s, "/test/{id}", helloWorld,
fuego.OptionPath("not-existing-in-url", "some id"),
)
})
})
}
func TestRequestContentType(t *testing.T) {
t.Run("Declare a request content type for the route", func(t *testing.T) {
s := fuego.NewServer()
route := fuego.Get(s, "/test", dummyController, fuego.OptionRequestContentType("application/json"))
r := httptest.NewRequest(http.MethodGet, "/test", nil)
w := httptest.NewRecorder()
s.Mux.ServeHTTP(w, r)
require.Equal(t, "{\"message\":\"hello world\"}\n", w.Body.String())
require.Len(t, route.AcceptedContentTypes, 1)
require.Equal(t, "application/json", route.AcceptedContentTypes[0])
})
t.Run("base", func(t *testing.T) {
s := fuego.NewServer()
route := fuego.Post(s, "/base", dummyController,
fuego.OptionRequestContentType("application/json"),
)
t.Log("route.Operation", route.Operation)
content := route.Operation.RequestBody.Value.Content
require.NotNil(t, content.Get("application/json"))
require.Nil(t, content.Get("application/xml"))
require.Equal(t, "#/components/schemas/ReqBody", content.Get("application/json").Schema.Ref)
_, ok := s.OpenAPI.Description().Components.RequestBodies["ReqBody"]
require.False(t, ok)
})
t.Run("variadic", func(t *testing.T) {
s := fuego.NewServer()
route := fuego.Post(s, "/test", dummyController,
fuego.OptionRequestContentType("application/json", "my/content-type"),
)
content := route.Operation.RequestBody.Value.Content
require.NotNil(t, content.Get("application/json"))
require.NotNil(t, content.Get("my/content-type"))
require.Nil(t, content.Get("application/xml"))
require.Equal(t, "#/components/schemas/ReqBody", content.Get("application/json").Schema.Ref)
require.Equal(t, "#/components/schemas/ReqBody", content.Get("my/content-type").Schema.Ref)
_, ok := s.OpenAPI.Description().Components.RequestBodies["ReqBody"]
require.False(t, ok)
})
t.Run("override server", func(t *testing.T) {
s := fuego.NewServer(fuego.WithRequestContentType("application/json", "application/xml"))
route := fuego.Post(
s, "/test", dummyController,
fuego.OptionRequestContentType("my/content-type"),
)
content := route.Operation.RequestBody.Value.Content
require.Nil(t, content.Get("application/json"))
require.Nil(t, content.Get("application/xml"))
require.NotNil(t, content.Get("my/content-type"))
require.Equal(t, "#/components/schemas/ReqBody", content.Get("my/content-type").Schema.Ref)
_, ok := s.OpenAPI.Description().Components.RequestBodies["ReqBody"]
require.False(t, ok)
})
}
func TestAddError(t *testing.T) {
t.Run("Declare an error for the route", func(t *testing.T) {
s := fuego.NewServer()
route := fuego.Get(s, "/test", helloWorld, fuego.OptionAddError(http.StatusConflict, "Conflict: Pet with the same name already exists"))
t.Log("route.Operation.Responses", route.Operation.Responses)
require.Equal(t, 5, route.Operation.Responses.Len()) // 200, 400, 409, 500, default
resp := route.Operation.Responses.Value("409")
require.NotNil(t, resp)
require.Equal(t, "Conflict: Pet with the same name already exists", *route.Operation.Responses.Value("409").Value.Description)
})
t.Run("should be fatal", func(t *testing.T) {
s := fuego.NewServer()
require.Panics(t, func() {
fuego.Get(s, "/test", helloWorld, fuego.OptionAddError(http.StatusConflict, "err", Resp{}, Resp{}))
})
})
}
func TestAddResponse(t *testing.T) {
t.Run("base", func(t *testing.T) {
s := fuego.NewServer()
route := fuego.Get(s, "/test", helloWorld, fuego.OptionAddResponse(
http.StatusConflict,
"Conflict: Pet with the same name already exists",
fuego.Response{
ContentTypes: []string{"application/json"},
Type: fuego.HTTPError{},
},
))
require.Equal(t, 5, route.Operation.Responses.Len()) // 200, 400, 409, 500, default
resp := route.Operation.Responses.Value("409")
require.NotNil(t, resp)
require.NotNil(t, resp.Value.Content.Get("application/json"))
require.Nil(t, resp.Value.Content.Get("application/xml"))
require.Equal(t, "Conflict: Pet with the same name already exists", *route.Operation.Responses.Value("409").Value.Description)
})
t.Run("no content types provided", func(t *testing.T) {
s := fuego.NewServer()
route := fuego.Get(s, "/test", helloWorld, fuego.OptionAddResponse(
http.StatusConflict,
"Conflict: Pet with the same name already exists",
fuego.Response{
Type: fuego.HTTPError{},
},
))
require.Equal(t, 5, route.Operation.Responses.Len()) // 200, 400, 409, 500, default
resp := route.Operation.Responses.Value("409")
require.NotNil(t, resp)
require.NotNil(t, resp.Value.Content.Get("application/json"))
require.NotNil(t, resp.Value.Content.Get("application/xml"))
require.Equal(t, "Conflict: Pet with the same name already exists", *route.Operation.Responses.Value("409").Value.Description)
})
t.Run("should override 200", func(t *testing.T) {
s := fuego.NewServer()
route := fuego.Get(s, "/test", helloWorld, fuego.OptionAddResponse(
http.StatusOK,
"set 200",
fuego.Response{
Type: fuego.HTTPError{},
ContentTypes: []string{"application/x-yaml"},
},
))
require.Equal(t, 4, route.Operation.Responses.Len()) // 200, 400, 500, default
resp := route.Operation.Responses.Value("200")
require.NotNil(t, resp)
require.Nil(t, resp.Value.Content.Get("application/json"))
require.Nil(t, resp.Value.Content.Get("application/xml"))
require.NotNil(t, resp.Value.Content.Get("application/x-yaml"))
require.Equal(t, "#/components/schemas/HTTPError", resp.Value.Content.Get("application/x-yaml").Schema.Ref)
require.Equal(t, "set 200", *route.Operation.Responses.Value("200").Value.Description)
})
t.Run("should be fatal", func(t *testing.T) {
s := fuego.NewServer()
require.Panics(t, func() {
fuego.Get(s, "/test", helloWorld, fuego.OptionAddResponse(
http.StatusConflict,
"Conflict: Pet with the same name already exists",
fuego.Response{},
))
})
})
}
func TestHide(t *testing.T) {
s := fuego.NewServer()
fuego.Get(s, "/hidden", helloWorld, fuego.OptionHide())
fuego.Get(s, "/visible", helloWorld)
spec := s.OutputOpenAPISpec()
pathItemVisible := spec.Paths.Find("/visible")
require.NotNil(t, pathItemVisible)
pathItemHidden := spec.Paths.Find("/hidden")
require.Nil(t, pathItemHidden)
t.Run("visible route works normally", func(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/visible", nil)
w := httptest.NewRecorder()
s.Mux.ServeHTTP(w, r)
require.Equal(t, 200, w.Code)
require.Equal(t, "hello world", w.Body.String())
})
t.Run("hidden route still accessible even if not in openAPI spec", func(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/hidden", nil)
w := httptest.NewRecorder()
s.Mux.ServeHTTP(w, r)
require.Equal(t, 200, w.Code)
require.Equal(t, "hello world", w.Body.String())
})
}
func TestOptionResponseHeader(t *testing.T) {
t.Run("Declare a response header for the route", func(t *testing.T) {
s := fuego.NewServer()
route := fuego.Get(s, "/test", helloWorld,
fuego.OptionResponseHeader("X-Test", "test header", param.Example("test", "My Header"), param.Default("test"), param.Description("test description")),
)
require.NotNil(t, route.Operation.Responses.Value("200").Value.Headers["X-Test"])
require.Equal(t, "My Header", route.Operation.Responses.Value("200").Value.Headers["X-Test"].Value.Examples["test"].Value.Value)
require.Equal(t, "test description", route.Operation.Responses.Value("200").Value.Headers["X-Test"].Value.Description)
})
t.Run("Declare a response header for the route with multiple status codes", func(t *testing.T) {
s := fuego.NewServer()
route := fuego.Get(s, "/test", helloWorld,
fuego.OptionResponseHeader("X-Test", "test header", param.StatusCodes(200, 206)),
)
require.NotNil(t, route.Operation.Responses.Value("200").Value.Headers["X-Test"])
require.NotNil(t, route.Operation.Responses.Value("206").Value.Headers["X-Test"])
require.Nil(t, route.Operation.Responses.Value("400").Value.Headers["X-Test"])
})
}
func TestSecurity(t *testing.T) {
t.Run("single security requirement with defined scheme", func(t *testing.T) {
s := fuego.NewServer(
fuego.WithSecurity(openapi3.SecuritySchemes{
"basic": &openapi3.SecuritySchemeRef{
Value: openapi3.NewSecurityScheme().
WithType("http").
WithScheme("basic"),
},
}),
)
basic := openapi3.SecurityRequirement{
"basic": []string{},
}
route := fuego.Get(s, "/test", helloWorld,
fuego.OptionSecurity(basic),
)
require.NotNil(t, route.Operation.Security)
require.Len(t, *route.Operation.Security, 1)
require.Contains(t, (*route.Operation.Security)[0], "basic")
require.Empty(t, (*route.Operation.Security)[0]["basic"])
r := httptest.NewRequest(http.MethodGet, "/test", nil)
w := httptest.NewRecorder()
s.Mux.ServeHTTP(w, r)
require.Equal(t, "hello world", w.Body.String())
})
t.Run("security with scopes and defined scheme", func(t *testing.T) {
s := fuego.NewServer(
fuego.WithSecurity(openapi3.SecuritySchemes{
"oauth2": &openapi3.SecuritySchemeRef{
Value: &openapi3.SecurityScheme{
Type: "oauth2",
Flows: &openapi3.OAuthFlows{
AuthorizationCode: &openapi3.OAuthFlow{
AuthorizationURL: "https://example.com/oauth/authorize",
TokenURL: "https://example.com/oauth/token",
Scopes: map[string]string{
"read:users": "Read user information",
},
},
},
},
},
}),
)
route := fuego.Get(s, "/test", helloWorld,
fuego.OptionSecurity(
openapi3.SecurityRequirement{
"oauth2": []string{"read:users", "write:users"},
},
),
)
require.NotNil(t, route.Operation.Security)
require.Len(t, *route.Operation.Security, 1)
require.Contains(t, (*route.Operation.Security)[0], "oauth2")
require.Equal(t,
[]string{"read:users", "write:users"},
(*route.Operation.Security)[0]["oauth2"],
)
})
t.Run("AND combination with defined schemes", func(t *testing.T) {
s := fuego.NewServer(
fuego.WithSecurity(openapi3.SecuritySchemes{
"basic": &openapi3.SecuritySchemeRef{
Value: openapi3.NewSecurityScheme().
WithType("http").
WithScheme("basic"),
},
"oauth2": &openapi3.SecuritySchemeRef{
Value: &openapi3.SecurityScheme{
Type: "oauth2",
Flows: &openapi3.OAuthFlows{
AuthorizationCode: &openapi3.OAuthFlow{
AuthorizationURL: "https://example.com/oauth/authorize",
TokenURL: "https://example.com/oauth/token",
Scopes: map[string]string{
"read:users": "Read user information",
},
},
},
},
},
}),
)
route := fuego.Get(s, "/test", helloWorld,
fuego.OptionSecurity(
openapi3.SecurityRequirement{
"basic": []string{},
"oauth2": []string{"read:users"},
},
),
)
require.NotNil(t, route.Operation.Security)
require.Len(t, *route.Operation.Security, 1)
require.Contains(t, (*route.Operation.Security)[0], "basic")
require.Empty(t, (*route.Operation.Security)[0]["basic"])
require.Contains(t, (*route.Operation.Security)[0], "oauth2")
require.Equal(t, []string{"read:users"}, (*route.Operation.Security)[0]["oauth2"])
})
t.Run("OR combination with defined schemes", func(t *testing.T) {
s := fuego.NewServer(
fuego.WithSecurity(openapi3.SecuritySchemes{
"basic": &openapi3.SecuritySchemeRef{
Value: openapi3.NewSecurityScheme().
WithType("http").
WithScheme("basic"),
},
"oauth2": &openapi3.SecuritySchemeRef{
Value: &openapi3.SecurityScheme{
Type: "oauth2",
Flows: &openapi3.OAuthFlows{
AuthorizationCode: &openapi3.OAuthFlow{
AuthorizationURL: "https://example.com/oauth/authorize",
TokenURL: "https://example.com/oauth/token",
Scopes: map[string]string{
"read:users": "Read user information",
},
},
},
},
},
}),
)
route := fuego.Get(s, "/test", helloWorld,
fuego.OptionSecurity(
openapi3.SecurityRequirement{
"basic": []string{},
},
openapi3.SecurityRequirement{
"oauth2": []string{"read:users"},
},
),
)
require.NotNil(t, route.Operation.Security)
require.Len(t, *route.Operation.Security, 2)
require.Contains(t, (*route.Operation.Security)[0], "basic")
require.Empty(t, (*route.Operation.Security)[0]["basic"])
require.Contains(t, (*route.Operation.Security)[1], "oauth2")
require.Equal(t, []string{"read:users"}, (*route.Operation.Security)[1]["oauth2"])
})
t.Run("panic on undefined security scheme", func(t *testing.T) {
s := fuego.NewServer()
require.Panics(t, func() {
fuego.Get(s, "/test", helloWorld,
fuego.OptionSecurity(
openapi3.SecurityRequirement{
"undefined": []string{},
},
),
)
})
})
t.Run("panic on partially undefined schemes", func(t *testing.T) {
s := fuego.NewServer(
fuego.WithSecurity(openapi3.SecuritySchemes{
"basic": &openapi3.SecuritySchemeRef{
Value: openapi3.NewSecurityScheme().
WithType("http").
WithScheme("basic"),
},
}),
)
require.Panics(t, func() {
fuego.Get(s, "/test", helloWorld,
fuego.OptionSecurity(
openapi3.SecurityRequirement{
"basic": []string{},
"undefined": []string{},
},
),
)
})
})
t.Run("empty security options", func(t *testing.T) {
s := fuego.NewServer()
route := fuego.Get(s, "/test", helloWorld,
fuego.OptionSecurity(),
)
require.NotNil(t, route.Operation.Security)
require.Empty(t, (*route.Operation.Security))
})
t.Run("multiple security options with different scopes", func(t *testing.T) {
s := fuego.NewServer(
fuego.WithSecurity(openapi3.SecuritySchemes{
"Bearer": &openapi3.SecuritySchemeRef{
Value: openapi3.NewSecurityScheme().
WithType("http").
WithScheme("bearer"),
},
"ApiKey": &openapi3.SecuritySchemeRef{
Value: openapi3.NewSecurityScheme().
WithType("apiKey").
WithIn("header").
WithName("X-API-Key"),
},
}),
)
route := fuego.Get(s, "/test", helloWorld,
fuego.OptionSecurity(
openapi3.SecurityRequirement{
"Bearer": []string{"read"},
"ApiKey": []string{"basic"},
},
),
)
require.NotNil(t, route.Operation.Security)
require.Len(t, *route.Operation.Security, 1)
security := (*route.Operation.Security)[0]
require.Contains(t, security, "Bearer")
require.Equal(t, []string{"read"}, security["Bearer"])
require.Contains(t, security, "ApiKey")
require.Equal(t, []string{"basic"}, security["ApiKey"])
})
}
func TestOptionDescription(t *testing.T) {
t.Run("Declare a description for the route", func(t *testing.T) {
s := fuego.NewServer()
route := fuego.Get(s, "/test", helloWorld,
option.Description("test description"),
)
require.Equal(t, "#### Controller: \n\n`github.com/go-fuego/fuego_test.helloWorld`\n\n---\n\ntest description", route.Operation.Description)
})
t.Run("Override Fuego's description for the route", func(t *testing.T) {
s := fuego.NewServer()
route := fuego.Get(s, "/test", helloWorld,
option.OverrideDescription("another description"),
)
require.Equal(t, "another description", route.Operation.Description)
})
t.Run("Add description to the route, route middleware is included", func(t *testing.T) {
s := fuego.NewServer()
route := fuego.Get(s, "/test", helloWorld,
option.Middleware(dummyMiddleware),
option.Description("another description"),
)
require.Equal(t, "#### Controller: \n\n`github.com/go-fuego/fuego_test.helloWorld`\n\n#### Middlewares:\n\n- `github.com/go-fuego/fuego_test.dummyMiddleware`\n\n---\n\nanother description", route.Operation.Description)
})
t.Run("Add description to the route, route middleware is included", func(t *testing.T) {
s := fuego.NewServer()
fuego.Use(s, dummyMiddleware)
group := fuego.Group(s, "/group", option.Middleware(dummyMiddleware))
fuego.Use(group, dummyMiddleware)
route := fuego.Get(s, "/test", helloWorld,
option.Middleware(dummyMiddleware),
option.Description("another description"),
option.Middleware(dummyMiddleware), // After the description
option.Middleware(dummyMiddleware), // 6th middleware
option.Middleware(dummyMiddleware), // 7th middleware, should not be included
)
require.Equal(t, "#### Controller: \n\n`github.com/go-fuego/fuego_test.helloWorld`\n\n#### Middlewares:\n\n- `github.com/go-fuego/fuego_test.dummyMiddleware`\n- `github.com/go-fuego/fuego_test.dummyMiddleware`\n- `github.com/go-fuego/fuego_test.dummyMiddleware`\n- `github.com/go-fuego/fuego_test.dummyMiddleware`\n- `github.com/go-fuego/fuego_test.dummyMiddleware`\n- more middleware…\n\n---\n\nanother description", route.Operation.Description)
})
}
func TestDefaultStatusCode(t *testing.T) {
t.Run("Declare a default status code for the route", func(t *testing.T) {
s := fuego.NewServer()
route := fuego.Post(s, "/test", helloWorld,
option.DefaultStatusCode(201),
)
r := httptest.NewRequest(http.MethodPost, "/test", nil)
w := httptest.NewRecorder()
s.Mux.ServeHTTP(w, r)
require.Equal(t, 201, w.Code)
require.Equal(t, "hello world", w.Body.String())
require.Equal(t, 201, route.DefaultStatusCode)
require.NotNil(t, route.Operation.Responses.Value("201").Value)
})
t.Run("Declare a default status code for the route but bypass it in the controller", func(t *testing.T) {
s := fuego.NewServer()
route := fuego.Post(s, "/test", func(c fuego.ContextNoBody) (string, error) {
c.SetStatus(200)
return "hello world", nil
},
option.DefaultStatusCode(201),
)
r := httptest.NewRequest(http.MethodPost, "/test", nil)
w := httptest.NewRecorder()
s.Mux.ServeHTTP(w, r)
require.Equal(t, 200, w.Code)
require.Equal(t, "hello world", w.Body.String())
require.Equal(t, 201, route.DefaultStatusCode, "default status code should not be changed")
require.NotNil(t, route.Operation.Responses.Value("201").Value, "default status is still in the spec even if code is not used")
})
t.Run("can return 204 when no data is being sent", func(t *testing.T) {
s := fuego.NewServer()
fuego.Get(s, "/", func(_ fuego.ContextNoBody) (any, error) {
return nil, nil
},
option.DefaultStatusCode(204),
)
r := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
s.Mux.ServeHTTP(w, r)
require.Equal(t, 204, w.Code)
})
t.Run("must return 500 when an error is being sent, even with no body", func(t *testing.T) {
s := fuego.NewServer()
fuego.Get(s, "/", func(_ fuego.ContextNoBody) (any, error) {
return nil, errors.New("error")
},
option.DefaultStatusCode(204),
)
r := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
s.Mux.ServeHTTP(w, r)
require.Equal(t, 500, w.Code)
})
}