mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-07 20:47:45 +00:00
extraction functions coverage bumped back up
Signed-off-by: quobix <dave@quobix.com>
This commit is contained in:
@@ -99,9 +99,12 @@ func LocateRefNodeWithContext(ctx context.Context, root *yaml.Node, idx *index.S
|
|||||||
|
|
||||||
if strings.HasPrefix(specPath, "http") {
|
if strings.HasPrefix(specPath, "http") {
|
||||||
u, _ := url.Parse(specPath)
|
u, _ := url.Parse(specPath)
|
||||||
p := filepath.Dir(u.Path)
|
p := ""
|
||||||
abs, _ := filepath.Abs(filepath.Join(p, explodedRefValue[0]))
|
if u.Path != "" {
|
||||||
u.Path = abs
|
p = filepath.Dir(u.Path)
|
||||||
|
}
|
||||||
|
u.Path = filepath.Join(p, explodedRefValue[0])
|
||||||
|
u.Fragment = ""
|
||||||
rv = fmt.Sprintf("%s#%s", u.String(), explodedRefValue[1])
|
rv = fmt.Sprintf("%s#%s", u.String(), explodedRefValue[1])
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
@@ -116,9 +119,11 @@ func LocateRefNodeWithContext(ctx context.Context, root *yaml.Node, idx *index.S
|
|||||||
if idx.GetConfig().BaseURL != nil {
|
if idx.GetConfig().BaseURL != nil {
|
||||||
|
|
||||||
u := *idx.GetConfig().BaseURL
|
u := *idx.GetConfig().BaseURL
|
||||||
|
p := ""
|
||||||
abs, _ := filepath.Abs(filepath.Join(u.Path, rv))
|
if u.Path != "" {
|
||||||
u.Path = abs
|
p = filepath.Dir(u.Path)
|
||||||
|
}
|
||||||
|
u.Path = filepath.Join(p, explodedRefValue[0])
|
||||||
rv = fmt.Sprintf("%s#%s", u.String(), explodedRefValue[1])
|
rv = fmt.Sprintf("%s#%s", u.String(), explodedRefValue[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -1678,3 +1679,213 @@ func TestSetReference_nil(t *testing.T) {
|
|||||||
SetReference(nil, "#/pigeon/street")
|
SetReference(nil, "#/pigeon/street")
|
||||||
assert.NotEqual(t, "#/pigeon/street", n.GetReference())
|
assert.NotEqual(t, "#/pigeon/street", n.GetReference())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLocateRefNode_CurrentPathKey_HttpLink(t *testing.T) {
|
||||||
|
|
||||||
|
no := yaml.Node{
|
||||||
|
Kind: yaml.MappingNode,
|
||||||
|
Content: []*yaml.Node{
|
||||||
|
{
|
||||||
|
Kind: yaml.ScalarNode,
|
||||||
|
Value: "$ref",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Kind: yaml.ScalarNode,
|
||||||
|
Value: "http://cakes.com#/components/schemas/thing",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := context.WithValue(context.Background(), index.CurrentPathKey, "http://cakes.com#/components/schemas/thing")
|
||||||
|
|
||||||
|
idx := index.NewSpecIndexWithConfig(&no, index.CreateClosedAPIIndexConfig())
|
||||||
|
n, i, e, c := LocateRefNodeWithContext(ctx, &no, idx)
|
||||||
|
assert.Nil(t, n)
|
||||||
|
assert.NotNil(t, i)
|
||||||
|
assert.NotNil(t, e)
|
||||||
|
assert.NotNil(t, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLocateRefNode_CurrentPathKey_HttpLink_RemoteCtx(t *testing.T) {
|
||||||
|
|
||||||
|
no := yaml.Node{
|
||||||
|
Kind: yaml.MappingNode,
|
||||||
|
Content: []*yaml.Node{
|
||||||
|
{
|
||||||
|
Kind: yaml.ScalarNode,
|
||||||
|
Value: "$ref",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Kind: yaml.ScalarNode,
|
||||||
|
Value: "#/components/schemas/thing",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := context.WithValue(context.Background(), index.CurrentPathKey, "https://cakes.com#/components/schemas/thing")
|
||||||
|
idx := index.NewSpecIndexWithConfig(&no, index.CreateClosedAPIIndexConfig())
|
||||||
|
n, i, e, c := LocateRefNodeWithContext(ctx, &no, idx)
|
||||||
|
assert.Nil(t, n)
|
||||||
|
assert.NotNil(t, i)
|
||||||
|
assert.NotNil(t, e)
|
||||||
|
assert.NotNil(t, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLocateRefNode_CurrentPathKey_HttpLink_RemoteCtx_WithPath(t *testing.T) {
|
||||||
|
|
||||||
|
no := yaml.Node{
|
||||||
|
Kind: yaml.MappingNode,
|
||||||
|
Content: []*yaml.Node{
|
||||||
|
{
|
||||||
|
Kind: yaml.ScalarNode,
|
||||||
|
Value: "$ref",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Kind: yaml.ScalarNode,
|
||||||
|
Value: "#/components/schemas/thing",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := context.WithValue(context.Background(), index.CurrentPathKey, "https://cakes.com/jazzzy/shoes#/components/schemas/thing")
|
||||||
|
idx := index.NewSpecIndexWithConfig(&no, index.CreateClosedAPIIndexConfig())
|
||||||
|
n, i, e, c := LocateRefNodeWithContext(ctx, &no, idx)
|
||||||
|
assert.Nil(t, n)
|
||||||
|
assert.NotNil(t, i)
|
||||||
|
assert.NotNil(t, e)
|
||||||
|
assert.NotNil(t, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLocateRefNode_CurrentPathKey_Path_Link(t *testing.T) {
|
||||||
|
|
||||||
|
no := yaml.Node{
|
||||||
|
Kind: yaml.MappingNode,
|
||||||
|
Content: []*yaml.Node{
|
||||||
|
{
|
||||||
|
Kind: yaml.ScalarNode,
|
||||||
|
Value: "$ref",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Kind: yaml.ScalarNode,
|
||||||
|
Value: "yazzy.yaml#/components/schemas/thing",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := context.WithValue(context.Background(), index.CurrentPathKey, "/jazzzy/shoes.yaml")
|
||||||
|
idx := index.NewSpecIndexWithConfig(&no, index.CreateClosedAPIIndexConfig())
|
||||||
|
n, i, e, c := LocateRefNodeWithContext(ctx, &no, idx)
|
||||||
|
assert.Nil(t, n)
|
||||||
|
assert.NotNil(t, i)
|
||||||
|
assert.NotNil(t, e)
|
||||||
|
assert.NotNil(t, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLocateRefNode_CurrentPathKey_Path_URL(t *testing.T) {
|
||||||
|
|
||||||
|
no := yaml.Node{
|
||||||
|
Kind: yaml.MappingNode,
|
||||||
|
Content: []*yaml.Node{
|
||||||
|
{
|
||||||
|
Kind: yaml.ScalarNode,
|
||||||
|
Value: "$ref",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Kind: yaml.ScalarNode,
|
||||||
|
Value: "yazzy.yaml#/components/schemas/thing",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
cf := index.CreateClosedAPIIndexConfig()
|
||||||
|
u, _ := url.Parse("https://herbs-and-coffee-in-the-fall.com")
|
||||||
|
cf.BaseURL = u
|
||||||
|
idx := index.NewSpecIndexWithConfig(&no, cf)
|
||||||
|
n, i, e, c := LocateRefNodeWithContext(context.Background(), &no, idx)
|
||||||
|
assert.Nil(t, n)
|
||||||
|
assert.NotNil(t, i)
|
||||||
|
assert.NotNil(t, e)
|
||||||
|
assert.NotNil(t, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLocateRefNode_CurrentPathKey_DeeperPath_URL(t *testing.T) {
|
||||||
|
|
||||||
|
no := yaml.Node{
|
||||||
|
Kind: yaml.MappingNode,
|
||||||
|
Content: []*yaml.Node{
|
||||||
|
{
|
||||||
|
Kind: yaml.ScalarNode,
|
||||||
|
Value: "$ref",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Kind: yaml.ScalarNode,
|
||||||
|
Value: "slasshy/mazsshy/yazzy.yaml#/components/schemas/thing",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
cf := index.CreateClosedAPIIndexConfig()
|
||||||
|
u, _ := url.Parse("https://herbs-and-coffee-in-the-fall.com/pizza/burgers")
|
||||||
|
cf.BaseURL = u
|
||||||
|
idx := index.NewSpecIndexWithConfig(&no, cf)
|
||||||
|
n, i, e, c := LocateRefNodeWithContext(context.Background(), &no, idx)
|
||||||
|
assert.Nil(t, n)
|
||||||
|
assert.NotNil(t, i)
|
||||||
|
assert.NotNil(t, e)
|
||||||
|
assert.NotNil(t, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLocateRefNode_NoExplode(t *testing.T) {
|
||||||
|
|
||||||
|
no := yaml.Node{
|
||||||
|
Kind: yaml.MappingNode,
|
||||||
|
Content: []*yaml.Node{
|
||||||
|
{
|
||||||
|
Kind: yaml.ScalarNode,
|
||||||
|
Value: "$ref",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Kind: yaml.ScalarNode,
|
||||||
|
Value: "components/schemas/thing.yaml",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
cf := index.CreateClosedAPIIndexConfig()
|
||||||
|
u, _ := url.Parse("http://smiledfdfdfdfds.com/bikes")
|
||||||
|
cf.BaseURL = u
|
||||||
|
idx := index.NewSpecIndexWithConfig(&no, cf)
|
||||||
|
n, i, e, c := LocateRefNodeWithContext(context.Background(), &no, idx)
|
||||||
|
assert.Nil(t, n)
|
||||||
|
assert.NotNil(t, i)
|
||||||
|
assert.NotNil(t, e)
|
||||||
|
assert.NotNil(t, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLocateRefNode_NoExplode_HTTP(t *testing.T) {
|
||||||
|
|
||||||
|
no := yaml.Node{
|
||||||
|
Kind: yaml.MappingNode,
|
||||||
|
Content: []*yaml.Node{
|
||||||
|
{
|
||||||
|
Kind: yaml.ScalarNode,
|
||||||
|
Value: "$ref",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Kind: yaml.ScalarNode,
|
||||||
|
Value: "components/schemas/thing.yaml",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
cf := index.CreateClosedAPIIndexConfig()
|
||||||
|
u, _ := url.Parse("http://smilfghfhfhfhfhes.com/bikes")
|
||||||
|
cf.BaseURL = u
|
||||||
|
idx := index.NewSpecIndexWithConfig(&no, cf)
|
||||||
|
ctx := context.WithValue(context.Background(), index.CurrentPathKey, "http://minty-fresh-shoes.com/nice/no.yaml")
|
||||||
|
n, i, e, c := LocateRefNodeWithContext(ctx, &no, idx)
|
||||||
|
assert.Nil(t, n)
|
||||||
|
assert.NotNil(t, i)
|
||||||
|
assert.NotNil(t, e)
|
||||||
|
assert.NotNil(t, c)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user