mirror of
https://github.com/Bornholm/formidable.git
synced 2025-07-22 21:41:35 +02:00
feat: url based multi-format loaders/decoders
This commit is contained in:
39
internal/data/scheme/http/loader_handler.go
Normal file
39
internal/data/scheme/http/loader_handler.go
Normal file
@ -0,0 +1,39 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
SchemeHTTP = "http"
|
||||
SchemeHTTPS = "https"
|
||||
)
|
||||
|
||||
type LoaderHandler struct {
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
func (h *LoaderHandler) Match(url *url.URL) bool {
|
||||
return url.Scheme == SchemeHTTP || url.Scheme == SchemeHTTPS
|
||||
}
|
||||
|
||||
func (h *LoaderHandler) Open(url *url.URL) (io.ReadCloser, error) {
|
||||
res, err := h.client.Get(url.String())
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return nil, errors.Errorf("unexpected status code '%d (%s)'", res.StatusCode, http.StatusText(res.StatusCode))
|
||||
}
|
||||
|
||||
return res.Body, nil
|
||||
}
|
||||
|
||||
func NewLoaderHandler(client *http.Client) *LoaderHandler {
|
||||
return &LoaderHandler{client}
|
||||
}
|
87
internal/data/scheme/http/loader_handler_test.go
Normal file
87
internal/data/scheme/http/loader_handler_test.go
Normal file
@ -0,0 +1,87 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
testDataDir = "../testdata"
|
||||
dummyPath = "dummy.txt"
|
||||
)
|
||||
|
||||
type loaderHandlerTestCase struct {
|
||||
URL string
|
||||
ExpectMatch bool
|
||||
ExpectOpenError bool
|
||||
ExpectOpenContent string
|
||||
}
|
||||
|
||||
func TestLoaderHandler(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
staticHandler := http.FileServer(http.Dir(testDataDir))
|
||||
|
||||
server := httptest.NewServer(staticHandler)
|
||||
defer server.Close()
|
||||
|
||||
loaderHandlerTestCases := []loaderHandlerTestCase{
|
||||
{
|
||||
URL: server.URL + "/" + dummyPath,
|
||||
ExpectMatch: true,
|
||||
ExpectOpenError: false,
|
||||
ExpectOpenContent: "dummy",
|
||||
},
|
||||
}
|
||||
|
||||
handler := NewLoaderHandler(server.Client())
|
||||
|
||||
for _, tc := range loaderHandlerTestCases {
|
||||
func(tc loaderHandlerTestCase) {
|
||||
t.Run(fmt.Sprintf("Load '%s'", tc.URL), func(t *testing.T) {
|
||||
url, err := url.Parse(tc.URL)
|
||||
if err != nil {
|
||||
t.Fatal(errors.Wrapf(err, "could not parse url '%s'", tc.URL))
|
||||
}
|
||||
|
||||
if e, g := tc.ExpectMatch, handler.Match(url); e != g {
|
||||
t.Errorf("URL '%s': expected matching result '%v', got '%v'", tc.URL, e, g)
|
||||
}
|
||||
|
||||
if !tc.ExpectMatch {
|
||||
return
|
||||
}
|
||||
|
||||
reader, err := handler.Open(url)
|
||||
if err != nil && !tc.ExpectOpenError {
|
||||
t.Fatal(errors.Wrapf(err, "could not open url '%s'", url.String()))
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err := reader.Close(); err != nil {
|
||||
t.Error(errors.WithStack(err))
|
||||
}
|
||||
}()
|
||||
|
||||
if tc.ExpectOpenError {
|
||||
t.Fatal(errors.Errorf("no error was returned as expected when opening url '%s'", url.String()))
|
||||
}
|
||||
|
||||
data, err := io.ReadAll(reader)
|
||||
if err != nil {
|
||||
t.Fatal(errors.WithStack(err))
|
||||
}
|
||||
|
||||
if e, g := tc.ExpectOpenContent, string(data); e != g {
|
||||
t.Errorf("URL '%s': expected content'%v', got '%v'", tc.URL, e, g)
|
||||
}
|
||||
})
|
||||
}(tc)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user