Fix pathing with relative non-single-depth root

master
Bel LaPointe 2019-11-14 11:14:57 -07:00
parent 7964518d36
commit e6f63a578f
4 changed files with 100 additions and 35 deletions

View File

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

View File

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

View File

@ -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")
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 testNavDirDir(t *testing.T, url string) {
t.Error("not impl")
}
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, `/<a href="/notes">notes</a>/`) {
return false
}
for _, segment := range segments {
regexp := regexp.MustCompile(`<a href="[^"]*` + segment + `">`)
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, `<button`) && strings.Contains(body, `type="submit"`)
}
func assertHasHref(body string, segments ...string) bool {
if !strings.Contains(body, `href="`) {
return false
}
for _, segment := range segments {
re := regexp.MustCompile(`a[^>]*href="` + segment + `"`)
if !re.MatchString(body) {
return false
}
}
return true
}

View File

@ -12,6 +12,10 @@ func (s *Server) Routes() error {
path string
handler http.HandlerFunc
}{
{
path: "/",
handler: s.root,
},
{
path: fmt.Sprintf("notes/%s%s", wildcard, wildcard),
handler: s.authenticate(s.notes),
@ -37,3 +41,8 @@ func (s *Server) Routes() error {
}
return nil
}
func (s *Server) root(w http.ResponseWriter, r *http.Request) {
r.URL.Path = "/notes"
http.Redirect(w, r, r.URL.String(), http.StatusPermanentRedirect)
}