@@ -6,11 +6,12 @@ package requests
6
6
import (
7
7
"bytes"
8
8
"encoding/json"
9
+ "net/http"
10
+ "testing"
11
+
9
12
"github.com/pb33f/libopenapi"
10
13
"github.com/pb33f/libopenapi-validator/paths"
11
14
"github.com/stretchr/testify/assert"
12
- "net/http"
13
- "testing"
14
15
)
15
16
16
17
func TestValidateBody_MissingContentType (t * testing.T ) {
@@ -58,6 +59,47 @@ paths:
58
59
"supported types for this operation: application/json" , errors [0 ].HowToFix )
59
60
}
60
61
62
+ func TestValidateBody_SkipValidationForNonJSON (t * testing.T ) {
63
+ spec := `openapi: 3.1.0
64
+ paths:
65
+ /burgers/createBurger:
66
+ post:
67
+ requestBody:
68
+ content:
69
+ application/yaml:
70
+ schema:
71
+ type: object
72
+ properties:
73
+ name:
74
+ type: string
75
+ patties:
76
+ type: integer
77
+ vegetarian:
78
+ type: boolean`
79
+
80
+ doc , _ := libopenapi .NewDocument ([]byte (spec ))
81
+
82
+ m , _ := doc .BuildV3Model ()
83
+ v := NewRequestBodyValidator (& m .Model )
84
+
85
+ body := map [string ]interface {}{
86
+ "name" : "Big Mac" ,
87
+ "patties" : false ,
88
+ "vegetarian" : 2 ,
89
+ }
90
+
91
+ bodyBytes , _ := json .Marshal (body )
92
+
93
+ request , _ := http .NewRequest (http .MethodPost , "https://things.com/burgers/createBurger" ,
94
+ bytes .NewBuffer (bodyBytes ))
95
+ request .Header .Set ("Content-Type" , "application/yaml" )
96
+
97
+ valid , errors := v .ValidateRequestBody (request )
98
+
99
+ assert .True (t , valid )
100
+ assert .Len (t , errors , 0 )
101
+ }
102
+
61
103
func TestValidateBody_PathNotFound (t * testing.T ) {
62
104
spec := `openapi: 3.1.0
63
105
paths:
@@ -195,6 +237,51 @@ paths:
195
237
196
238
}
197
239
240
+ func TestValidateBody_ContentTypeNotSet (t * testing.T ) {
241
+ spec := `openapi: 3.1.0
242
+ paths:
243
+ /burgers/createBurger:
244
+ post:
245
+ requestBody:
246
+ content:
247
+ application/json:
248
+ schema:
249
+ type: object
250
+ properties:
251
+ name:
252
+ type: string
253
+ patties:
254
+ type: integer
255
+ vegetarian:
256
+ type: boolean`
257
+
258
+ doc , _ := libopenapi .NewDocument ([]byte (spec ))
259
+
260
+ m , _ := doc .BuildV3Model ()
261
+ v := NewRequestBodyValidator (& m .Model )
262
+
263
+ body := map [string ]interface {}{
264
+ "name" : "Big Mac" ,
265
+ "patties" : 2 ,
266
+ "vegetarian" : true ,
267
+ }
268
+
269
+ bodyBytes , _ := json .Marshal (body )
270
+
271
+ request , _ := http .NewRequest (http .MethodPost , "https://things.com/burgers/createBurger" ,
272
+ bytes .NewBuffer (bodyBytes ))
273
+
274
+ // preset the path
275
+ path , _ , pv := paths .FindPath (request , & m .Model )
276
+ v .SetPathItem (path , pv )
277
+
278
+ valid , errors := v .ValidateRequestBody (request )
279
+
280
+ assert .False (t , valid )
281
+ assert .Len (t , errors , 1 )
282
+
283
+ }
284
+
198
285
func TestValidateBody_InvalidBasicSchema (t * testing.T ) {
199
286
spec := `openapi: 3.1.0
200
287
paths:
@@ -750,6 +837,53 @@ components:
750
837
assert .Equal (t , 11 , errors [0 ].SchemaValidationErrors [0 ].Column )
751
838
}
752
839
840
+ func TestValidateBody_SchemaHasNoRequestBody (t * testing.T ) {
841
+ spec := `openapi: 3.1.0
842
+ paths:
843
+ /burgers/createBurger:
844
+ post:`
845
+
846
+ doc , _ := libopenapi .NewDocument ([]byte (spec ))
847
+
848
+ m , _ := doc .BuildV3Model ()
849
+ v := NewRequestBodyValidator (& m .Model )
850
+
851
+ request , _ := http .NewRequest (http .MethodPost , "https://things.com/burgers/createBurger" ,
852
+ http .NoBody )
853
+ request .Header .Set ("Content-Type" , "application/json" )
854
+
855
+ valid , errors := v .ValidateRequestBody (request )
856
+
857
+ assert .True (t , valid )
858
+ assert .Len (t , errors , 0 )
859
+
860
+ }
861
+
862
+ func TestValidateBody_MediaTypeHasNullSchema (t * testing.T ) {
863
+ spec := `openapi: 3.1.0
864
+ paths:
865
+ /burgers/createBurger:
866
+ post:
867
+ requestBody:
868
+ content:
869
+ application/json:`
870
+
871
+ doc , _ := libopenapi .NewDocument ([]byte (spec ))
872
+
873
+ m , _ := doc .BuildV3Model ()
874
+ v := NewRequestBodyValidator (& m .Model )
875
+
876
+ request , _ := http .NewRequest (http .MethodPost , "https://things.com/burgers/createBurger" ,
877
+ http .NoBody )
878
+ request .Header .Set ("Content-Type" , "application/json" )
879
+
880
+ valid , errors := v .ValidateRequestBody (request )
881
+
882
+ assert .True (t , valid )
883
+ assert .Len (t , errors , 0 )
884
+
885
+ }
886
+
753
887
func TestValidateBody_MissingBody (t * testing.T ) {
754
888
spec := `openapi: 3.1.0
755
889
paths:
0 commit comments