Fix pathing with relative non-single-depth root
parent
7964518d36
commit
e6f63a578f
|
|
@ -29,7 +29,7 @@ func Refresh() {
|
||||||
|
|
||||||
as := args.NewArgSet()
|
as := args.NewArgSet()
|
||||||
as.Append(args.STRING, "root", "root dir path", "./public")
|
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, "wrap", "file with http header/footer", "./wrapper.html")
|
||||||
as.Append(args.STRING, "oauth", "oauth URL", "")
|
as.Append(args.STRING, "oauth", "oauth URL", "")
|
||||||
if err := as.Parse(); err != nil {
|
if err := as.Parse(); err != nil {
|
||||||
|
|
|
||||||
|
|
@ -16,16 +16,10 @@ type Path struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPathFromLocal(p string) Path {
|
func NewPathFromLocal(p string) Path {
|
||||||
if !strings.HasPrefix(p, "/") {
|
splits := strings.SplitN(p, path.Base(config.Root)+"/", 2)
|
||||||
cwd, _ := os.Getwd()
|
|
||||||
p = path.Clean(path.Join(cwd, p))
|
|
||||||
}
|
|
||||||
splits := strings.SplitN(p, path.Base(config.Root), 2)
|
|
||||||
href := splits[0]
|
href := splits[0]
|
||||||
if len(splits) > 1 && (splits[0] == "" || splits[0] == "/") {
|
if len(splits) > 1 && (splits[0] == "" || splits[0] == "/") {
|
||||||
href = splits[1]
|
href = splits[1]
|
||||||
} else {
|
|
||||||
href = strings.Join(splits, path.Base(config.Root))
|
|
||||||
}
|
}
|
||||||
href = path.Join("/notes", href)
|
href = path.Join("/notes", href)
|
||||||
return NewPathFromURL(href)
|
return NewPathFromURL(href)
|
||||||
|
|
|
||||||
114
main_test.go
114
main_test.go
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"local/notes-server/config"
|
"local/notes-server/config"
|
||||||
"local/notes-server/server"
|
"local/notes-server/server"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
|
|
@ -29,7 +28,7 @@ func makeFiles(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
config.Root = d
|
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 {
|
if err := os.MkdirAll(path.Join(d, dir), os.ModePerm); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
@ -62,7 +61,6 @@ func testServer(t *testing.T, url string) {
|
||||||
testFile(t, url)
|
testFile(t, url)
|
||||||
testNavRootDir(t, url)
|
testNavRootDir(t, url)
|
||||||
testNavRootFile(t, url)
|
testNavRootFile(t, url)
|
||||||
testNavDirDir(t, url)
|
|
||||||
testNavDirFile(t, url)
|
testNavDirFile(t, url)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -78,18 +76,17 @@ func testCreate(t *testing.T, url string) {
|
||||||
}
|
}
|
||||||
b, _ := ioutil.ReadAll(resp.Body)
|
b, _ := ioutil.ReadAll(resp.Body)
|
||||||
s := string(b)
|
s := string(b)
|
||||||
log.Println(string(s))
|
|
||||||
if ok := assertHasMultilink(s, path); !ok {
|
if ok := assertHasMultilink(s, path); !ok {
|
||||||
t.Error(err)
|
t.Error(ok)
|
||||||
}
|
}
|
||||||
if ok := assertHasForm(s, "/submit/"+path); !ok {
|
if ok := assertHasForm(s, "/submit/"+path); !ok {
|
||||||
t.Error(err)
|
t.Error(ok)
|
||||||
}
|
}
|
||||||
if ok := assertHasTextArea(s); !ok {
|
if ok := assertHasTextArea(s); !ok {
|
||||||
t.Error(err)
|
t.Error(ok)
|
||||||
}
|
}
|
||||||
if ok := assertHasSubmit(s); !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)
|
b, _ := ioutil.ReadAll(resp.Body)
|
||||||
s := string(b)
|
s := string(b)
|
||||||
if ok := assertHasMultilink(s, path); !ok {
|
if ok := assertHasMultilink(s, path); !ok {
|
||||||
t.Error(err)
|
t.Error(ok)
|
||||||
}
|
}
|
||||||
if ok := assertHasForm(s); !ok {
|
if ok := assertHasForm(s); !ok {
|
||||||
t.Error(err)
|
t.Error(ok)
|
||||||
}
|
}
|
||||||
if ok := assertHasTextArea(s); !ok {
|
if ok := assertHasTextArea(s); !ok {
|
||||||
t.Error(err)
|
t.Error(ok)
|
||||||
}
|
}
|
||||||
if ok := assertHasSubmit(s); !ok {
|
if ok := assertHasSubmit(s); !ok {
|
||||||
t.Error(err)
|
t.Error(ok)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testDir(t *testing.T, url string) {
|
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) {
|
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) {
|
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) {
|
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) {
|
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 {
|
func assertHasMultilink(body string, segments ...string) bool {
|
||||||
if !strings.Contains(body, `/<a href="/notes">notes</a>/`) {
|
if !strings.Contains(body, `/<a href="/notes">notes</a>/`) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
for _, segment := range segments {
|
for i := range segments {
|
||||||
regexp := regexp.MustCompile(`<a href="[^"]*` + segment + `">`)
|
segments[i] = "/notes/" + strings.TrimPrefix(segments[i], "/notes/")
|
||||||
if !regexp.MatchString(body) {
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
}
|
return assertHasHref(body, segments...)
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func assertHasForm(body string, action ...string) bool {
|
func assertHasForm(body string, action ...string) bool {
|
||||||
|
|
@ -169,3 +218,16 @@ func assertHasTextArea(body string) bool {
|
||||||
func assertHasSubmit(body string) bool {
|
func assertHasSubmit(body string) bool {
|
||||||
return strings.Contains(body, `<button`) && strings.Contains(body, `type="submit"`)
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,10 @@ func (s *Server) Routes() error {
|
||||||
path string
|
path string
|
||||||
handler http.HandlerFunc
|
handler http.HandlerFunc
|
||||||
}{
|
}{
|
||||||
|
{
|
||||||
|
path: "/",
|
||||||
|
handler: s.root,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
path: fmt.Sprintf("notes/%s%s", wildcard, wildcard),
|
path: fmt.Sprintf("notes/%s%s", wildcard, wildcard),
|
||||||
handler: s.authenticate(s.notes),
|
handler: s.authenticate(s.notes),
|
||||||
|
|
@ -37,3 +41,8 @@ func (s *Server) Routes() error {
|
||||||
}
|
}
|
||||||
return nil
|
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)
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue