github.com/jamescostian/go-swagger@v0.30.4-0.20221130163922-68364d6b567b/scan/meta_test.go (about) 1 //go:build !go1.11 2 // +build !go1.11 3 4 // Copyright 2015 go-swagger maintainers 5 // 6 // Licensed under the Apache License, Version 2.0 (the "License"); 7 // you may not use this file except in compliance with the License. 8 // You may obtain a copy of the License at 9 // 10 // http://www.apache.org/licenses/LICENSE-2.0 11 // 12 // Unless required by applicable law or agreed to in writing, software 13 // distributed under the License is distributed on an "AS IS" BASIS, 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 // See the License for the specific language governing permissions and 16 // limitations under the License. 17 18 package scan 19 20 import ( 21 goparser "go/parser" 22 "go/token" 23 "testing" 24 25 "github.com/go-openapi/spec" 26 "github.com/stretchr/testify/assert" 27 ) 28 29 func TestSetInfoVersion(t *testing.T) { 30 info := new(spec.Swagger) 31 err := setInfoVersion(info, []string{"0.0.1"}) 32 assert.NoError(t, err) 33 assert.Equal(t, "0.0.1", info.Info.Version) 34 } 35 36 func TestSetInfoLicense(t *testing.T) { 37 info := new(spec.Swagger) 38 err := setInfoLicense(info, []string{"MIT http://license.org/MIT"}) 39 assert.NoError(t, err) 40 assert.Equal(t, "MIT", info.Info.License.Name) 41 assert.Equal(t, "http://license.org/MIT", info.Info.License.URL) 42 } 43 44 func TestSetInfoContact(t *testing.T) { 45 info := new(spec.Swagger) 46 err := setInfoContact(info, []string{"Homer J. Simpson <homer@simpsons.com> http://simpsons.com"}) 47 assert.NoError(t, err) 48 assert.Equal(t, "Homer J. Simpson", info.Info.Contact.Name) 49 assert.Equal(t, "homer@simpsons.com", info.Info.Contact.Email) 50 assert.Equal(t, "http://simpsons.com", info.Info.Contact.URL) 51 } 52 53 func TestParseInfo(t *testing.T) { 54 swspec := new(spec.Swagger) 55 parser := newMetaParser(swspec) 56 docFile := "../fixtures/goparsing/classification/doc.go" 57 fileSet := token.NewFileSet() 58 fileTree, err := goparser.ParseFile(fileSet, docFile, nil, goparser.ParseComments) 59 if err != nil { 60 t.FailNow() 61 } 62 63 err = parser.Parse(fileTree.Doc) 64 65 assert.NoError(t, err) 66 verifyInfo(t, swspec.Info) 67 } 68 69 func TestParseSwagger(t *testing.T) { 70 swspec := new(spec.Swagger) 71 parser := newMetaParser(swspec) 72 docFile := "../fixtures/goparsing/classification/doc.go" 73 fileSet := token.NewFileSet() 74 fileTree, err := goparser.ParseFile(fileSet, docFile, nil, goparser.ParseComments) 75 if err != nil { 76 t.FailNow() 77 } 78 79 err = parser.Parse(fileTree.Doc) 80 verifyMeta(t, swspec) 81 82 assert.NoError(t, err) 83 } 84 85 func verifyMeta(t testing.TB, doc *spec.Swagger) { 86 assert.NotNil(t, doc) 87 verifyInfo(t, doc.Info) 88 assert.EqualValues(t, []string{"application/json", "application/xml"}, doc.Consumes) 89 assert.EqualValues(t, []string{"application/json", "application/xml"}, doc.Produces) 90 assert.EqualValues(t, []string{"http", "https"}, doc.Schemes) 91 assert.EqualValues(t, []map[string][]string{{"api_key": {}}}, doc.Security) 92 expectedSecuritySchemaKey := spec.SecurityScheme{ 93 SecuritySchemeProps: spec.SecuritySchemeProps{ 94 Type: "apiKey", 95 In: "header", 96 Name: "KEY", 97 }, 98 } 99 expectedSecuritySchemaOAuth := spec.SecurityScheme{ 100 SecuritySchemeProps: spec.SecuritySchemeProps{ 101 Type: "oauth2", 102 In: "header", 103 AuthorizationURL: "/oauth2/auth", 104 TokenURL: "/oauth2/token", 105 Flow: "accessCode", 106 Scopes: map[string]string{ 107 "bla1": "foo1", 108 "bla2": "foo2", 109 }, 110 }, 111 } 112 expectedExtensions := spec.Extensions{ 113 "x-meta-array": []interface{}{ 114 "value1", 115 "value2", 116 }, 117 "x-meta-array-obj": []interface{}{ 118 map[string]interface{}{ 119 "name": "obj", 120 "value": "field", 121 }, 122 }, 123 "x-meta-value": "value", 124 } 125 expectedInfoExtensions := spec.Extensions{ 126 "x-info-array": []interface{}{ 127 "value1", 128 "value2", 129 }, 130 "x-info-array-obj": []interface{}{ 131 map[string]interface{}{ 132 "name": "obj", 133 "value": "field", 134 }, 135 }, 136 "x-info-value": "value", 137 } 138 assert.NotNil(t, doc.SecurityDefinitions["api_key"]) 139 assert.NotNil(t, doc.SecurityDefinitions["oauth2"]) 140 assert.EqualValues(t, spec.SecurityDefinitions{"api_key": &expectedSecuritySchemaKey, "oauth2": &expectedSecuritySchemaOAuth}, doc.SecurityDefinitions) 141 assert.EqualValues(t, expectedExtensions, doc.Extensions) 142 assert.EqualValues(t, expectedInfoExtensions, doc.Info.Extensions) 143 assert.Equal(t, "localhost", doc.Host) 144 assert.Equal(t, "/v2", doc.BasePath) 145 146 } 147 148 func verifyInfo(t testing.TB, info *spec.Info) { 149 assert.NotNil(t, info) 150 assert.Equal(t, "0.0.1", info.Version) 151 assert.Equal(t, "there are no TOS at this moment, use at your own risk we take no responsibility", info.TermsOfService) 152 assert.Equal(t, "Petstore API.", info.Title) 153 descr := `the purpose of this application is to provide an application 154 that is using plain go code to define an API 155 156 This should demonstrate all the possible comment annotations 157 that are available to turn go code into a fully compliant swagger 2.0 spec` 158 assert.Equal(t, descr, info.Description) 159 160 assert.NotNil(t, info.License) 161 assert.Equal(t, "MIT", info.License.Name) 162 assert.Equal(t, "http://opensource.org/licenses/MIT", info.License.URL) 163 164 assert.NotNil(t, info.Contact) 165 assert.Equal(t, "John Doe", info.Contact.Name) 166 assert.Equal(t, "john.doe@example.com", info.Contact.Email) 167 assert.Equal(t, "http://john.doe.com", info.Contact.URL) 168 }