Extract Files seam, WebDAV, and Visit from CheckDNS.
Publish go-nc-files v1 with local/WebDAV Root, DefaultPathStore, and neutral Working Folder language; depends on go-nc-exapp v0.1.0. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+119
@@ -0,0 +1,119 @@
|
||||
package goncfiles_test
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
gonexapp "gitea.neitzel.de/konrad/go-nc-exapp"
|
||||
"gitea.neitzel.de/konrad/go-nc-files"
|
||||
)
|
||||
|
||||
func TestWebDAVReadWriteListExists(t *testing.T) {
|
||||
store := map[string][]byte{}
|
||||
dirs := map[string]bool{"/remote.php/dav/files/alice/myapp": true}
|
||||
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Header.Get("AUTHORIZATION-APP-API") == "" {
|
||||
http.Error(w, "no auth", 401)
|
||||
return
|
||||
}
|
||||
path := r.URL.Path
|
||||
switch r.Method {
|
||||
case "PROPFIND":
|
||||
if !dirs[strings.TrimSuffix(path, "/")] && store[path] == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusMultiStatus)
|
||||
var b strings.Builder
|
||||
b.WriteString(`<?xml version="1.0"?><d:multistatus xmlns:d="DAV:">`)
|
||||
b.WriteString(`<d:response><d:href>` + path + `</d:href><d:propstat><d:prop><d:resourcetype><d:collection/></d:resourcetype></d:prop></d:propstat></d:response>`)
|
||||
prefix := strings.TrimSuffix(path, "/")
|
||||
for p := range store {
|
||||
if strings.HasPrefix(p, prefix+"/") && !strings.Contains(strings.TrimPrefix(p, prefix+"/"), "/") {
|
||||
b.WriteString(`<d:response><d:href>` + p + `</d:href><d:propstat><d:prop><d:resourcetype/></d:prop></d:propstat></d:response>`)
|
||||
}
|
||||
}
|
||||
b.WriteString(`</d:multistatus>`)
|
||||
_, _ = w.Write([]byte(b.String()))
|
||||
case http.MethodPut:
|
||||
data, _ := io.ReadAll(r.Body)
|
||||
store[path] = data
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
case http.MethodGet:
|
||||
data, ok := store[path]
|
||||
if !ok {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
_, _ = w.Write(data)
|
||||
case http.MethodHead:
|
||||
if _, ok := store[path]; !ok {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
case "MKCOL":
|
||||
dirs[strings.TrimSuffix(path, "/")] = true
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
default:
|
||||
http.Error(w, "method", 405)
|
||||
}
|
||||
}))
|
||||
t.Cleanup(srv.Close)
|
||||
|
||||
cred := gonexapp.Credentials{
|
||||
BaseURL: srv.URL,
|
||||
AppID: "myexapp",
|
||||
AppVersion: "0.1.0",
|
||||
AAVersion: "1.0.0",
|
||||
AppSecret: "secret",
|
||||
UserID: "alice",
|
||||
}
|
||||
root := goncfiles.WebDAVRoot{Cred: cred, Client: srv.Client()}
|
||||
folder, err := root.EnsureFolder("myapp")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := folder.Write("note.txt", []byte("hello")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ok, err := folder.Exists("note.txt")
|
||||
if err != nil || !ok {
|
||||
t.Fatalf("exists: ok=%v err=%v", ok, err)
|
||||
}
|
||||
data, err := folder.Read("note.txt")
|
||||
if err != nil || string(data) != "hello" {
|
||||
t.Fatalf("read: %q err=%v", data, err)
|
||||
}
|
||||
entries, err := folder.List()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
found := false
|
||||
for _, e := range entries {
|
||||
if e.Name == "note.txt" {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("list: %+v", entries)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWebDAVOpenMissingIsNotExist(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
http.NotFound(w, r)
|
||||
}))
|
||||
t.Cleanup(srv.Close)
|
||||
root := goncfiles.WebDAVRoot{Cred: gonexapp.Credentials{
|
||||
BaseURL: srv.URL, AppID: "myexapp", AppVersion: "0.1.0", AAVersion: "1.0.0", AppSecret: "s", UserID: "alice",
|
||||
}, Client: srv.Client()}
|
||||
_, err := root.OpenFolder("nope")
|
||||
if err == nil || !goncfiles.IsNotExist(err) {
|
||||
t.Fatalf("got %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user