1 Commits
v0.2 ... v0.3

Author SHA1 Message Date
bel
0d497f4fa8 Fix nonroot path 2019-11-06 17:44:04 -07:00
4 changed files with 56 additions and 4 deletions

View File

@@ -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))

View File

@@ -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)
}
}
}

View File

@@ -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

View File

@@ -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{