diff --git a/server/path.go b/server/path.go index b09dcaa..34ccdca 100755 --- a/server/path.go +++ b/server/path.go @@ -16,9 +16,13 @@ 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) href := splits[0] - if len(splits) > 1 && splits[0] == "" { + if len(splits) > 1 && (splits[0] == "" || splits[0] == "/") { href = splits[1] } else { href = strings.Join(splits, path.Base(config.Root)) diff --git a/server/path_test.go b/server/path_test.go index 77dcb32..4b4994b 100755 --- a/server/path_test.go +++ b/server/path_test.go @@ -1,6 +1,9 @@ package server -import "testing" +import ( + "local/notes-server/config" + "testing" +) func TestPathIs(t *testing.T) { p := Path{Local: "/dev/null"} @@ -21,3 +24,48 @@ func TestPathIs(t *testing.T) { t.Fatal(ok, p) } } + +func TestNewPathFromLocal(t *testing.T) { + cases := []struct { + in string + root string + href string + local string + }{ + { + in: "/wiki/b/a.md", + root: "/wiki", + href: "/notes/b/a.md", + local: "/wiki/b/a.md", + }, + { + in: "/wiki/a.md", + root: "/wiki", + href: "/notes/a.md", + local: "/wiki/a.md", + }, + { + in: "/b/a.md", + root: "/", + href: "/notes/b/a.md", + local: "/b/a.md", + }, + { + in: "/a.md", + root: "/", + href: "/notes/a.md", + local: "/a.md", + }, + } + + for i, c := range cases { + config.Root = c.root + p := NewPathFromLocal(c.in) + if p.HREF != c.href { + t.Fatal(i, "href", p.HREF, c.href, c, p) + } + if p.Local != c.local { + t.Fatal(i, "local", p.Local, c.local, c, p) + } + } +} diff --git a/server/routes.go b/server/routes.go index ae4c11b..8b853ee 100755 --- a/server/routes.go +++ b/server/routes.go @@ -44,7 +44,7 @@ func (s *Server) Routes() error { func (s *Server) authenticate(foo http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if config.OAuthServer != "" { - err := oauth2client.Authenticate(config.OAuthServer, w, r) + err := oauth2client.Authenticate(config.OAuthServer, "notes-server", w, r) if err != nil { log.Println(err) return diff --git a/server/routes_test.go b/server/routes_test.go index cd97f06..774bff8 100755 --- a/server/routes_test.go +++ b/server/routes_test.go @@ -37,7 +37,7 @@ func TestServerNotes(t *testing.T) { t.Logf("serve %s: %s", r.URL.Path, w.Body.Bytes()) } -func TestServerNotes(t *testing.T) { +func TestServerNotesB(t *testing.T) { s := New() w := httptest.NewRecorder() r := &http.Request{