54 lines
1.4 KiB
Go
54 lines
1.4 KiB
Go
package upstream
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestPrepareCacheIsPinnedAndReusableOffline(t *testing.T) {
|
|
s, err := LoadPinned(filepath.Join("..", ".."))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
root := t.TempDir()
|
|
b, _ := json.Marshal(s.Lock)
|
|
if err = os.WriteFile(filepath.Join(root, "gitea.lock.json"), b, 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
prefix := "/" + s.Lock.Commit + "/"
|
|
if !strings.HasPrefix(r.URL.Path, prefix) {
|
|
t.Errorf("unpinned request %s", r.URL.Path)
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
data, ok := s.Files[strings.TrimPrefix(r.URL.Path, prefix)]
|
|
if !ok {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.Write(data)
|
|
}))
|
|
if _, err := prepareFrom(context.Background(), root, server.URL+"/"); err != nil {
|
|
server.Close()
|
|
t.Fatal(err)
|
|
}
|
|
server.Close()
|
|
if _, err := prepareFrom(context.Background(), root, server.URL+"/"); err != nil {
|
|
t.Fatalf("offline cache: %v", err)
|
|
}
|
|
path := filepath.Join(CacheDir(root), "templates", "mail", "base", "head.tmpl")
|
|
if err = os.WriteFile(path, []byte("corrupt"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := prepareFrom(context.Background(), root, server.URL+"/"); err == nil {
|
|
t.Fatal("corrupt cache was accepted")
|
|
}
|
|
}
|