github.com/emreu/go-swagger@v0.22.1/scan/meta_test.go (about)

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