Skip to content

Commit

Permalink
added more coverage
Browse files Browse the repository at this point in the history
Signed-off-by: quobix <[email protected]>
  • Loading branch information
daveshanley committed Feb 20, 2024
1 parent a6496ae commit f32dd87
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
3 changes: 2 additions & 1 deletion index/rolodex_remote_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,8 @@ func (i *RemoteFS) Open(remoteURL string) (fs.File, error) {

i.logger.Error("unable to fetch remote document",
"file", remoteParsedURL.Path, "status", response.StatusCode, "resp", string(responseBytes))
return nil, fmt.Errorf("unable to fetch remote document: %s", string(responseBytes))
return nil, fmt.Errorf("unable to fetch remote document '%s' (error %d)", remoteParsedURL.String(),
response.StatusCode)
}

absolutePath := remoteParsedURL.Path
Expand Down
39 changes: 39 additions & 0 deletions index/rolodex_remote_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package index

import (
"bytes"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -348,6 +349,44 @@ func TestNewRemoteFS_RemoteBaseURL_NoErrorNoResponse(t *testing.T) {
assert.Equal(t, "empty response from remote URL: https://pb33f.io/woof.yaml", y.Error())
}

func TestNewRemoteFS_RemoteBaseURL_200_NotOpenAPI(t *testing.T) {
cf := CreateOpenAPIIndexConfig()
h := func(url string) (*http.Response, error) {
b := io.NopCloser(bytes.NewBuffer([]byte("not openapi")))
return &http.Response{StatusCode: 200, Body: b}, nil
}
cf.RemoteURLHandler = h

cf.BaseURL, _ = url.Parse("https://pb33f.io/the/love/machine")
rfs, _ := NewRemoteFSWithConfig(cf)

rolo := NewRolodex(cf)
rolo.AddRemoteFS("https://pb33f.io/the/love/machine", rfs)
f, e := rolo.Open("https://pb33f.io/woof.yaml")
assert.NoError(t, e)
c, err := f.(*rolodexFile).Index(cf)
assert.Nil(t, c)
assert.Error(t, err)

}

func TestNewRemoteFS_RemoteBaseURL_Error400(t *testing.T) {
cf := CreateOpenAPIIndexConfig()
h := func(url string) (*http.Response, error) {
b := io.NopCloser(bytes.NewBuffer([]byte{}))
return &http.Response{StatusCode: 400, Body: b}, nil
}
cf.RemoteURLHandler = h

cf.BaseURL, _ = url.Parse("https://pb33f.io/the/love/machine")
rfs, _ := NewRemoteFSWithConfig(cf)

x, y := rfs.Open("https://pb33f.io/woof.yaml")
assert.Nil(t, x)
assert.Error(t, y)
assert.Equal(t, "unable to fetch remote document 'https://pb33f.io/woof.yaml' (error 400)", y.Error())
}

func TestNewRemoteFS_RemoteBaseURL_ReadBodyFail(t *testing.T) {
cf := CreateOpenAPIIndexConfig()
h := func(url string) (*http.Response, error) {
Expand Down

0 comments on commit f32dd87

Please sign in to comment.