...

Source file src/github.com/go-openapi/validate/example_validator_test.go

Documentation: github.com/go-openapi/validate

     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 validate
    16  
    17  import (
    18  	"path/filepath"
    19  	"testing"
    20  
    21  	"github.com/go-openapi/analysis"
    22  	"github.com/go-openapi/loads"
    23  	"github.com/go-openapi/spec"
    24  	"github.com/go-openapi/strfmt"
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func TestExample_ValidateExamples(t *testing.T) {
    29  	doc, _ := loads.Analyzed(PetStoreJSONMessage, "")
    30  	validator := NewSpecValidator(spec.MustLoadSwagger20Schema(), strfmt.Default)
    31  	validator.spec = doc
    32  	validator.analyzer = analysis.New(doc.Spec())
    33  	myDefaultValidator := &defaultValidator{SpecValidator: validator}
    34  	res := myDefaultValidator.Validate()
    35  	assert.Empty(t, res.Errors)
    36  
    37  	tests := []string{
    38  		// - $ref in response
    39  		"response",
    40  		"response-ref",
    41  		"parameter",
    42  		"parameter-required",
    43  		"parameter-ref",
    44  		"parameter-items",
    45  		"header",
    46  		"header-items",
    47  		"schema",
    48  		"schema-ref",
    49  		"schema-additionalProperties",
    50  		"schema-patternProperties",
    51  		"schema-items",
    52  		"schema-allOf",
    53  		"parameter-schema",
    54  		"default-response",
    55  		"header-response",
    56  		"header-items-default-response",
    57  		"header-items-response",
    58  		"header-pattern",
    59  		"header-badpattern",
    60  		"schema-items-allOf",
    61  	}
    62  
    63  	for _, tt := range tests {
    64  		path := filepath.Join("fixtures", "validation", "example", "valid-example-"+tt+".json")
    65  		if DebugTest {
    66  			t.Logf("Testing valid example values for: %s", path)
    67  		}
    68  
    69  		validator := makeSpecValidator(t, path)
    70  		myExampleValidator := &exampleValidator{SpecValidator: validator}
    71  		res := myExampleValidator.Validate()
    72  		assert.Empty(t, res.Errors, tt+noErrorMsg)
    73  		/*
    74  			// Special case: warning only
    75  			if tt == "parameter-required" {
    76  				assert.Contains(t, verifiedTestWarnings(res), "limit in query has an example value and is required as parameter")
    77  			}
    78  		*/
    79  		debugTest(t, path, res)
    80  
    81  		path = filepath.Join("fixtures", "validation", "example", "invalid-example-"+tt+".json")
    82  
    83  		if DebugTest {
    84  			t.Logf("Testing invalid example values for: %s", path)
    85  		}
    86  
    87  		validator = makeSpecValidator(t, path)
    88  		myExampleValidator = &exampleValidator{SpecValidator: validator}
    89  		res = myExampleValidator.Validate()
    90  		switch tt {
    91  		case "header-badpattern":
    92  			// This fixture exhibits real errors besides example values
    93  			assert.NotEmpty(t, res.Errors, tt+hasErrorMsg)
    94  			assert.NotEmpty(t, res.Warnings, tt+hasWarningMsg)
    95  		default:
    96  			assert.Empty(t, res.Errors, tt+noErrorMsg)
    97  			assert.NotEmpty(t, res.Warnings, tt+hasWarningMsg)
    98  		}
    99  		// Update: now we have an additional message to explain it's all about a default value
   100  		// Example:
   101  		// - default value for limit in query does not validate its Schema
   102  		// - limit in query must be of type integer: "string"]
   103  		assert.NotEmptyf(t, res.Warnings, tt+" should have at least 1 warning")
   104  
   105  		debugTest(t, path, res)
   106  		if DebugTest && t.Failed() {
   107  			t.FailNow()
   108  		}
   109  	}
   110  }
   111  
   112  func TestExample_EdgeCase(t *testing.T) {
   113  	// Testing guards
   114  	var myExampleValidator *exampleValidator
   115  	res := myExampleValidator.Validate()
   116  	assert.True(t, res.IsValid())
   117  
   118  	myExampleValidator = &exampleValidator{}
   119  	res = myExampleValidator.Validate()
   120  	assert.True(t, res.IsValid())
   121  }
   122  

View as plain text