parent
02f8a32b46
commit
534b93e142
13 changed files with 425 additions and 29 deletions
1
util/embedfs/test.txt
Normal file
1
util/embedfs/test.txt
Normal file
|
@ -0,0 +1 @@
|
|||
This is a test file for embedfs_test.go
|
44
util/embedfs_test.go
Normal file
44
util/embedfs_test.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"github.com/stretchr/testify/require"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
modTime = time.Now()
|
||||
|
||||
//go:embed embedfs
|
||||
testFs embed.FS
|
||||
testFsCached = &CachingEmbedFS{ModTime: modTime, FS: testFs}
|
||||
)
|
||||
|
||||
func TestCachingEmbedFS(t *testing.T) {
|
||||
s := http.FileServer(http.FS(testFsCached))
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/embedfs/test.txt", nil)
|
||||
s.ServeHTTP(rr, req)
|
||||
require.Equal(t, 200, rr.Code)
|
||||
lastModified := rr.Header().Get("Last-Modified")
|
||||
|
||||
rr = httptest.NewRecorder()
|
||||
req, _ = http.NewRequest("GET", "/embedfs/test.txt", nil)
|
||||
req.Header.Set("If-Modified-Since", lastModified)
|
||||
s.ServeHTTP(rr, req)
|
||||
require.Equal(t, 304, rr.Code) // Huzzah!
|
||||
}
|
||||
|
||||
func TestCachingEmbedFS_Range(t *testing.T) {
|
||||
s := http.FileServer(http.FS(testFsCached))
|
||||
rr := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/embedfs/test.txt", nil)
|
||||
req.Header.Set("Range", "bytes=1-20")
|
||||
s.ServeHTTP(rr, req)
|
||||
require.Equal(t, 206, rr.Code)
|
||||
require.Equal(t, "his is a test file f", rr.Body.String())
|
||||
}
|
|
@ -58,8 +58,3 @@ func (l *Limiter) Value() int64 {
|
|||
defer l.mu.Unlock()
|
||||
return l.value
|
||||
}
|
||||
|
||||
// Limit returns the defined limit
|
||||
func (l *Limiter) Limit() int64 {
|
||||
return l.limit
|
||||
}
|
||||
|
|
30
util/limit_test.go
Normal file
30
util/limit_test.go
Normal file
|
@ -0,0 +1,30 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLimiter_Add(t *testing.T) {
|
||||
l := NewLimiter(10)
|
||||
if err := l.Add(5); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := l.Add(5); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := l.Add(5); err != ErrLimitReached {
|
||||
t.Fatalf("expected ErrLimitReached, got %#v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLimiter_AddSub(t *testing.T) {
|
||||
l := NewLimiter(10)
|
||||
l.Add(5)
|
||||
if l.Value() != 5 {
|
||||
t.Fatalf("expected value to be %d, got %d", 5, l.Value())
|
||||
}
|
||||
l.Sub(2)
|
||||
if l.Value() != 3 {
|
||||
t.Fatalf("expected value to be %d, got %d", 3, l.Value())
|
||||
}
|
||||
}
|
56
util/util_test.go
Normal file
56
util/util_test.go
Normal file
|
@ -0,0 +1,56 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/require"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestDurationToHuman_SevenDays(t *testing.T) {
|
||||
d := 7 * 24 * time.Hour
|
||||
require.Equal(t, "7d", DurationToHuman(d))
|
||||
}
|
||||
|
||||
func TestDurationToHuman_MoreThanOneDay(t *testing.T) {
|
||||
d := 49 * time.Hour
|
||||
require.Equal(t, "2d1h", DurationToHuman(d))
|
||||
}
|
||||
|
||||
func TestDurationToHuman_LessThanOneDay(t *testing.T) {
|
||||
d := 17*time.Hour + 15*time.Minute
|
||||
require.Equal(t, "17h15m", DurationToHuman(d))
|
||||
}
|
||||
|
||||
func TestDurationToHuman_TenOfThings(t *testing.T) {
|
||||
d := 10*time.Hour + 10*time.Minute + 10*time.Second
|
||||
require.Equal(t, "10h10m10s", DurationToHuman(d))
|
||||
}
|
||||
|
||||
func TestDurationToHuman_Zero(t *testing.T) {
|
||||
require.Equal(t, "0", DurationToHuman(0))
|
||||
}
|
||||
|
||||
func TestRandomString(t *testing.T) {
|
||||
s1 := RandomString(10)
|
||||
s2 := RandomString(10)
|
||||
s3 := RandomString(12)
|
||||
require.Equal(t, 10, len(s1))
|
||||
require.Equal(t, 10, len(s2))
|
||||
require.Equal(t, 12, len(s3))
|
||||
require.NotEqual(t, s1, s2)
|
||||
}
|
||||
|
||||
func TestFileExists(t *testing.T) {
|
||||
filename := filepath.Join(t.TempDir(), "somefile.txt")
|
||||
require.Nil(t, ioutil.WriteFile(filename, []byte{0x25, 0x86}, 0600))
|
||||
require.True(t, FileExists(filename))
|
||||
require.False(t, FileExists(filename+".doesnotexist"))
|
||||
}
|
||||
|
||||
func TestInStringList(t *testing.T) {
|
||||
s := []string{"one", "two"}
|
||||
require.True(t, InStringList(s, "two"))
|
||||
require.False(t, InStringList(s, "three"))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue