simple test for rss feed

This commit is contained in:
Niko Abeler 2022-08-16 20:36:05 +02:00
parent 0b38ba839d
commit 40a8989aa6
2 changed files with 45 additions and 2 deletions

View File

@ -64,7 +64,7 @@ func userRSSHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Reques
notFoundHandler(repo)(w, r)
return
}
html, err := owl.RenderRSSFeed(user)
xml, err := owl.RenderRSSFeed(user)
if err != nil {
println("Error rendering index page: ", err.Error())
w.WriteHeader(http.StatusInternalServerError)
@ -72,7 +72,8 @@ func userRSSHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Reques
return
}
println("Rendering index page for user", user.Name())
w.Write([]byte(html))
w.Header().Set("Content-Type", "application/rss+xml")
w.Write([]byte(xml))
}
}

42
cmd/owl-web/rss_test.go Normal file
View File

@ -0,0 +1,42 @@
package main_test
import (
main "h4kor/owl-blogs/cmd/owl-web"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestMultiUserUserRssIndexHandler(t *testing.T) {
repo := getTestRepo()
user, _ := repo.CreateUser("test-1")
user.CreateNewPost("post-1")
// Create Request and Response
req, err := http.NewRequest("GET", user.UrlPath()+"index.xml", 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)
}
// Check the response Content-Type is what we expect.
if !strings.Contains(rr.Header().Get("Content-Type"), "application/rss+xml") {
t.Errorf("handler returned wrong Content-Type: got %v want %v",
rr.Header().Get("Content-Type"), "application/rss+xml")
}
// Check the response body contains names of users
if !strings.Contains(rr.Body.String(), "post-1") {
t.Error("post-1 not listed on index page. Got: ")
t.Error(rr.Body.String())
}
}