github.com/djarvur/go-swagger@v0.18.0/scan/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 scan 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 ) 25 26 func TestSetInfoVersion(t *testing.T) { 27 info := new(spec.Swagger) 28 err := setInfoVersion(info, []string{"0.0.1"}) 29 assert.NoError(t, err) 30 assert.Equal(t, "0.0.1", info.Info.Version) 31 } 32 33 func TestSetInfoLicense(t *testing.T) { 34 info := new(spec.Swagger) 35 err := setInfoLicense(info, []string{"MIT http://license.org/MIT"}) 36 assert.NoError(t, err) 37 assert.Equal(t, "MIT", info.Info.License.Name) 38 assert.Equal(t, "http://license.org/MIT", info.Info.License.URL) 39 } 40 41 func TestSetInfoContact(t *testing.T) { 42 info := new(spec.Swagger) 43 err := setInfoContact(info, []string{"Homer J. Simpson <homer@simpsons.com> http://simpsons.com"}) 44 assert.NoError(t, err) 45 assert.Equal(t, "Homer J. Simpson", info.Info.Contact.Name) 46 assert.Equal(t, "homer@simpsons.com", info.Info.Contact.Email) 47 assert.Equal(t, "http://simpsons.com", info.Info.Contact.URL) 48 } 49 50 func TestParseInfo(t *testing.T) { 51 swspec := new(spec.Swagger) 52 parser := newMetaParser(swspec) 53 docFile := "../fixtures/goparsing/classification/doc.go" 54 fileSet := token.NewFileSet() 55 fileTree, err := goparser.ParseFile(fileSet, docFile, nil, goparser.ParseComments) 56 if err != nil { 57 t.FailNow() 58 } 59 60 err = parser.Parse(fileTree.Doc) 61 62 assert.NoError(t, err) 63 verifyInfo(t, swspec.Info) 64 } 65 66 func TestParseSwagger(t *testing.T) { 67 swspec := new(spec.Swagger) 68 parser := newMetaParser(swspec) 69 docFile := "../fixtures/goparsing/classification/doc.go" 70 fileSet := token.NewFileSet() 71 fileTree, err := goparser.ParseFile(fileSet, docFile, nil, goparser.ParseComments) 72 if err != nil { 73 t.FailNow() 74 } 75 76 err = parser.Parse(fileTree.Doc) 77 verifyMeta(t, swspec) 78 79 assert.NoError(t, err) 80 } 81 82 func verifyMeta(t testing.TB, doc *spec.Swagger) { 83 assert.NotNil(t, doc) 84 verifyInfo(t, doc.Info) 85 assert.EqualValues(t, []string{"application/json", "application/xml"}, doc.Consumes) 86 assert.EqualValues(t, []string{"application/json", "application/xml"}, doc.Produces) 87 assert.EqualValues(t, []string{"http", "https"}, doc.Schemes) 88 assert.EqualValues(t, []map[string][]string{{"api_key": {}}}, doc.Security) 89 expectedSecuritySchemaKey := spec.SecurityScheme{ 90 SecuritySchemeProps: spec.SecuritySchemeProps{ 91 Type: "apiKey", 92 In: "header", 93 Name: "KEY", 94 }, 95 } 96 expectedSecuritySchemaOAuth := spec.SecurityScheme{ 97 SecuritySchemeProps: spec.SecuritySchemeProps{ 98 Type: "oauth2", 99 In: "header", 100 AuthorizationURL: "/oauth2/auth", 101 TokenURL: "/oauth2/token", 102 Flow: "accessCode", 103 Scopes: map[string]string{ 104 "bla1": "foo1", 105 "bla2": "foo2", 106 }, 107 }, 108 } 109 expectedExtensions := spec.Extensions{ 110 "x-meta-array": []interface{}{ 111 "value1", 112 "value2", 113 }, 114 "x-meta-array-obj": []interface{}{ 115 map[string]interface{}{ 116 "name": "obj", 117 "value": "field", 118 }, 119 }, 120 "x-meta-value": "value", 121 } 122 expectedInfoExtensions := spec.Extensions{ 123 "x-info-array": []interface{}{ 124 "value1", 125 "value2", 126 }, 127 "x-info-array-obj": []interface{}{ 128 map[string]interface{}{ 129 "name": "obj", 130 "value": "field", 131 }, 132 }, 133 "x-info-value": "value", 134 } 135 assert.NotNil(t, doc.SecurityDefinitions["api_key"]) 136 assert.NotNil(t, doc.SecurityDefinitions["oauth2"]) 137 assert.EqualValues(t, spec.SecurityDefinitions{"api_key": &expectedSecuritySchemaKey, "oauth2": &expectedSecuritySchemaOAuth}, doc.SecurityDefinitions) 138 assert.EqualValues(t, expectedExtensions, doc.Extensions) 139 assert.EqualValues(t, expectedInfoExtensions, doc.Info.Extensions) 140 assert.Equal(t, "localhost", doc.Host) 141 assert.Equal(t, "/v2", doc.BasePath) 142 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 assert.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 assert.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 }