...

Source file src/github.com/go-openapi/analysis/internal/flatten/normalize/normalize_test.go

Documentation: github.com/go-openapi/analysis/internal/flatten/normalize

     1  package normalize
     2  
     3  import (
     4  	"net/url"
     5  	"path/filepath"
     6  	"runtime"
     7  	"testing"
     8  
     9  	_ "github.com/go-openapi/analysis/internal/antest"
    10  	"github.com/go-openapi/spec"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  const (
    15  	definitionA    = "#/definitions/A"
    16  	definitionABC  = "#/definitions/abc"
    17  	definitionBase = "#/definitions/base"
    18  	exampleBase    = "https://example.com/base"
    19  )
    20  
    21  func TestNormalize_Path(t *testing.T) {
    22  	t.Parallel()
    23  
    24  	values := []struct{ Source, Expected string }{
    25  		{definitionA, definitionA},
    26  		{"http://somewhere.com/definitions/A", "http://somewhere.com/definitions/A"},
    27  		{wrapWindowsPath("/definitions/A"), wrapWindowsPath("/definitions/A")}, // considered absolute on unix but not on windows
    28  		{wrapWindowsPath("/definitions/errorModel.json") + definitionA, wrapWindowsPath("/definitions/errorModel.json") + definitionA},
    29  		{"http://somewhere.com", "http://somewhere.com"},
    30  		{wrapWindowsPath("./definitions/definitions.yaml") + definitionA, wrapWindowsPath("/abs/to/spec/definitions/definitions.yaml") + definitionA},
    31  		{"#", wrapWindowsPath("/abs/to/spec")},
    32  	}
    33  
    34  	for _, v := range values {
    35  		assert.Equal(t, v.Expected, Path(spec.MustCreateRef(v.Source), wrapWindowsPath("/abs/to/spec/spec.json")))
    36  	}
    37  }
    38  
    39  func TestNormalize_RebaseRef(t *testing.T) {
    40  	t.Parallel()
    41  
    42  	assert.Equal(t, definitionABC, RebaseRef(definitionBase, definitionABC))
    43  	assert.Equal(t, definitionABC, RebaseRef("", definitionABC))
    44  	assert.Equal(t, definitionABC, RebaseRef(".", definitionABC))
    45  	assert.Equal(t, "otherfile"+definitionABC, RebaseRef("file"+definitionBase, "otherfile"+definitionABC))
    46  	assert.Equal(t, wrapWindowsPath("../otherfile")+definitionABC, RebaseRef(wrapWindowsPath("../file")+definitionBase, wrapWindowsPath("./otherfile")+definitionABC))
    47  	assert.Equal(t, wrapWindowsPath("../otherfile")+definitionABC, RebaseRef(wrapWindowsPath("../file")+definitionBase, wrapWindowsPath("otherfile")+definitionABC))
    48  	assert.Equal(t, wrapWindowsPath("local/remote/otherfile")+definitionABC, RebaseRef(wrapWindowsPath("local/file")+definitionBase, wrapWindowsPath("remote/otherfile")+definitionABC))
    49  	assert.Equal(t, wrapWindowsPath("local/remote/otherfile.yaml"), RebaseRef(wrapWindowsPath("local/file.yaml"), wrapWindowsPath("remote/otherfile.yaml")))
    50  
    51  	assert.Equal(t, "file#/definitions/abc", RebaseRef("file#/definitions/base", definitionABC))
    52  
    53  	// with remote
    54  	assert.Equal(t, exampleBase+definitionABC, RebaseRef(exampleBase, exampleBase+definitionABC))
    55  	assert.Equal(t, exampleBase+definitionABC, RebaseRef(exampleBase, definitionABC))
    56  	assert.Equal(t, exampleBase+"#/dir/definitions/abc", RebaseRef(exampleBase, "#/dir/definitions/abc"))
    57  	assert.Equal(t, exampleBase+"/dir/definitions/abc", RebaseRef(exampleBase+"/spec.yaml", "dir/definitions/abc"))
    58  	assert.Equal(t, exampleBase+"/dir/definitions/abc", RebaseRef(exampleBase+"/", "dir/definitions/abc"))
    59  	assert.Equal(t, "https://example.com/dir/definitions/abc", RebaseRef(exampleBase, "dir/definitions/abc"))
    60  }
    61  
    62  // wrapWindowsPath adapts path expectations for tests running on windows
    63  func wrapWindowsPath(p string) string {
    64  	if runtime.GOOS != "windows" {
    65  		return p
    66  	}
    67  
    68  	pp := filepath.FromSlash(p)
    69  	if !filepath.IsAbs(p) && []rune(pp)[0] == '\\' {
    70  		pp, _ = filepath.Abs(p)
    71  		u, _ := url.Parse(pp)
    72  
    73  		return u.String()
    74  	}
    75  
    76  	return pp
    77  }
    78  

View as plain text