64 lines
1.5 KiB
Go
64 lines
1.5 KiB
Go
|
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
||
|
|
||
|
package transport
|
||
|
|
||
|
import (
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
"testing"
|
||
|
"fmt"
|
||
|
_ "os"
|
||
|
"io/fs"
|
||
|
_ "path/filepath"
|
||
|
"net/http"
|
||
|
"net/http/httptest"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func TestNewHTTPFileInfo(t *testing.T) {
|
||
|
for _, v := range []struct { Valid bool; URL string } {
|
||
|
{ Valid: true, URL: "https://localhost/test" },
|
||
|
{ Valid: false, URL: "a_b://localhost/test" },
|
||
|
} {
|
||
|
h := NewHTTPFileInfo(v.URL)
|
||
|
if v.Valid {
|
||
|
assert.NotNil(t, h)
|
||
|
assert.NotNil(t, h.parsedURI)
|
||
|
} else {
|
||
|
assert.Nil(t, h)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestHTTPFileInfo(t *testing.T) {
|
||
|
expectedTime := time.Now()
|
||
|
body := []byte(`
|
||
|
type: "user"
|
||
|
attributes:
|
||
|
name: "foo"
|
||
|
gecos: "foo user"
|
||
|
`)
|
||
|
|
||
|
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||
|
assert.Equal(t, req.URL.String(), "/resource/user")
|
||
|
rw.Header().Add("Content-Type", "application/yaml")
|
||
|
n,e := rw.Write(body)
|
||
|
assert.Nil(t, e)
|
||
|
assert.Greater(t, n, 0)
|
||
|
}))
|
||
|
defer server.Close()
|
||
|
uri := fmt.Sprintf("%s/resource/user", server.URL)
|
||
|
|
||
|
r, readerErr := NewReaderURI(uri)
|
||
|
assert.Nil(t, readerErr)
|
||
|
|
||
|
fi, statErr := r.Stat()
|
||
|
assert.Nil(t, statErr)
|
||
|
|
||
|
assert.Equal(t, "user", fi.Name())
|
||
|
assert.Equal(t, int64(len(body)), fi.Size())
|
||
|
assert.Equal(t, "application/yaml", fi.Sys().(*HTTPFileInfo).ContentType)
|
||
|
assert.Greater(t, expectedTime, fi.ModTime())
|
||
|
assert.False(t, fi.IsDir())
|
||
|
assert.Equal(t, fs.FileMode(0777), fi.Mode())
|
||
|
}
|