Merge branch 'main' into ordered-libopenapi

This commit is contained in:
Tristan Cartledge
2023-11-28 08:59:00 +00:00

View File

@@ -6,13 +6,14 @@ package index
import (
"errors"
"fmt"
"github.com/stretchr/testify/assert"
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var test_httpClient = &http.Client{Timeout: time.Duration(60) * time.Second}
@@ -84,11 +85,10 @@ func test_buildServer() *httptest.Server {
}
func TestNewRemoteFS_BasicCheck(t *testing.T) {
server := test_buildServer()
defer server.Close()
//remoteFS := NewRemoteFS("https://raw.githubusercontent.com/digitalocean/openapi/main/specification/")
// remoteFS := NewRemoteFS("https://raw.githubusercontent.com/digitalocean/openapi/main/specification/")
remoteFS, _ := NewRemoteFSWithRootURL(server.URL)
remoteFS.RemoteHandlerFunc = test_httpClient.Get
@@ -106,11 +106,10 @@ func TestNewRemoteFS_BasicCheck(t *testing.T) {
assert.Len(t, bytes, 53)
lastMod := stat.ModTime()
assert.Equal(t, "2015-10-21 07:28:00 +0000 GMT", lastMod.String())
assert.Equal(t, "2015-10-21 07:28:00 +0000 UTC", lastMod.UTC().String())
}
func TestNewRemoteFS_BasicCheck_NoScheme(t *testing.T) {
server := test_buildServer()
defer server.Close()
@@ -124,7 +123,6 @@ func TestNewRemoteFS_BasicCheck_NoScheme(t *testing.T) {
}
func TestNewRemoteFS_BasicCheck_Relative(t *testing.T) {
server := test_buildServer()
defer server.Close()
@@ -146,11 +144,10 @@ func TestNewRemoteFS_BasicCheck_Relative(t *testing.T) {
assert.Equal(t, int64(64), stat.Size())
lastMod := stat.ModTime()
assert.Equal(t, "2015-10-21 08:28:00 +0000 GMT", lastMod.String())
assert.Equal(t, "2015-10-21 08:28:00 +0000 UTC", lastMod.UTC().String())
}
func TestNewRemoteFS_BasicCheck_Relative_Deeper(t *testing.T) {
server := test_buildServer()
defer server.Close()
@@ -186,11 +183,10 @@ func TestNewRemoteFS_BasicCheck_Relative_Deeper(t *testing.T) {
assert.Nil(t, file.(*RemoteFile).Close())
lastMod := stat.ModTime()
assert.Equal(t, "2015-10-21 10:28:00 +0000 GMT", lastMod.String())
assert.Equal(t, "2015-10-21 10:28:00 +0000 UTC", lastMod.UTC().String())
}
func TestRemoteFile_NoContent(t *testing.T) {
rf := &RemoteFile{}
x, y := rf.GetContentAsYAMLNode()
assert.Nil(t, x)
@@ -198,7 +194,6 @@ func TestRemoteFile_NoContent(t *testing.T) {
}
func TestRemoteFile_BadContent(t *testing.T) {
rf := &RemoteFile{data: []byte("bad: data: on: a single: line: makes: for: unhappy: yaml"), index: &SpecIndex{}}
x, y := rf.GetContentAsYAMLNode()
assert.Nil(t, x)
@@ -206,7 +201,6 @@ func TestRemoteFile_BadContent(t *testing.T) {
}
func TestRemoteFile_GoodContent(t *testing.T) {
rf := &RemoteFile{data: []byte("good: data"), index: &SpecIndex{}}
x, y := rf.GetContentAsYAMLNode()
assert.NotNil(t, x)
@@ -218,37 +212,29 @@ func TestRemoteFile_GoodContent(t *testing.T) {
d, err := io.ReadAll(rf)
assert.Empty(t, d)
assert.Error(t, err)
}
func TestRemoteFile_Index_AlreadySet(t *testing.T) {
rf := &RemoteFile{data: []byte("good: data"), index: &SpecIndex{}}
x, y := rf.Index(&SpecIndexConfig{})
assert.NotNil(t, x)
assert.NoError(t, y)
}
func TestRemoteFile_Index_BadContent(t *testing.T) {
rf := &RemoteFile{data: []byte("no: sleep: until: the bugs: weep")}
x, y := rf.Index(&SpecIndexConfig{})
assert.Nil(t, x)
assert.Error(t, y)
}
func TestRemoteFS_NoConfig(t *testing.T) {
x, y := NewRemoteFSWithConfig(nil)
assert.Nil(t, x)
assert.Error(t, y)
}
func TestRemoteFS_SetRemoteHandler(t *testing.T) {
h := func(url string) (*http.Response, error) {
return nil, errors.New("nope")
}
@@ -270,7 +256,6 @@ func TestRemoteFS_SetRemoteHandler(t *testing.T) {
assert.Nil(t, i)
assert.Error(t, n)
assert.Equal(t, "nope", n.Error())
}
func TestRemoteFS_NoConfigBadURL(t *testing.T) {
@@ -280,34 +265,27 @@ func TestRemoteFS_NoConfigBadURL(t *testing.T) {
}
func TestNewRemoteFS_Open_NoConfig(t *testing.T) {
rfs := &RemoteFS{}
x, y := rfs.Open("https://pb33f.io")
assert.Nil(t, x)
assert.Error(t, y)
}
func TestNewRemoteFS_Open_ConfigNotAllowed(t *testing.T) {
rfs := &RemoteFS{indexConfig: CreateClosedAPIIndexConfig()}
x, y := rfs.Open("https://pb33f.io")
assert.Nil(t, x)
assert.Error(t, y)
}
func TestNewRemoteFS_Open_BadURL(t *testing.T) {
rfs := &RemoteFS{indexConfig: CreateOpenAPIIndexConfig()}
x, y := rfs.Open("I am not a URL. I am a box of candy.. yum yum yum:: in my tum tum tum")
assert.Nil(t, x)
assert.Error(t, y)
}
func TestNewRemoteFS_RemoteBaseURL_RelativeRequest(t *testing.T) {
cf := CreateOpenAPIIndexConfig()
h := func(url string) (*http.Response, error) {
return nil, fmt.Errorf("nope, not having it %s", url)
@@ -321,11 +299,9 @@ func TestNewRemoteFS_RemoteBaseURL_RelativeRequest(t *testing.T) {
assert.Nil(t, x)
assert.Error(t, y)
assert.Equal(t, "nope, not having it https://pb33f.io/the/love/machine/gib/gab/jib/jab.yaml", y.Error())
}
func TestNewRemoteFS_RemoteBaseURL_BadRequestButContainsBody(t *testing.T) {
cf := CreateOpenAPIIndexConfig()
h := func(url string) (*http.Response, error) {
return &http.Response{}, fmt.Errorf("it's bad, but who cares %s", url)
@@ -339,11 +315,9 @@ func TestNewRemoteFS_RemoteBaseURL_BadRequestButContainsBody(t *testing.T) {
assert.Nil(t, x)
assert.Error(t, y)
assert.Equal(t, "it's bad, but who cares https://pb33f.io/woof.yaml", y.Error())
}
func TestNewRemoteFS_RemoteBaseURL_NoErrorNoResponse(t *testing.T) {
cf := CreateOpenAPIIndexConfig()
h := func(url string) (*http.Response, error) {
return nil, nil // useless!
@@ -360,7 +334,6 @@ func TestNewRemoteFS_RemoteBaseURL_NoErrorNoResponse(t *testing.T) {
}
func TestNewRemoteFS_RemoteBaseURL_ReadBodyFail(t *testing.T) {
cf := CreateOpenAPIIndexConfig()
h := func(url string) (*http.Response, error) {
r := &http.Response{}
@@ -380,7 +353,6 @@ func TestNewRemoteFS_RemoteBaseURL_ReadBodyFail(t *testing.T) {
}
func TestNewRemoteFS_RemoteBaseURL_EmptySpecFailIndex(t *testing.T) {
cf := CreateOpenAPIIndexConfig()
h := func(url string) (*http.Response, error) {
r := &http.Response{}
@@ -399,7 +371,6 @@ func TestNewRemoteFS_RemoteBaseURL_EmptySpecFailIndex(t *testing.T) {
}
func TestNewRemoteFS_Unsupported(t *testing.T) {
cf := CreateOpenAPIIndexConfig()
rfs, _ := NewRemoteFSWithConfig(cf)