This commit is contained in:
1 parent
72bc974aa6
commit
9ec2585030
12 files changed
+299
-54
No files matched your search
@@ -98,7 +98,7 @@ func (m *MockFS) ReadDir(path string) ([]os.FileInfo, error) {
|
||||
if len(prefix) > 0 && prefix[len(prefix)-1] != '/' {
|
||||
prefix += "/"
|
||||
}
|
||||
|
||||
|
||||
if len(p) > len(prefix) && p[:len(prefix)] == prefix {
|
||||
// Ensure direct child (no more slashes)
|
||||
rel := p[len(prefix):]
|
||||
@@ -150,7 +150,7 @@ func TestCachedClient_Stat(t *testing.T) {
|
||||
mockFS := NewMockFS()
|
||||
mockFS.AddFile("/test.txt", []byte("hello"))
|
||||
|
||||
c := NewCachedClient(mockFS, client, time.Hour)
|
||||
c := NewCachedClient(mockFS, client, time.Hour, time.Hour*2)
|
||||
|
||||
// First call: Cache miss
|
||||
info, err := c.Stat("/test.txt")
|
||||
@@ -209,7 +209,7 @@ func TestCachedClient_Read(t *testing.T) {
|
||||
content := []byte("hello world")
|
||||
mockFS.AddFile("/read.txt", content)
|
||||
|
||||
c := NewCachedClient(mockFS, client, time.Hour)
|
||||
c := NewCachedClient(mockFS, client, time.Hour, time.Hour*2)
|
||||
|
||||
// First call: Miss
|
||||
data, err := c.Read("/read.txt")
|
||||
@@ -257,7 +257,7 @@ func TestCachedClient_ReadDir(t *testing.T) {
|
||||
mockFS.AddFile("/dir/f1.txt", []byte("1"))
|
||||
mockFS.AddFile("/dir/f2.txt", []byte("2"))
|
||||
|
||||
c := NewCachedClient(mockFS, client, time.Hour)
|
||||
c := NewCachedClient(mockFS, client, time.Hour, time.Hour*2)
|
||||
|
||||
// First call: Miss
|
||||
infos, err := c.ReadDir("/dir")
|
||||
@@ -303,7 +303,7 @@ func TestCachedClient_RevalidateStat(t *testing.T) {
|
||||
mockFS := NewMockFS()
|
||||
mockFS.AddFile("/reval.txt", []byte("initial"))
|
||||
|
||||
c := NewCachedClient(mockFS, client, time.Hour)
|
||||
c := NewCachedClient(mockFS, client, time.Hour, time.Hour*2)
|
||||
|
||||
// Populate cache
|
||||
_, _ = c.Stat("/reval.txt")
|
||||
@@ -342,7 +342,7 @@ func TestCachedClient_RevalidateStat(t *testing.T) {
|
||||
// We can inspect redis directly or trust `Stat` returns cached value (which should be updated).
|
||||
// Let's inspect redis to be sure.
|
||||
// Or just call Stat again. It should be a cache HIT, but return NEW values.
|
||||
|
||||
|
||||
// Reset stats to check hit
|
||||
c.hits.Store(0)
|
||||
c.misses.Store(0)
|
||||
@@ -351,11 +351,11 @@ func TestCachedClient_RevalidateStat(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("Stat failed: %v", err)
|
||||
}
|
||||
|
||||
|
||||
if info.Size() != int64(len("updated content")) {
|
||||
t.Errorf("expected size %d, got %d", len("updated content"), info.Size())
|
||||
}
|
||||
|
||||
|
||||
// Should be a hit because revalidate updated the cache entry, so it exists and is valid.
|
||||
if c.hits.Load() != 1 {
|
||||
t.Errorf("expected 1 hit, got %d", c.hits.Load())
|
||||
@@ -381,7 +381,7 @@ func TestCachedClient_RevalidateRead(t *testing.T) {
|
||||
mockFS := NewMockFS()
|
||||
mockFS.AddFile("/read_reval.txt", []byte("initial"))
|
||||
|
||||
c := NewCachedClient(mockFS, client, time.Hour)
|
||||
c := NewCachedClient(mockFS, client, time.Hour, time.Hour*2)
|
||||
|
||||
// Populate cache
|
||||
_, _ = c.Read("/read_reval.txt")
|
||||
@@ -431,7 +431,7 @@ func TestCachedClient_RevalidateReadDir(t *testing.T) {
|
||||
mockFS.AddDir("/dir_reval")
|
||||
mockFS.AddFile("/dir_reval/f1.txt", []byte("1"))
|
||||
|
||||
c := NewCachedClient(mockFS, client, time.Hour)
|
||||
c := NewCachedClient(mockFS, client, time.Hour, time.Hour*2)
|
||||
|
||||
// Populate cache
|
||||
_, err = c.ReadDir("/dir_reval")
|
||||
@@ -461,4 +461,3 @@ func TestCachedClient_RevalidateReadDir(t *testing.T) {
|
||||
t.Errorf("expected 1 hit, got %d", c.hits.Load())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,10 @@ package templer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -25,12 +27,38 @@ func Middleware(next http.Handler) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// Path returns the current path without a leading slash
|
||||
// Path returns the current path with a leading slash
|
||||
func Path(ctx context.Context) string {
|
||||
path, ok := ctx.Value(pathKey).(string)
|
||||
if !ok {
|
||||
slog.Error("could not extract request path from context")
|
||||
return ""
|
||||
}
|
||||
return strings.TrimPrefix(path, "/")
|
||||
return path
|
||||
}
|
||||
|
||||
func RelativePath(ctx context.Context, relPath string) (path string, err error) {
|
||||
baseStr := Path(ctx)
|
||||
|
||||
if baseStr != "" && !strings.HasSuffix(baseStr, "/") {
|
||||
baseStr += "/"
|
||||
}
|
||||
|
||||
base, err := url.Parse(baseStr)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("could not parse base path: %w", err)
|
||||
return
|
||||
}
|
||||
rel, err := url.Parse(relPath)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("could not parse relative path: %w", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Resolve the relative path against the base
|
||||
path = base.ResolveReference(rel).Path
|
||||
if strings.HasSuffix(path, "/") {
|
||||
path = path[:len(path)-1]
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package templer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRelativePath(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string // description of this test case
|
||||
// Named input parameters for target function.
|
||||
base string
|
||||
relPath string
|
||||
want string
|
||||
wantErr bool
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
{
|
||||
name: "with no relative path stays the same",
|
||||
base: "/foo/bar",
|
||||
relPath: "",
|
||||
want: "/foo/bar",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "with . relative path stays the same",
|
||||
base: "/foo/bar",
|
||||
relPath: ".",
|
||||
want: "/foo/bar",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "with ./ relative path stays the same",
|
||||
base: "/foo/bar",
|
||||
relPath: "./",
|
||||
want: "/foo/bar",
|
||||
wantErr: false,
|
||||
},
|
||||
|
||||
{
|
||||
name: "with .. relative path goes one up",
|
||||
base: "/foo/bar",
|
||||
relPath: "..",
|
||||
want: "/foo",
|
||||
wantErr: false,
|
||||
},
|
||||
|
||||
{
|
||||
name: "with ../baz relative path replaces last element with baz",
|
||||
base: "/foo/bar",
|
||||
relPath: "../baz",
|
||||
want: "/foo/baz",
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ctx := context.WithValue(t.Context(), pathKey, tt.base)
|
||||
got, gotErr := RelativePath(ctx, tt.relPath)
|
||||
if gotErr != nil {
|
||||
if !tt.wantErr {
|
||||
t.Errorf("RelativePath() failed: %v", gotErr)
|
||||
}
|
||||
return
|
||||
}
|
||||
if tt.wantErr {
|
||||
t.Fatal("RelativePath() succeeded unexpectedly")
|
||||
}
|
||||
// TODO: update the condition below to compare got with tt.want.
|
||||
if got != tt.want {
|
||||
t.Errorf("RelativePath() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user