...

Source file src/github.com/go-openapi/analysis/fixer_test.go

Documentation: github.com/go-openapi/analysis

     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 analysis
    16  
    17  import (
    18  	"path/filepath"
    19  	"strconv"
    20  	"testing"
    21  
    22  	"github.com/go-openapi/analysis/internal/antest"
    23  	"github.com/go-openapi/spec"
    24  	"github.com/stretchr/testify/assert"
    25  	"github.com/stretchr/testify/require"
    26  )
    27  
    28  func TestFixer_EmptyResponseDescriptions(t *testing.T) {
    29  	t.Parallel()
    30  
    31  	bp := filepath.Join("fixtures", "fixer", "fixer.yaml")
    32  	sp := antest.LoadOrFail(t, bp)
    33  
    34  	FixEmptyResponseDescriptions(sp)
    35  
    36  	for path, pathItem := range sp.Paths.Paths {
    37  		require.NotNil(t, pathItem, "expected a fixture with all path items provided in: %s", path)
    38  
    39  		if path == "/noDesc" {
    40  			// scope for fixed descriptions
    41  			assertAllVerbs(t, pathItem, true)
    42  		} else {
    43  			// scope for unchanged descriptions
    44  			assertAllVerbs(t, pathItem, false)
    45  		}
    46  	}
    47  
    48  	for r, toPin := range sp.Responses {
    49  		resp := toPin
    50  		assert.Truef(t, assertResponse(t, "/responses/"+r, &resp, true),
    51  			"expected a fixed empty description in response %s", r)
    52  	}
    53  }
    54  
    55  func assertAllVerbs(t testing.TB, pathItem spec.PathItem, isEmpty bool) {
    56  	msg := "expected %s description for %s"
    57  	var mode string
    58  	if isEmpty {
    59  		mode = "a fixed empty"
    60  	} else {
    61  		mode = "an unmodified"
    62  	}
    63  
    64  	assert.Truef(t, assertResponseInOperation(t, pathItem.Get, isEmpty), msg, mode, "GET")
    65  	assert.Truef(t, assertResponseInOperation(t, pathItem.Put, isEmpty), msg, mode, "PUT")
    66  	assert.Truef(t, assertResponseInOperation(t, pathItem.Post, isEmpty), msg, mode, "POST")
    67  	assert.Truef(t, assertResponseInOperation(t, pathItem.Delete, isEmpty), msg, mode, "DELETE")
    68  	assert.Truef(t, assertResponseInOperation(t, pathItem.Options, isEmpty), msg, mode, "OPTIONS")
    69  	assert.Truef(t, assertResponseInOperation(t, pathItem.Patch, isEmpty), msg, mode, "PATCH")
    70  	assert.Truef(t, assertResponseInOperation(t, pathItem.Head, isEmpty), msg, mode, "HEAD")
    71  }
    72  
    73  func assertResponseInOperation(t testing.TB, op *spec.Operation, isEmpty bool) bool {
    74  	require.NotNilf(t, op, "expected a fixture with all REST verbs set")
    75  
    76  	if op.Responses == nil {
    77  		return true
    78  	}
    79  
    80  	if op.Responses.Default != nil {
    81  		return assert.Truef(t, assertResponse(t, "default", op.Responses.Default, isEmpty),
    82  			"unexpected description in response %s for operation", "default")
    83  	}
    84  
    85  	for code, resp := range op.Responses.StatusCodeResponses {
    86  		pin := resp
    87  
    88  		return assert.Truef(t, assertResponse(t, strconv.Itoa(code), &pin, isEmpty),
    89  			"unexpected description in response %d for operation", code)
    90  	}
    91  
    92  	return true
    93  }
    94  
    95  func assertResponse(t testing.TB, path string, resp *spec.Response, isEmpty bool) bool {
    96  	var expected string
    97  
    98  	if isEmpty {
    99  		expected = "(empty)"
   100  	} else {
   101  		expected = "my description"
   102  	}
   103  
   104  	if resp.Ref.String() != "" {
   105  		expected = ""
   106  	}
   107  
   108  	if !assert.Equalf(t, expected, resp.Description, "unexpected description for resp. %s", path) {
   109  		return false
   110  	}
   111  
   112  	return true
   113  }
   114  

View as plain text