@@ -46,6 +46,15 @@ func TestMain(m *testing.M) {
4646 os .Exit (code )
4747}
4848
49+ func loadTestdata (t * testing.T , name string ) string {
50+ t .Helper ()
51+ b , err := os .ReadFile ("testdata/" + name )
52+ if err != nil {
53+ t .Fatalf ("cannot read testdata/%s: %v" , name , err )
54+ }
55+ return string (b )
56+ }
57+
4958func runTestCases (t * testing.T , tests []TestCase ) {
5059 for _ , test := range tests {
5160 description := test .description
@@ -117,6 +126,52 @@ func TestApi(t *testing.T) {
117126 runTestCases (t , tests )
118127}
119128
129+ func TestValidateEndpoint (t * testing.T ) {
130+ validYml := loadTestdata (t , "valid.publiccode.yml" )
131+ invalidYml := loadTestdata (t , "invalid.publiccode.yml" )
132+
133+ tests := []TestCase {
134+ {
135+ description : "validate: empty body" ,
136+ query : "QUERY /v1/validate" ,
137+ body : "" ,
138+ expectedCode : 400 ,
139+ expectedBody : `{"title":"empty body","detail":"need a body to validate","status":400}` ,
140+ expectedContentType : "application/problem+json" ,
141+ },
142+ {
143+ description : "validate: valid file" ,
144+ query : "QUERY /v1/validate" ,
145+ body : validYml ,
146+ expectedCode : 200 ,
147+ expectedContentType : "application/json; charset=utf-8" ,
148+ validateFunc : func (t * testing.T , response map [string ]any ) {
149+ assert .Equal (t , true , response ["valid" ])
150+ results , ok := response ["results" ].([]any )
151+ assert .True (t , ok )
152+ assert .Len (t , results , 0 )
153+ assert .NotNil (t , response ["normalized" ])
154+ },
155+ },
156+ {
157+ description : "validate: invalid file" ,
158+ query : "QUERY /v1/validate" ,
159+ body : invalidYml ,
160+ expectedCode : 200 ,
161+ expectedContentType : "application/json; charset=utf-8" ,
162+ validateFunc : func (t * testing.T , response map [string ]any ) {
163+ assert .Equal (t , false , response ["valid" ])
164+ results , ok := response ["results" ].([]any )
165+ assert .True (t , ok )
166+ assert .NotEmpty (t , results )
167+ assert .Nil (t , response ["normalized" ])
168+ },
169+ },
170+ }
171+
172+ runTestCases (t , tests )
173+ }
174+
120175func TestStatusEndpoints (t * testing.T ) {
121176 tests := []TestCase {
122177 {
0 commit comments