2022-09-05 18:34:24 +00:00
|
|
|
package web_test
|
2022-08-20 20:46:52 +00:00
|
|
|
|
|
|
|
import (
|
2022-09-05 18:34:24 +00:00
|
|
|
main "h4kor/owl-blogs/cmd/owl/web"
|
2022-08-20 20:46:52 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPostHandlerReturns404OnDrafts(t *testing.T) {
|
|
|
|
repo := getTestRepo()
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|