mirror of
https://github.com/prymitive/karma
synced 2026-08-19 11:36:24 +00:00
fix(backend): remove file:// transport support
This commit is contained in:
@@ -1,62 +0,0 @@
|
||||
package uri
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type fileReader struct {
|
||||
fd *os.File
|
||||
}
|
||||
|
||||
func (fr *fileReader) Read(b []byte) (n int, err error) {
|
||||
return fr.fd.Read(b)
|
||||
}
|
||||
|
||||
func (fr *fileReader) Close() error {
|
||||
return fr.fd.Close()
|
||||
}
|
||||
|
||||
// FileURIReader can read data from file:// URIs
|
||||
type FileURIReader struct {
|
||||
}
|
||||
|
||||
func (r *FileURIReader) pathFromURI(uri string) (string, error) {
|
||||
u, err := url.Parse(uri)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// if we a file URI with an absolute path then return it
|
||||
if strings.HasPrefix(uri, "file:///") {
|
||||
return u.Path, nil
|
||||
}
|
||||
// if we have a file URI with relative path we need to expand it into an
|
||||
// absolute path, url.Parse doesn't support relative file paths
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
absolutePath := path.Join(cwd, u.Host, u.Path)
|
||||
return absolutePath, nil
|
||||
}
|
||||
|
||||
func (r *FileURIReader) Read(uri string, _ map[string]string) (io.ReadCloser, error) {
|
||||
filename, err := r.pathFromURI(uri)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Infof("Reading file '%s'", filename)
|
||||
fd, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fr := fileReader{fd: fd}
|
||||
return &fr, nil
|
||||
}
|
||||
@@ -28,8 +28,6 @@ func NewReader(uri string, timeout time.Duration, clientTransport http.RoundTrip
|
||||
Transport: clientTransport,
|
||||
}
|
||||
return &HTTPURIReader{client: client}, nil
|
||||
case "file":
|
||||
return &FileURIReader{}, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported URI scheme '%s' in '%s'", u.Scheme, SanitizeURI(u.String()))
|
||||
}
|
||||
|
||||
@@ -7,28 +7,14 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/prymitive/karma/internal/mock"
|
||||
"github.com/prymitive/karma/internal/uri"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func getFileSize(path string) int64 {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fi, err := file.Stat()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return fi.Size()
|
||||
}
|
||||
|
||||
type httpTransportTest struct {
|
||||
timeout time.Duration
|
||||
tlsConfig *tls.Config
|
||||
@@ -56,35 +42,6 @@ var httpTransportTests = []httpTransportTest{
|
||||
},
|
||||
}
|
||||
|
||||
type fileTransportTest struct {
|
||||
uri string
|
||||
failed bool
|
||||
timeout time.Duration
|
||||
size int64
|
||||
headers map[string]string
|
||||
}
|
||||
|
||||
var fileTransportTests = []fileTransportTest{
|
||||
{
|
||||
uri: fmt.Sprintf("file://%s", mock.GetAbsoluteMockPath("api/v2/status", mock.ListAllMocks()[0])),
|
||||
size: getFileSize(mock.GetAbsoluteMockPath("api/v2/status", mock.ListAllMocks()[0])),
|
||||
},
|
||||
{
|
||||
uri: "file:///non-existing-file.abcdef",
|
||||
failed: true,
|
||||
},
|
||||
{
|
||||
uri: "file://uri.go",
|
||||
size: getFileSize("uri.go"),
|
||||
failed: true,
|
||||
},
|
||||
{
|
||||
uri: "file://../uri/uri.go",
|
||||
size: getFileSize("uri.go"),
|
||||
failed: true,
|
||||
},
|
||||
}
|
||||
|
||||
func readAll(source io.ReadCloser) (int64, error) {
|
||||
var readSize int64
|
||||
b := make([]byte, 512)
|
||||
@@ -153,31 +110,3 @@ func TestHTTPReader(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFileReader(t *testing.T) {
|
||||
//log.SetLevel(log.FatalLevel)
|
||||
for _, testCase := range fileTransportTests {
|
||||
transp, err := uri.NewReader(testCase.uri, testCase.timeout, &http.Transport{}, testCase.headers)
|
||||
if err != nil {
|
||||
t.Errorf("[%v] failed to create new transport: %s", testCase, err)
|
||||
}
|
||||
|
||||
source, err := transp.Read(testCase.uri, testCase.headers)
|
||||
if err != nil {
|
||||
if !testCase.failed {
|
||||
t.Errorf("[%v] unexpected failure while creating reader: %s", testCase, err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
got, err := readAll(source)
|
||||
source.Close()
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("[%v] Read() failed: %s", testCase, err)
|
||||
}
|
||||
|
||||
if got != testCase.size {
|
||||
t.Errorf("[%v] Wrong response size, got %d, expected %d", testCase, got, testCase.size)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user