owl-blogs/cmd/owl/web/post_test.go

40 lines
932 B
Go
Raw Permalink Normal View History

2022-09-05 18:34:24 +00:00
package web_test
import (
"h4kor/owl-blogs"
2022-09-05 18:34:24 +00:00
main "h4kor/owl-blogs/cmd/owl/web"
"net/http"
"net/http/httptest"
"os"
"testing"
)
func TestPostHandlerReturns404OnDrafts(t *testing.T) {
repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser("test-1")
post, _ := user.CreateNewPost("post-1")
content := "---\n"
content += "title: test\n"
content += "draft: true\n"
content += "---\n"
content += "\n"
content += "Write your post here.\n"
os.WriteFile(post.ContentFile(), []byte(content), 0644)
// Create Request and Response
req, err := http.NewRequest("GET", post.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.StatusNotFound {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusNotFound)
}
}