integration of alias redirect in other handlers. #2
This commit is contained in:
parent
3bf4ec4c94
commit
7a857e857f
|
@ -76,3 +76,124 @@ func TestNoRedirectOnNonExistingAliases(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNoRedirectIfValidPostUrl(t *testing.T) {
|
||||||
|
repo := getTestRepo()
|
||||||
|
user, _ := repo.CreateUser("test-1")
|
||||||
|
post, _ := user.CreateNewPost("post-1")
|
||||||
|
post2, _ := user.CreateNewPost("post-2")
|
||||||
|
|
||||||
|
content := "---\n"
|
||||||
|
content += "title: Test\n"
|
||||||
|
content += "aliases: \n"
|
||||||
|
content += " - " + post2.UrlPath() + "\n"
|
||||||
|
content += "---\n"
|
||||||
|
content += "This is a test"
|
||||||
|
os.WriteFile(post.ContentFile(), []byte(content), 0644)
|
||||||
|
|
||||||
|
// Create Request and Response
|
||||||
|
req, err := http.NewRequest("GET", post2.UrlPath(), nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
router := main.Router(&repo)
|
||||||
|
router.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
// Check the status code is what we expect.
|
||||||
|
if status := rr.Code; status != http.StatusOK {
|
||||||
|
t.Errorf("handler returned wrong status code: got %v want %v",
|
||||||
|
status, http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRedirectIfInvalidPostUrl(t *testing.T) {
|
||||||
|
repo := getTestRepo()
|
||||||
|
user, _ := repo.CreateUser("test-1")
|
||||||
|
post, _ := user.CreateNewPost("post-1")
|
||||||
|
|
||||||
|
content := "---\n"
|
||||||
|
content += "title: Test\n"
|
||||||
|
content += "aliases: \n"
|
||||||
|
content += " - " + user.UrlPath() + "posts/not-a-real-post/" + "\n"
|
||||||
|
content += "---\n"
|
||||||
|
content += "This is a test"
|
||||||
|
os.WriteFile(post.ContentFile(), []byte(content), 0644)
|
||||||
|
|
||||||
|
// Create Request and Response
|
||||||
|
req, err := http.NewRequest("GET", user.UrlPath()+"posts/not-a-real-post/", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
router := main.Router(&repo)
|
||||||
|
router.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
// Check the status code is what we expect.
|
||||||
|
if status := rr.Code; status != http.StatusMovedPermanently {
|
||||||
|
t.Errorf("handler returned wrong status code: got %v want %v",
|
||||||
|
status, http.StatusMovedPermanently)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRedirectIfInvalidUserUrl(t *testing.T) {
|
||||||
|
repo := getTestRepo()
|
||||||
|
user, _ := repo.CreateUser("test-1")
|
||||||
|
post, _ := user.CreateNewPost("post-1")
|
||||||
|
|
||||||
|
content := "---\n"
|
||||||
|
content += "title: Test\n"
|
||||||
|
content += "aliases: \n"
|
||||||
|
content += " - /user/not-real/ \n"
|
||||||
|
content += "---\n"
|
||||||
|
content += "This is a test"
|
||||||
|
os.WriteFile(post.ContentFile(), []byte(content), 0644)
|
||||||
|
|
||||||
|
// Create Request and Response
|
||||||
|
req, err := http.NewRequest("GET", "/user/not-real/", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
router := main.Router(&repo)
|
||||||
|
router.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
// Check the status code is what we expect.
|
||||||
|
if status := rr.Code; status != http.StatusMovedPermanently {
|
||||||
|
t.Errorf("handler returned wrong status code: got %v want %v",
|
||||||
|
status, http.StatusMovedPermanently)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRedirectIfInvalidMediaUrl(t *testing.T) {
|
||||||
|
repo := getTestRepo()
|
||||||
|
user, _ := repo.CreateUser("test-1")
|
||||||
|
post, _ := user.CreateNewPost("post-1")
|
||||||
|
|
||||||
|
content := "---\n"
|
||||||
|
content += "title: Test\n"
|
||||||
|
content += "aliases: \n"
|
||||||
|
content += " - " + post.UrlMediaPath("not-real") + "\n"
|
||||||
|
content += "---\n"
|
||||||
|
content += "This is a test"
|
||||||
|
os.WriteFile(post.ContentFile(), []byte(content), 0644)
|
||||||
|
|
||||||
|
// Create Request and Response
|
||||||
|
req, err := http.NewRequest("GET", post.UrlMediaPath("not-real"), nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
router := main.Router(&repo)
|
||||||
|
router.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
// Check the status code is what we expect.
|
||||||
|
if status := rr.Code; status != http.StatusMovedPermanently {
|
||||||
|
t.Errorf("handler returned wrong status code: got %v want %v",
|
||||||
|
status, http.StatusMovedPermanently)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -41,8 +41,7 @@ func userIndexHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Requ
|
||||||
user, err := getUserFromRepo(repo, ps)
|
user, err := getUserFromRepo(repo, ps)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
println("Error getting user: ", err.Error())
|
println("Error getting user: ", err.Error())
|
||||||
w.WriteHeader(http.StatusNotFound)
|
notFoundHandler(repo)(w, r)
|
||||||
w.Write([]byte("User not found"))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
html, err := owl.RenderIndexPage(user)
|
html, err := owl.RenderIndexPage(user)
|
||||||
|
@ -64,15 +63,13 @@ func postHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Request,
|
||||||
user, err := getUserFromRepo(repo, ps)
|
user, err := getUserFromRepo(repo, ps)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
println("Error getting user: ", err.Error())
|
println("Error getting user: ", err.Error())
|
||||||
w.WriteHeader(http.StatusNotFound)
|
notFoundHandler(repo)(w, r)
|
||||||
w.Write([]byte("User not found"))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
post, err := user.GetPost(postId)
|
post, err := user.GetPost(postId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
println("Error getting post: ", err.Error())
|
println("Error getting post: ", err.Error())
|
||||||
w.WriteHeader(http.StatusNotFound)
|
notFoundHandler(repo)(w, r)
|
||||||
w.Write([]byte("Post not found"))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
html, err := owl.RenderPost(post)
|
html, err := owl.RenderPost(post)
|
||||||
|
@ -96,22 +93,19 @@ func postMediaHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Requ
|
||||||
user, err := getUserFromRepo(repo, ps)
|
user, err := getUserFromRepo(repo, ps)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
println("Error getting user: ", err.Error())
|
println("Error getting user: ", err.Error())
|
||||||
w.WriteHeader(http.StatusNotFound)
|
notFoundHandler(repo)(w, r)
|
||||||
w.Write([]byte("User not found"))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
post, err := user.GetPost(postId)
|
post, err := user.GetPost(postId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
println("Error getting post: ", err.Error())
|
println("Error getting post: ", err.Error())
|
||||||
w.WriteHeader(http.StatusNotFound)
|
notFoundHandler(repo)(w, r)
|
||||||
w.Write([]byte("Post not found"))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
filepath = path.Join(post.MediaDir(), filepath)
|
filepath = path.Join(post.MediaDir(), filepath)
|
||||||
if _, err := os.Stat(filepath); err != nil {
|
if _, err := os.Stat(filepath); err != nil {
|
||||||
println("Error getting file: ", err.Error())
|
println("Error getting file: ", err.Error())
|
||||||
w.WriteHeader(http.StatusNotFound)
|
notFoundHandler(repo)(w, r)
|
||||||
w.Write([]byte("File not found"))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
http.ServeFile(w, r, filepath)
|
http.ServeFile(w, r, filepath)
|
||||||
|
|
5
user.go
5
user.go
|
@ -60,6 +60,11 @@ func (user User) Posts() ([]string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (user User) GetPost(id string) (Post, error) {
|
func (user User) GetPost(id string) (Post, error) {
|
||||||
|
// check if posts index.md exists
|
||||||
|
if !fileExists(path.Join(user.Dir(), "public", id, "index.md")) {
|
||||||
|
return Post{}, fmt.Errorf("post %s does not exist", id)
|
||||||
|
}
|
||||||
|
|
||||||
post := Post{user: &user, id: id}
|
post := Post{user: &user, id: id}
|
||||||
_, metaData := post.MarkdownData()
|
_, metaData := post.MarkdownData()
|
||||||
title := metaData["title"]
|
title := metaData["title"]
|
||||||
|
|
Loading…
Reference in New Issue