github.com/djarvur/go-swagger@v0.18.0/scan/responses_test.go (about) 1 // Copyright 2015 go-swagger maintainers 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package scan 16 17 import ( 18 goparser "go/parser" 19 "log" 20 "testing" 21 22 "github.com/go-openapi/spec" 23 "github.com/stretchr/testify/assert" 24 ) 25 26 func TestParseResponses(t *testing.T) { 27 docFile := "../fixtures/goparsing/classification/operations/responses.go" 28 fileTree, err := goparser.ParseFile(classificationProg.Fset, docFile, nil, goparser.ParseComments) 29 if err != nil { 30 log.Fatal(err) 31 } 32 33 rp := newResponseParser(classificationProg) 34 responses := make(map[string]spec.Response) 35 err = rp.Parse(fileTree, responses) 36 if err != nil { 37 t.Fatal(err) 38 } 39 assert.Len(t, responses, 8) 40 cr, ok := responses["complexerOne"] 41 assert.True(t, ok) 42 assert.Len(t, cr.Headers, 7) 43 for k, header := range cr.Headers { 44 switch k { 45 case "id": 46 assert.Equal(t, "integer", header.Type) 47 assert.Equal(t, "int64", header.Format) 48 case "name": 49 assert.Equal(t, "string", header.Type) 50 assert.Equal(t, "", header.Format) 51 case "age": 52 assert.Equal(t, "integer", header.Type) 53 assert.Equal(t, "int32", header.Format) 54 case "notes": 55 assert.Equal(t, "string", header.Type) 56 assert.Equal(t, "", header.Format) 57 case "extra": 58 assert.Equal(t, "string", header.Type) 59 assert.Equal(t, "", header.Format) 60 case "createdAt": 61 assert.Equal(t, "string", header.Type) 62 assert.Equal(t, "date-time", header.Format) 63 case "NoTagName": 64 assert.Equal(t, "string", header.Type) 65 assert.Equal(t, "", header.Format) 66 default: 67 assert.Fail(t, "unknown header: "+k) 68 } 69 } 70 71 cpr, ok := responses["complexerPointerOne"] 72 assert.True(t, ok) 73 assert.Len(t, cpr.Headers, 4) 74 for k, header := range cpr.Headers { 75 switch k { 76 case "id": 77 assert.Equal(t, "integer", header.Type) 78 assert.Equal(t, "int64", header.Format) 79 case "name": 80 assert.Equal(t, "string", header.Type) 81 assert.Equal(t, "", header.Format) 82 case "age": 83 assert.Equal(t, "integer", header.Type) 84 assert.Equal(t, "int32", header.Format) 85 case "extra": 86 assert.Equal(t, "integer", header.Type) 87 assert.Equal(t, "int64", header.Format) 88 default: 89 assert.Fail(t, "unknown header: "+k) 90 } 91 } 92 93 sos, ok := responses["simpleOnes"] 94 assert.True(t, ok) 95 assert.Len(t, sos.Headers, 1) 96 97 sosf, ok := responses["simpleOnesFunc"] 98 assert.True(t, ok) 99 assert.Len(t, sosf.Headers, 1) 100 101 res, ok := responses["someResponse"] 102 assert.True(t, ok) 103 assert.Len(t, res.Headers, 7) 104 105 for k, header := range res.Headers { 106 switch k { 107 case "id": 108 assert.Equal(t, "ID of this some response instance.\nids in this application start at 11 and are smaller than 1000", header.Description) 109 assert.Equal(t, "integer", header.Type) 110 assert.Equal(t, "int64", header.Format) 111 //assert.Equal(t, "ID", header.Extensions["x-go-name"]) 112 assert.EqualValues(t, 1000, *header.Maximum) 113 assert.True(t, header.ExclusiveMaximum) 114 assert.EqualValues(t, 10, *header.Minimum) 115 assert.True(t, header.ExclusiveMinimum) 116 assert.Equal(t, 11, header.Default, "ID default value is incorrect") 117 118 case "score": 119 assert.Equal(t, "The Score of this model", header.Description) 120 assert.Equal(t, "integer", header.Type) 121 assert.Equal(t, "int32", header.Format) 122 //assert.Equal(t, "Score", header.Extensions["x-go-name"]) 123 assert.EqualValues(t, 45, *header.Maximum) 124 assert.False(t, header.ExclusiveMaximum) 125 assert.EqualValues(t, 3, *header.Minimum) 126 assert.False(t, header.ExclusiveMinimum) 127 assert.Equal(t, 27, header.Example) 128 129 case "x-hdr-name": 130 assert.Equal(t, "Name of this some response instance", header.Description) 131 assert.Equal(t, "string", header.Type) 132 //assert.Equal(t, "Name", header.Extensions["x-go-name"]) 133 assert.EqualValues(t, 4, *header.MinLength) 134 assert.EqualValues(t, 50, *header.MaxLength) 135 assert.Equal(t, "[A-Za-z0-9-.]*", header.Pattern) 136 137 case "active": 138 assert.Equal(t, "Active state of the record", header.Description) 139 assert.Equal(t, "boolean", header.Type) 140 assert.Equal(t, true, header.Default) 141 142 case "created": 143 assert.Equal(t, "Created holds the time when this entry was created", header.Description) 144 assert.Equal(t, "string", header.Type) 145 assert.Equal(t, "date-time", header.Format) 146 //assert.Equal(t, "Created", header.Extensions["x-go-name"]) 147 148 case "foo_slice": 149 assert.Equal(t, "a FooSlice has foos which are strings", header.Description) 150 //assert.Equal(t, "FooSlice", header.Extensions["x-go-name"]) 151 assert.Equal(t, "array", header.Type) 152 assert.True(t, header.UniqueItems) 153 assert.Equal(t, "pipe", header.CollectionFormat) 154 assert.NotNil(t, header.Items, "foo_slice should have had an items property") 155 assert.EqualValues(t, 3, *header.MinItems, "'foo_slice' should have had 3 min items") 156 assert.EqualValues(t, 10, *header.MaxItems, "'foo_slice' should have had 10 max items") 157 itprop := header.Items 158 assert.EqualValues(t, 3, *itprop.MinLength, "'foo_slice.items.minLength' should have been 3") 159 assert.EqualValues(t, 10, *itprop.MaxLength, "'foo_slice.items.maxLength' should have been 10") 160 assert.EqualValues(t, "\\w+", itprop.Pattern, "'foo_slice.items.pattern' should have \\w+") 161 assert.Equal(t, "foo", itprop.Example) 162 163 case "bar_slice": 164 assert.Equal(t, "a BarSlice has bars which are strings", header.Description) 165 assert.Equal(t, "array", header.Type) 166 assert.True(t, header.UniqueItems) 167 assert.Equal(t, "pipe", header.CollectionFormat) 168 assert.NotNil(t, header.Items, "bar_slice should have had an items property") 169 assert.EqualValues(t, 3, *header.MinItems, "'bar_slice' should have had 3 min items") 170 assert.EqualValues(t, 10, *header.MaxItems, "'bar_slice' should have had 10 max items") 171 itprop := header.Items 172 if assert.NotNil(t, itprop) { 173 assert.EqualValues(t, 4, *itprop.MinItems, "'bar_slice.items.minItems' should have been 4") 174 assert.EqualValues(t, 9, *itprop.MaxItems, "'bar_slice.items.maxItems' should have been 9") 175 itprop2 := itprop.Items 176 if assert.NotNil(t, itprop2) { 177 assert.EqualValues(t, 5, *itprop2.MinItems, "'bar_slice.items.items.minItems' should have been 5") 178 assert.EqualValues(t, 8, *itprop2.MaxItems, "'bar_slice.items.items.maxItems' should have been 8") 179 itprop3 := itprop2.Items 180 if assert.NotNil(t, itprop3) { 181 assert.EqualValues(t, 3, *itprop3.MinLength, "'bar_slice.items.items.items.minLength' should have been 3") 182 assert.EqualValues(t, 10, *itprop3.MaxLength, "'bar_slice.items.items.items.maxLength' should have been 10") 183 assert.EqualValues(t, "\\w+", itprop3.Pattern, "'bar_slice.items.items.items.pattern' should have \\w+") 184 } 185 } 186 } 187 188 default: 189 assert.Fail(t, "unknown property: "+k) 190 } 191 } 192 193 assert.NotNil(t, res.Schema) 194 aprop := res.Schema 195 assert.Equal(t, "array", aprop.Type[0]) 196 assert.NotNil(t, aprop.Items) 197 assert.NotNil(t, aprop.Items.Schema) 198 itprop := aprop.Items.Schema 199 assert.Len(t, itprop.Properties, 4) 200 assert.Len(t, itprop.Required, 3) 201 assertProperty(t, itprop, "integer", "id", "int32", "ID") 202 iprop, ok := itprop.Properties["id"] 203 assert.True(t, ok) 204 assert.Equal(t, "ID of this some response instance.\nids in this application start at 11 and are smaller than 1000", iprop.Description) 205 assert.EqualValues(t, 1000, *iprop.Maximum) 206 assert.True(t, iprop.ExclusiveMaximum, "'id' should have had an exclusive maximum") 207 assert.NotNil(t, iprop.Minimum) 208 assert.EqualValues(t, 10, *iprop.Minimum) 209 assert.True(t, iprop.ExclusiveMinimum, "'id' should have had an exclusive minimum") 210 211 assertRef(t, itprop, "pet", "Pet", "#/definitions/pet") 212 iprop, ok = itprop.Properties["pet"] 213 assert.True(t, ok) 214 // if itprop.Ref.String() == "" { 215 // assert.Equal(t, "The Pet to add to this NoModel items bucket.\nPets can appear more than once in the bucket", iprop.Description) 216 // } 217 218 assertProperty(t, itprop, "integer", "quantity", "int16", "Quantity") 219 iprop, ok = itprop.Properties["quantity"] 220 assert.True(t, ok) 221 assert.Equal(t, "The amount of pets to add to this bucket.", iprop.Description) 222 assert.EqualValues(t, 1, *iprop.Minimum) 223 assert.EqualValues(t, 10, *iprop.Maximum) 224 225 assertProperty(t, itprop, "string", "notes", "", "Notes") 226 iprop, ok = itprop.Properties["notes"] 227 assert.True(t, ok) 228 assert.Equal(t, "Notes to add to this item.\nThis can be used to add special instructions.", iprop.Description) 229 230 res, ok = responses["resp"] 231 assert.True(t, ok) 232 assert.NotNil(t, res.Schema) 233 assert.Equal(t, "#/definitions/user", res.Schema.Ref.String()) 234 }