From e6f63a578f52770a4a3364162128c226fd959b1c Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Thu, 14 Nov 2019 11:14:57 -0700 Subject: [PATCH] Fix pathing with relative non-single-depth root --- config/config.go | 2 +- filetree/path.go | 8 +--- main_test.go | 116 ++++++++++++++++++++++++++++++++++++----------- server/routes.go | 9 ++++ 4 files changed, 100 insertions(+), 35 deletions(-) diff --git a/config/config.go b/config/config.go index 005e4d6..6edcd0c 100755 --- a/config/config.go +++ b/config/config.go @@ -29,7 +29,7 @@ func Refresh() { as := args.NewArgSet() as.Append(args.STRING, "root", "root dir path", "./public") - as.Append(args.STRING, "port", "port to listen on", "39909") + as.Append(args.STRING, "port", "port to listen on", "49909") as.Append(args.STRING, "wrap", "file with http header/footer", "./wrapper.html") as.Append(args.STRING, "oauth", "oauth URL", "") if err := as.Parse(); err != nil { diff --git a/filetree/path.go b/filetree/path.go index 44292d3..8621fa1 100755 --- a/filetree/path.go +++ b/filetree/path.go @@ -16,16 +16,10 @@ type Path struct { } func NewPathFromLocal(p string) Path { - if !strings.HasPrefix(p, "/") { - cwd, _ := os.Getwd() - p = path.Clean(path.Join(cwd, p)) - } - splits := strings.SplitN(p, path.Base(config.Root), 2) + splits := strings.SplitN(p, path.Base(config.Root)+"/", 2) href := splits[0] if len(splits) > 1 && (splits[0] == "" || splits[0] == "/") { href = splits[1] - } else { - href = strings.Join(splits, path.Base(config.Root)) } href = path.Join("/notes", href) return NewPathFromURL(href) diff --git a/main_test.go b/main_test.go index 83f6b72..c2433ff 100644 --- a/main_test.go +++ b/main_test.go @@ -5,7 +5,6 @@ import ( "io/ioutil" "local/notes-server/config" "local/notes-server/server" - "log" "net/http" "net/http/httptest" "os" @@ -29,7 +28,7 @@ func makeFiles(t *testing.T) { t.Fatal(err) } config.Root = d - for _, dir := range []string{"dirA", "dirB"} { + for _, dir := range []string{"dirA", "dirB", "."} { if err := os.MkdirAll(path.Join(d, dir), os.ModePerm); err != nil { t.Fatal(err) } @@ -62,7 +61,6 @@ func testServer(t *testing.T, url string) { testFile(t, url) testNavRootDir(t, url) testNavRootFile(t, url) - testNavDirDir(t, url) testNavDirFile(t, url) } @@ -78,18 +76,17 @@ func testCreate(t *testing.T, url string) { } b, _ := ioutil.ReadAll(resp.Body) s := string(b) - log.Println(string(s)) if ok := assertHasMultilink(s, path); !ok { - t.Error(err) + t.Error(ok) } if ok := assertHasForm(s, "/submit/"+path); !ok { - t.Error(err) + t.Error(ok) } if ok := assertHasTextArea(s); !ok { - t.Error(err) + t.Error(ok) } if ok := assertHasSubmit(s); !ok { - t.Error(err) + t.Error(ok) } } } @@ -107,55 +104,107 @@ func testEdit(t *testing.T, url string) { b, _ := ioutil.ReadAll(resp.Body) s := string(b) if ok := assertHasMultilink(s, path); !ok { - t.Error(err) + t.Error(ok) } if ok := assertHasForm(s); !ok { - t.Error(err) + t.Error(ok) } if ok := assertHasTextArea(s); !ok { - t.Error(err) + t.Error(ok) } if ok := assertHasSubmit(s); !ok { - t.Error(err) + t.Error(ok) } } } func testDir(t *testing.T, url string) { - t.Error("not impl") + path := url + "/notes/dirA" + resp, err := http.Get(path) + if err != nil { + t.Fatal(err) + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + t.Fatal(resp.StatusCode) + } + b, _ := ioutil.ReadAll(resp.Body) + s := string(b) + if ok := assertHasMultilink(s, "/notes/dirA"); !ok { + t.Error(ok) + } + if ok := assertHasForm(s, "/create/dirA"); !ok { + t.Error(ok) + } + if ok := assertHasSubmit(s); !ok { + t.Error(ok) + } } func testFile(t *testing.T, url string) { - t.Error("not impl") + path := url + "/notes/dirA/fileA" + resp, err := http.Get(path) + if err != nil { + t.Fatal(err) + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + t.Fatal(resp.StatusCode) + } + b, _ := ioutil.ReadAll(resp.Body) + s := string(b) + if ok := assertHasMultilink(s, "/notes/dirA", "/notes/dirA/fileA"); !ok { + t.Error(ok) + } + if ok := assertHasHref(s, "/edit/dirA/fileA"); !ok { + t.Error(ok) + } } func testNavRootDir(t *testing.T, url string) { - t.Error("not impl") + resp, err := http.Get(url + "/notes") + if err != nil { + t.Fatal(err) + } + b, _ := ioutil.ReadAll(resp.Body) + s := string(b) + if ok := assertHasHref(s, "/notes/dirA"); !ok { + t.Fatal(s) + } } func testNavRootFile(t *testing.T, url string) { - t.Error("not impl") -} - -func testNavDirDir(t *testing.T, url string) { - t.Error("not impl") + resp, err := http.Get(url + "/notes") + if err != nil { + t.Fatal(err) + } + b, _ := ioutil.ReadAll(resp.Body) + s := string(b) + if ok := assertHasHref(s, "/notes/fileA"); !ok { + t.Fatal(s) + } } func testNavDirFile(t *testing.T, url string) { - t.Error("not impl") + resp, err := http.Get(url + "/notes/dirA") + if err != nil { + t.Fatal(err) + } + b, _ := ioutil.ReadAll(resp.Body) + s := string(b) + if ok := assertHasHref(s, "/notes/dirA/fileA"); !ok { + t.Fatal(s) + } } func assertHasMultilink(body string, segments ...string) bool { if !strings.Contains(body, `/notes/`) { return false } - for _, segment := range segments { - regexp := regexp.MustCompile(``) - if !regexp.MatchString(body) { - return false - } + for i := range segments { + segments[i] = "/notes/" + strings.TrimPrefix(segments[i], "/notes/") } - return true + return assertHasHref(body, segments...) } func assertHasForm(body string, action ...string) bool { @@ -169,3 +218,16 @@ func assertHasTextArea(body string) bool { func assertHasSubmit(body string) bool { return strings.Contains(body, `