github.com/go-swagger/go-swagger@v0.31.0/codescan/meta_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 codescan 16 17 import ( 18 goparser "go/parser" 19 "go/token" 20 "testing" 21 22 "github.com/go-openapi/spec" 23 "github.com/stretchr/testify/assert" 24 "github.com/stretchr/testify/require" 25 ) 26 27 func TestSetInfoVersion(t *testing.T) { 28 info := new(spec.Swagger) 29 err := setInfoVersion(info, []string{"0.0.1"}) 30 require.NoError(t, err) 31 assert.Equal(t, "0.0.1", info.Info.Version) 32 } 33 34 func TestSetInfoLicense(t *testing.T) { 35 info := new(spec.Swagger) 36 err := setInfoLicense(info, []string{"MIT http://license.org/MIT"}) 37 require.NoError(t, err) 38 assert.Equal(t, "MIT", info.Info.License.Name) 39 assert.Equal(t, "http://license.org/MIT", info.Info.License.URL) 40 } 41 42 func TestSetInfoContact(t *testing.T) { 43 info := new(spec.Swagger) 44 err := setInfoContact(info, []string{"Homer J. Simpson <homer@simpsons.com> http://simpsons.com"}) 45 require.NoError(t, err) 46 assert.Equal(t, "Homer J. Simpson", info.Info.Contact.Name) 47 assert.Equal(t, "homer@simpsons.com", info.Info.Contact.Email) 48 assert.Equal(t, "http://simpsons.com", info.Info.Contact.URL) 49 } 50 51 func TestParseInfo(t *testing.T) { 52 swspec := new(spec.Swagger) 53 parser := newMetaParser(swspec) 54 docFile := "../fixtures/goparsing/classification/doc.go" 55 fileSet := token.NewFileSet() 56 fileTree, err := goparser.ParseFile(fileSet, docFile, nil, goparser.ParseComments) 57 if err != nil { 58 t.FailNow() 59 } 60 61 err = parser.Parse(fileTree.Doc) 62 63 require.NoError(t, err) 64 verifyInfo(t, swspec.Info) 65 } 66 67 func TestParseSwagger(t *testing.T) { 68 swspec := new(spec.Swagger) 69 parser := newMetaParser(swspec) 70 docFile := "../fixtures/goparsing/classification/doc.go" 71 fileSet := token.NewFileSet() 72 fileTree, err := goparser.ParseFile(fileSet, docFile, nil, goparser.ParseComments) 73 if err != nil { 74 t.FailNow() 75 } 76 77 err = parser.Parse(fileTree.Doc) 78 verifyMeta(t, swspec) 79 80 require.NoError(t, err) 81 } 82 83 func verifyMeta(t testing.TB, doc *spec.Swagger) { 84 assert.NotNil(t, doc) 85 verifyInfo(t, doc.Info) 86 assert.EqualValues(t, []string{"application/json", "application/xml"}, doc.Consumes) 87 assert.EqualValues(t, []string{"application/json", "application/xml"}, doc.Produces) 88 assert.EqualValues(t, []string{"http", "https"}, doc.Schemes) 89 assert.EqualValues(t, []map[string][]string{{"api_key": {}}}, doc.Security) 90 expectedSecuritySchemaKey := spec.SecurityScheme{ 91 SecuritySchemeProps: spec.SecuritySchemeProps{ 92 Type: "apiKey", 93 In: "header", 94 Name: "KEY", 95 }, 96 } 97 expectedSecuritySchemaOAuth := spec.SecurityScheme{ 98 SecuritySchemeProps: spec.SecuritySchemeProps{ 99 Type: "oauth2", 100 In: "header", 101 AuthorizationURL: "/oauth2/auth", 102 TokenURL: "/oauth2/token", 103 Flow: "accessCode", 104 Scopes: map[string]string{ 105 "bla1": "foo1", 106 "bla2": "foo2", 107 }, 108 }, 109 } 110 expectedExtensions := spec.Extensions{ 111 "x-meta-array": []interface{}{ 112 "value1", 113 "value2", 114 }, 115 "x-meta-array-obj": []interface{}{ 116 map[string]interface{}{ 117 "name": "obj", 118 "value": "field", 119 }, 120 }, 121 "x-meta-value": "value", 122 } 123 expectedInfoExtensions := spec.Extensions{ 124 "x-info-array": []interface{}{ 125 "value1", 126 "value2", 127 }, 128 "x-info-array-obj": []interface{}{ 129 map[string]interface{}{ 130 "name": "obj", 131 "value": "field", 132 }, 133 }, 134 "x-info-value": "value", 135 } 136 assert.NotNil(t, doc.SecurityDefinitions["api_key"]) 137 assert.NotNil(t, doc.SecurityDefinitions["oauth2"]) 138 assert.EqualValues(t, spec.SecurityDefinitions{"api_key": &expectedSecuritySchemaKey, "oauth2": &expectedSecuritySchemaOAuth}, doc.SecurityDefinitions) 139 assert.EqualValues(t, expectedExtensions, doc.Extensions) 140 assert.EqualValues(t, expectedInfoExtensions, doc.Info.Extensions) 141 assert.Equal(t, "localhost", doc.Host) 142 assert.Equal(t, "/v2", doc.BasePath) 143 } 144 145 func verifyInfo(t testing.TB, info *spec.Info) { 146 assert.NotNil(t, info) 147 assert.Equal(t, "0.0.1", info.Version) 148 assert.Equal(t, "there are no TOS at this moment, use at your own risk we take no responsibility", info.TermsOfService) 149 assert.Equal(t, "Petstore API.", info.Title) 150 descr := `the purpose of this application is to provide an application 151 that is using plain go code to define an API 152 153 This should demonstrate all the possible comment annotations 154 that are available to turn go code into a fully compliant swagger 2.0 spec` 155 assert.Equal(t, descr, info.Description) 156 157 require.NotNil(t, info.License) 158 assert.Equal(t, "MIT", info.License.Name) 159 assert.Equal(t, "http://opensource.org/licenses/MIT", info.License.URL) 160 161 require.NotNil(t, info.Contact) 162 assert.Equal(t, "John Doe", info.Contact.Name) 163 assert.Equal(t, "john.doe@example.com", info.Contact.Email) 164 assert.Equal(t, "http://john.doe.com", info.Contact.URL) 165 } 166 167 func TestMoreParseMeta(t *testing.T) { 168 for _, docFile := range []string{ 169 "../fixtures/goparsing/meta/v1/doc.go", 170 "../fixtures/goparsing/meta/v2/doc.go", 171 "../fixtures/goparsing/meta/v3/doc.go", 172 "../fixtures/goparsing/meta/v4/doc.go", 173 } { 174 175 swspec := new(spec.Swagger) 176 parser := newMetaParser(swspec) 177 fileSet := token.NewFileSet() 178 fileTree, err := goparser.ParseFile(fileSet, docFile, nil, goparser.ParseComments) 179 if err != nil { 180 t.FailNow() 181 } 182 183 err = parser.Parse(fileTree.Doc) 184 require.NoError(t, err) 185 assert.Equal(t, "there are no TOS at this moment, use at your own risk we take no responsibility", swspec.Info.TermsOfService) 186 /* 187 jazon, err := json.MarshalIndent(swspec.Info, "", " ") 188 require.NoError(t, err) 189 t.Logf("%v", string(jazon)) 190 */ 191 } 192 }