mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-10 12:37:48 +00:00
@@ -153,12 +153,8 @@ func createDocument(info *datamodel.SpecInfo, config *datamodel.DocumentConfigur
|
|||||||
|
|
||||||
// If basePath is provided, add a local filesystem to the rolodex.
|
// If basePath is provided, add a local filesystem to the rolodex.
|
||||||
if idxConfig.BasePath != "" {
|
if idxConfig.BasePath != "" {
|
||||||
var absError error
|
|
||||||
var cwd string
|
var cwd string
|
||||||
cwd, absError = filepath.Abs(config.BasePath)
|
cwd, _ = filepath.Abs(config.BasePath)
|
||||||
if absError != nil {
|
|
||||||
return nil, absError
|
|
||||||
}
|
|
||||||
// if a supplied local filesystem is provided, add it to the rolodex.
|
// if a supplied local filesystem is provided, add it to the rolodex.
|
||||||
if config.LocalFS != nil {
|
if config.LocalFS != nil {
|
||||||
rolodex.AddLocalFS(cwd, config.LocalFS)
|
rolodex.AddLocalFS(cwd, config.LocalFS)
|
||||||
@@ -184,27 +180,19 @@ func createDocument(info *datamodel.SpecInfo, config *datamodel.DocumentConfigur
|
|||||||
// if base url is provided, add a remote filesystem to the rolodex.
|
// if base url is provided, add a remote filesystem to the rolodex.
|
||||||
if idxConfig.BaseURL != nil {
|
if idxConfig.BaseURL != nil {
|
||||||
|
|
||||||
// if a supplied remote filesystem is provided, add it to the rolodex.
|
// create a remote filesystem
|
||||||
if config.RemoteFS != nil {
|
remoteFS, fsErr := index.NewRemoteFSWithConfig(idxConfig)
|
||||||
if config.BaseURL == nil {
|
if fsErr != nil {
|
||||||
return nil, errors.New("cannot use remote filesystem without a BaseURL")
|
return nil, fsErr
|
||||||
}
|
|
||||||
rolodex.AddRemoteFS(config.BaseURL.String(), config.RemoteFS)
|
|
||||||
|
|
||||||
} else {
|
|
||||||
// create a remote filesystem
|
|
||||||
remoteFS, fsErr := index.NewRemoteFSWithConfig(idxConfig)
|
|
||||||
if fsErr != nil {
|
|
||||||
return nil, fsErr
|
|
||||||
}
|
|
||||||
if config.RemoteURLHandler != nil {
|
|
||||||
remoteFS.RemoteHandlerFunc = config.RemoteURLHandler
|
|
||||||
}
|
|
||||||
idxConfig.AllowRemoteLookup = true
|
|
||||||
|
|
||||||
// add to the rolodex
|
|
||||||
rolodex.AddRemoteFS(config.BaseURL.String(), remoteFS)
|
|
||||||
}
|
}
|
||||||
|
if config.RemoteURLHandler != nil {
|
||||||
|
remoteFS.RemoteHandlerFunc = config.RemoteURLHandler
|
||||||
|
}
|
||||||
|
idxConfig.AllowRemoteLookup = true
|
||||||
|
|
||||||
|
// add to the rolodex
|
||||||
|
rolodex.AddRemoteFS(config.BaseURL.String(), remoteFS)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var errs []error
|
var errs []error
|
||||||
|
|||||||
@@ -5,7 +5,10 @@ package v2
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/pb33f/libopenapi/index"
|
||||||
"github.com/pb33f/libopenapi/utils"
|
"github.com/pb33f/libopenapi/utils"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -347,3 +350,120 @@ func TestCircularReferenceError(t *testing.T) {
|
|||||||
assert.Len(t, utils.UnwrapErrors(err), 3)
|
assert.Len(t, utils.UnwrapErrors(err), 3)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRolodexLocalFileSystem(t *testing.T) {
|
||||||
|
data, _ := os.ReadFile("../../../test_specs/first.yaml")
|
||||||
|
info, _ := datamodel.ExtractSpecInfo(data)
|
||||||
|
|
||||||
|
cf := datamodel.NewDocumentConfiguration()
|
||||||
|
cf.BasePath = "../../../test_specs"
|
||||||
|
cf.FileFilter = []string{"first.yaml", "second.yaml", "third.yaml"}
|
||||||
|
lDoc, err := CreateDocumentFromConfig(info, cf)
|
||||||
|
assert.NotNil(t, lDoc)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRolodexLocalFileSystem_ProvideNonRolodexFS(t *testing.T) {
|
||||||
|
data, _ := os.ReadFile("../../../test_specs/first.yaml")
|
||||||
|
info, _ := datamodel.ExtractSpecInfo(data)
|
||||||
|
baseDir := "../../../test_specs"
|
||||||
|
|
||||||
|
cf := datamodel.NewDocumentConfiguration()
|
||||||
|
cf.BasePath = baseDir
|
||||||
|
cf.FileFilter = []string{"first.yaml", "second.yaml", "third.yaml"}
|
||||||
|
cf.LocalFS = os.DirFS(baseDir)
|
||||||
|
lDoc, err := CreateDocumentFromConfig(info, cf)
|
||||||
|
assert.NotNil(t, lDoc)
|
||||||
|
assert.Error(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRolodexLocalFileSystem_ProvideRolodexFS(t *testing.T) {
|
||||||
|
data, _ := os.ReadFile("../../../test_specs/first.yaml")
|
||||||
|
info, _ := datamodel.ExtractSpecInfo(data)
|
||||||
|
baseDir := "../../../test_specs"
|
||||||
|
cf := datamodel.NewDocumentConfiguration()
|
||||||
|
cf.BasePath = baseDir
|
||||||
|
cf.FileFilter = []string{"first.yaml", "second.yaml", "third.yaml"}
|
||||||
|
|
||||||
|
localFS, lErr := index.NewLocalFSWithConfig(&index.LocalFSConfig{
|
||||||
|
BaseDirectory: baseDir,
|
||||||
|
DirFS: os.DirFS(baseDir),
|
||||||
|
FileFilters: cf.FileFilter,
|
||||||
|
})
|
||||||
|
cf.LocalFS = localFS
|
||||||
|
|
||||||
|
assert.NoError(t, lErr)
|
||||||
|
lDoc, err := CreateDocumentFromConfig(info, cf)
|
||||||
|
assert.NotNil(t, lDoc)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRolodexLocalFileSystem_BadPath(t *testing.T) {
|
||||||
|
data, _ := os.ReadFile("../../../test_specs/first.yaml")
|
||||||
|
info, _ := datamodel.ExtractSpecInfo(data)
|
||||||
|
|
||||||
|
cf := datamodel.NewDocumentConfiguration()
|
||||||
|
cf.BasePath = "/NOWHERE"
|
||||||
|
cf.FileFilter = []string{"first.yaml", "second.yaml", "third.yaml"}
|
||||||
|
lDoc, err := CreateDocumentFromConfig(info, cf)
|
||||||
|
assert.Nil(t, lDoc)
|
||||||
|
assert.Error(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRolodexRemoteFileSystem(t *testing.T) {
|
||||||
|
data, _ := os.ReadFile("../../../test_specs/first.yaml")
|
||||||
|
info, _ := datamodel.ExtractSpecInfo(data)
|
||||||
|
|
||||||
|
cf := datamodel.NewDocumentConfiguration()
|
||||||
|
|
||||||
|
baseUrl := "https://raw.githubusercontent.com/pb33f/libopenapi/main/test_specs"
|
||||||
|
u, _ := url.Parse(baseUrl)
|
||||||
|
cf.BaseURL = u
|
||||||
|
lDoc, err := CreateDocumentFromConfig(info, cf)
|
||||||
|
assert.NotNil(t, lDoc)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRolodexRemoteFileSystem_BadBase(t *testing.T) {
|
||||||
|
data, _ := os.ReadFile("../../../test_specs/first.yaml")
|
||||||
|
info, _ := datamodel.ExtractSpecInfo(data)
|
||||||
|
|
||||||
|
cf := datamodel.NewDocumentConfiguration()
|
||||||
|
|
||||||
|
baseUrl := "https://no-no-this-will-not-work-it-just-will-not-get-the-job-done-mate.com"
|
||||||
|
u, _ := url.Parse(baseUrl)
|
||||||
|
cf.BaseURL = u
|
||||||
|
lDoc, err := CreateDocumentFromConfig(info, cf)
|
||||||
|
assert.NotNil(t, lDoc)
|
||||||
|
assert.Error(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRolodexRemoteFileSystem_CustomRemote_NoBaseURL(t *testing.T) {
|
||||||
|
data, _ := os.ReadFile("../../../test_specs/first.yaml")
|
||||||
|
info, _ := datamodel.ExtractSpecInfo(data)
|
||||||
|
|
||||||
|
cf := datamodel.NewDocumentConfiguration()
|
||||||
|
cf.RemoteFS, _ = index.NewRemoteFSWithConfig(&index.SpecIndexConfig{})
|
||||||
|
lDoc, err := CreateDocumentFromConfig(info, cf)
|
||||||
|
assert.NotNil(t, lDoc)
|
||||||
|
assert.Error(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRolodexRemoteFileSystem_CustomHttpHandler(t *testing.T) {
|
||||||
|
data, _ := os.ReadFile("../../../test_specs/first.yaml")
|
||||||
|
info, _ := datamodel.ExtractSpecInfo(data)
|
||||||
|
|
||||||
|
cf := datamodel.NewDocumentConfiguration()
|
||||||
|
cf.RemoteURLHandler = http.Get
|
||||||
|
baseUrl := "https://no-no-this-will-not-work-it-just-will-not-get-the-job-done-mate.com"
|
||||||
|
u, _ := url.Parse(baseUrl)
|
||||||
|
cf.BaseURL = u
|
||||||
|
|
||||||
|
pizza := func(url string) (resp *http.Response, err error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
cf.RemoteURLHandler = pizza
|
||||||
|
lDoc, err := CreateDocumentFromConfig(info, cf)
|
||||||
|
assert.NotNil(t, lDoc)
|
||||||
|
assert.Error(t, err)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user