WIP clean up tests. Moving checks into assertions functions

This commit is contained in:
Niko Abeler 2022-11-02 22:02:49 +01:00
parent 73def1b477
commit c92ab958a6
13 changed files with 248 additions and 625 deletions

View File

@ -3,6 +3,7 @@ package web_test
import ( import (
"h4kor/owl-blogs" "h4kor/owl-blogs"
main "h4kor/owl-blogs/cmd/owl/web" main "h4kor/owl-blogs/cmd/owl/web"
"h4kor/owl-blogs/priv/assertions"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os" "os"
@ -25,26 +26,14 @@ func TestRedirectOnAliases(t *testing.T) {
// Create Request and Response // Create Request and Response
req, err := http.NewRequest("GET", "/foo/bar", nil) req, err := http.NewRequest("GET", "/foo/bar", nil)
if err != nil { assertions.AssertNoError(t, err, "Error creating request")
t.Fatal(err)
}
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
router := main.Router(&repo) router := main.Router(&repo)
router.ServeHTTP(rr, req) router.ServeHTTP(rr, req)
// Check the status code is what we expect. assertions.AssertStatus(t, rr, http.StatusMovedPermanently)
if status := rr.Code; status != http.StatusMovedPermanently {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusMovedPermanently)
}
// Check that Location header is set correctly // Check that Location header is set correctly
if rr.Header().Get("Location") != post.UrlPath() { assertions.AssertEqual(t, rr.Header().Get("Location"), post.UrlPath())
t.Errorf("Location header is not set correctly, expected: %v Got: %v",
post.UrlPath(),
rr.Header().Get("Location"),
)
}
} }
func TestNoRedirectOnNonExistingAliases(t *testing.T) { func TestNoRedirectOnNonExistingAliases(t *testing.T) {
@ -63,18 +52,12 @@ func TestNoRedirectOnNonExistingAliases(t *testing.T) {
// Create Request and Response // Create Request and Response
req, err := http.NewRequest("GET", "/foo/bar2", nil) req, err := http.NewRequest("GET", "/foo/bar2", nil)
if err != nil { assertions.AssertNoError(t, err, "Error creating request")
t.Fatal(err)
}
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
router := main.Router(&repo) router := main.Router(&repo)
router.ServeHTTP(rr, req) router.ServeHTTP(rr, req)
// Check the status code is what we expect. assertions.AssertStatus(t, rr, http.StatusNotFound)
if status := rr.Code; status != http.StatusNotFound {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusNotFound)
}
} }
@ -94,18 +77,12 @@ func TestNoRedirectIfValidPostUrl(t *testing.T) {
// Create Request and Response // Create Request and Response
req, err := http.NewRequest("GET", post2.UrlPath(), nil) req, err := http.NewRequest("GET", post2.UrlPath(), nil)
if err != nil { assertions.AssertNoError(t, err, "Error creating request")
t.Fatal(err)
}
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
router := main.Router(&repo) router := main.Router(&repo)
router.ServeHTTP(rr, req) router.ServeHTTP(rr, req)
// Check the status code is what we expect. assertions.AssertStatus(t, rr, http.StatusOK)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
} }
@ -124,18 +101,12 @@ func TestRedirectIfInvalidPostUrl(t *testing.T) {
// Create Request and Response // Create Request and Response
req, err := http.NewRequest("GET", user.UrlPath()+"posts/not-a-real-post/", nil) req, err := http.NewRequest("GET", user.UrlPath()+"posts/not-a-real-post/", nil)
if err != nil { assertions.AssertNoError(t, err, "Error creating request")
t.Fatal(err)
}
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
router := main.Router(&repo) router := main.Router(&repo)
router.ServeHTTP(rr, req) router.ServeHTTP(rr, req)
// Check the status code is what we expect. assertions.AssertStatus(t, rr, http.StatusMovedPermanently)
if status := rr.Code; status != http.StatusMovedPermanently {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusMovedPermanently)
}
} }
@ -154,18 +125,12 @@ func TestRedirectIfInvalidUserUrl(t *testing.T) {
// Create Request and Response // Create Request and Response
req, err := http.NewRequest("GET", "/user/not-real/", nil) req, err := http.NewRequest("GET", "/user/not-real/", nil)
if err != nil { assertions.AssertNoError(t, err, "Error creating request")
t.Fatal(err)
}
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
router := main.Router(&repo) router := main.Router(&repo)
router.ServeHTTP(rr, req) router.ServeHTTP(rr, req)
// Check the status code is what we expect. assertions.AssertStatus(t, rr, http.StatusMovedPermanently)
if status := rr.Code; status != http.StatusMovedPermanently {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusMovedPermanently)
}
} }
@ -184,18 +149,12 @@ func TestRedirectIfInvalidMediaUrl(t *testing.T) {
// Create Request and Response // Create Request and Response
req, err := http.NewRequest("GET", post.UrlMediaPath("not-real"), nil) req, err := http.NewRequest("GET", post.UrlMediaPath("not-real"), nil)
if err != nil { assertions.AssertNoError(t, err, "Error creating request")
t.Fatal(err)
}
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
router := main.Router(&repo) router := main.Router(&repo)
router.ServeHTTP(rr, req) router.ServeHTTP(rr, req)
// Check the status code is what we expect. assertions.AssertStatus(t, rr, http.StatusMovedPermanently)
if status := rr.Code; status != http.StatusMovedPermanently {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusMovedPermanently)
}
} }
@ -222,17 +181,11 @@ func TestDeepAliasInSingleUserMode(t *testing.T) {
// Create Request and Response // Create Request and Response
req, err := http.NewRequest("GET", "/2016/09/13/create-tileable-textures-with-gimp/", nil) req, err := http.NewRequest("GET", "/2016/09/13/create-tileable-textures-with-gimp/", nil)
if err != nil { assertions.AssertNoError(t, err, "Error creating request")
t.Fatal(err)
}
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
router := main.SingleUserRouter(&repo) router := main.SingleUserRouter(&repo)
router.ServeHTTP(rr, req) router.ServeHTTP(rr, req)
// Check the status code is what we expect. assertions.AssertStatus(t, rr, http.StatusMovedPermanently)
if status := rr.Code; status != http.StatusMovedPermanently {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusMovedPermanently)
}
} }

View File

@ -3,12 +3,12 @@ package web_test
import ( import (
"h4kor/owl-blogs" "h4kor/owl-blogs"
main "h4kor/owl-blogs/cmd/owl/web" main "h4kor/owl-blogs/cmd/owl/web"
"h4kor/owl-blogs/priv/assertions"
"math/rand" "math/rand"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os" "os"
"path" "path"
"strings"
"testing" "testing"
"time" "time"
) )
@ -39,28 +39,16 @@ func TestMultiUserRepoIndexHandler(t *testing.T) {
// Create Request and Response // Create Request and Response
req, err := http.NewRequest("GET", "/", nil) req, err := http.NewRequest("GET", "/", nil)
if err != nil { assertions.AssertNoError(t, err, "Error creating request")
t.Fatal(err)
}
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
router := main.Router(&repo) router := main.Router(&repo)
router.ServeHTTP(rr, req) router.ServeHTTP(rr, req)
// Check the status code is what we expect. assertions.AssertStatus(t, rr, http.StatusOK)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
// Check the response body contains names of users // Check the response body contains names of users
if !strings.Contains(rr.Body.String(), "user_1") { assertions.AssertContains(t, rr.Body.String(), "user_1")
t.Error("user_1 not listed on index page. Got: ") assertions.AssertContains(t, rr.Body.String(), "user_2")
t.Error(rr.Body.String())
}
if !strings.Contains(rr.Body.String(), "user_2") {
t.Error("user_2 not listed on index page. Got: ")
t.Error(rr.Body.String())
}
} }
func TestMultiUserUserIndexHandler(t *testing.T) { func TestMultiUserUserIndexHandler(t *testing.T) {
@ -70,24 +58,15 @@ func TestMultiUserUserIndexHandler(t *testing.T) {
// Create Request and Response // Create Request and Response
req, err := http.NewRequest("GET", user.UrlPath(), nil) req, err := http.NewRequest("GET", user.UrlPath(), nil)
if err != nil { assertions.AssertNoError(t, err, "Error creating request")
t.Fatal(err)
}
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
router := main.Router(&repo) router := main.Router(&repo)
router.ServeHTTP(rr, req) router.ServeHTTP(rr, req)
// Check the status code is what we expect. assertions.AssertStatus(t, rr, http.StatusOK)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
// Check the response body contains names of users // Check the response body contains names of users
if !strings.Contains(rr.Body.String(), "post-1") { assertions.AssertContains(t, rr.Body.String(), "post-1")
t.Error("post-1 not listed on index page. Got: ")
t.Error(rr.Body.String())
}
} }
func TestMultiUserPostHandler(t *testing.T) { func TestMultiUserPostHandler(t *testing.T) {
@ -97,18 +76,12 @@ func TestMultiUserPostHandler(t *testing.T) {
// Create Request and Response // Create Request and Response
req, err := http.NewRequest("GET", post.UrlPath(), nil) req, err := http.NewRequest("GET", post.UrlPath(), nil)
if err != nil { assertions.AssertNoError(t, err, "Error creating request")
t.Fatal(err)
}
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
router := main.Router(&repo) router := main.Router(&repo)
router.ServeHTTP(rr, req) router.ServeHTTP(rr, req)
// Check the status code is what we expect. assertions.AssertStatus(t, rr, http.StatusOK)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
} }
func TestMultiUserPostMediaHandler(t *testing.T) { func TestMultiUserPostMediaHandler(t *testing.T) {
@ -119,24 +92,16 @@ func TestMultiUserPostMediaHandler(t *testing.T) {
// Create test media file // Create test media file
path := path.Join(post.MediaDir(), "data.txt") path := path.Join(post.MediaDir(), "data.txt")
err := os.WriteFile(path, []byte("test"), 0644) err := os.WriteFile(path, []byte("test"), 0644)
if err != nil { assertions.AssertNoError(t, err, "Error creating request")
t.Fatal(err)
}
// Create Request and Response // Create Request and Response
req, err := http.NewRequest("GET", post.UrlMediaPath("data.txt"), nil) req, err := http.NewRequest("GET", post.UrlMediaPath("data.txt"), nil)
if err != nil { assertions.AssertNoError(t, err, "Error creating request")
t.Fatal(err)
}
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
router := main.Router(&repo) router := main.Router(&repo)
router.ServeHTTP(rr, req) router.ServeHTTP(rr, req)
// Check the status code is what we expect. assertions.AssertStatus(t, rr, http.StatusOK)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
// Check the response body contains data of media file // Check the response body contains data of media file
if !(rr.Body.String() == "test") { if !(rr.Body.String() == "test") {

View File

@ -3,6 +3,7 @@ package web_test
import ( import (
"h4kor/owl-blogs" "h4kor/owl-blogs"
main "h4kor/owl-blogs/cmd/owl/web" main "h4kor/owl-blogs/cmd/owl/web"
"h4kor/owl-blogs/priv/assertions"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os" "os"
@ -24,16 +25,10 @@ func TestPostHandlerReturns404OnDrafts(t *testing.T) {
// Create Request and Response // Create Request and Response
req, err := http.NewRequest("GET", post.UrlPath(), nil) req, err := http.NewRequest("GET", post.UrlPath(), nil)
if err != nil { assertions.AssertNoError(t, err, "Error creating request")
t.Fatal(err)
}
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
router := main.Router(&repo) router := main.Router(&repo)
router.ServeHTTP(rr, req) router.ServeHTTP(rr, req)
// Check the status code is what we expect. assertions.AssertStatus(t, rr, http.StatusNotFound)
if status := rr.Code; status != http.StatusNotFound {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusNotFound)
}
} }

View File

@ -3,9 +3,9 @@ package web_test
import ( import (
"h4kor/owl-blogs" "h4kor/owl-blogs"
main "h4kor/owl-blogs/cmd/owl/web" main "h4kor/owl-blogs/cmd/owl/web"
"h4kor/owl-blogs/priv/assertions"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"strings"
"testing" "testing"
) )
@ -16,28 +16,16 @@ func TestMultiUserUserRssIndexHandler(t *testing.T) {
// Create Request and Response // Create Request and Response
req, err := http.NewRequest("GET", user.UrlPath()+"index.xml", nil) req, err := http.NewRequest("GET", user.UrlPath()+"index.xml", nil)
if err != nil { assertions.AssertNoError(t, err, "Error creating request")
t.Fatal(err)
}
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
router := main.Router(&repo) router := main.Router(&repo)
router.ServeHTTP(rr, req) router.ServeHTTP(rr, req)
// Check the status code is what we expect. assertions.AssertStatus(t, rr, http.StatusOK)
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. // Check the response Content-Type is what we expect.
if !strings.Contains(rr.Header().Get("Content-Type"), "application/rss+xml") { assertions.AssertContains(t, 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 // Check the response body contains names of users
if !strings.Contains(rr.Body.String(), "post-1") { assertions.AssertContains(t, rr.Body.String(), "post-1")
t.Error("post-1 not listed on index page. Got: ")
t.Error(rr.Body.String())
}
} }

View File

@ -3,6 +3,7 @@ package web_test
import ( import (
owl "h4kor/owl-blogs" owl "h4kor/owl-blogs"
main "h4kor/owl-blogs/cmd/owl/web" main "h4kor/owl-blogs/cmd/owl/web"
"h4kor/owl-blogs/priv/assertions"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os" "os"
@ -23,24 +24,15 @@ func TestSingleUserUserIndexHandler(t *testing.T) {
// Create Request and Response // Create Request and Response
req, err := http.NewRequest("GET", user.UrlPath(), nil) req, err := http.NewRequest("GET", user.UrlPath(), nil)
if err != nil { assertions.AssertNoError(t, err, "Error creating request")
t.Fatal(err)
}
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
router := main.SingleUserRouter(&repo) router := main.SingleUserRouter(&repo)
router.ServeHTTP(rr, req) router.ServeHTTP(rr, req)
// Check the status code is what we expect. assertions.AssertStatus(t, rr, http.StatusOK)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
// Check the response body contains names of users // Check the response body contains names of users
if !strings.Contains(rr.Body.String(), "post-1") { assertions.AssertContains(t, rr.Body.String(), "post-1")
t.Error("post-1 not listed on index page. Got: ")
t.Error(rr.Body.String())
}
} }
func TestSingleUserPostHandler(t *testing.T) { func TestSingleUserPostHandler(t *testing.T) {
@ -49,18 +41,12 @@ func TestSingleUserPostHandler(t *testing.T) {
// Create Request and Response // Create Request and Response
req, err := http.NewRequest("GET", post.UrlPath(), nil) req, err := http.NewRequest("GET", post.UrlPath(), nil)
if err != nil { assertions.AssertNoError(t, err, "Error creating request")
t.Fatal(err)
}
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
router := main.SingleUserRouter(&repo) router := main.SingleUserRouter(&repo)
router.ServeHTTP(rr, req) router.ServeHTTP(rr, req)
// Check the status code is what we expect. assertions.AssertStatus(t, rr, http.StatusOK)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
} }
func TestSingleUserPostMediaHandler(t *testing.T) { func TestSingleUserPostMediaHandler(t *testing.T) {
@ -70,24 +56,16 @@ func TestSingleUserPostMediaHandler(t *testing.T) {
// Create test media file // Create test media file
path := path.Join(post.MediaDir(), "data.txt") path := path.Join(post.MediaDir(), "data.txt")
err := os.WriteFile(path, []byte("test"), 0644) err := os.WriteFile(path, []byte("test"), 0644)
if err != nil { assertions.AssertNoError(t, err, "Error creating request")
t.Fatal(err)
}
// Create Request and Response // Create Request and Response
req, err := http.NewRequest("GET", post.UrlMediaPath("data.txt"), nil) req, err := http.NewRequest("GET", post.UrlMediaPath("data.txt"), nil)
if err != nil { assertions.AssertNoError(t, err, "Error creating request")
t.Fatal(err)
}
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
router := main.SingleUserRouter(&repo) router := main.SingleUserRouter(&repo)
router.ServeHTTP(rr, req) router.ServeHTTP(rr, req)
// Check the status code is what we expect. assertions.AssertStatus(t, rr, http.StatusOK)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
// Check the response body contains data of media file // Check the response body contains data of media file
if !(rr.Body.String() == "test") { if !(rr.Body.String() == "test") {
@ -117,9 +95,7 @@ func TestHasNoDraftsInList(t *testing.T) {
// Create Request and Response // Create Request and Response
req, err := http.NewRequest("GET", "/", nil) req, err := http.NewRequest("GET", "/", nil)
if err != nil { assertions.AssertNoError(t, err, "Error creating request")
t.Fatal(err)
}
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
router := main.SingleUserRouter(&repo) router := main.SingleUserRouter(&repo)
router.ServeHTTP(rr, req) router.ServeHTTP(rr, req)

View File

@ -3,6 +3,7 @@ package web_test
import ( import (
"h4kor/owl-blogs" "h4kor/owl-blogs"
main "h4kor/owl-blogs/cmd/owl/web" main "h4kor/owl-blogs/cmd/owl/web"
"h4kor/owl-blogs/priv/assertions"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
@ -34,14 +35,6 @@ func setupWebmentionTest(repo owl.Repository, user owl.User, target string, sour
return rr, nil return rr, nil
} }
func assertStatus(t *testing.T, rr *httptest.ResponseRecorder, expStatus int) {
if status := rr.Code; status != expStatus {
t.Errorf("handler returned wrong status code: got %v want %v",
status, expStatus)
return
}
}
func TestWebmentionHandleAccepts(t *testing.T) { func TestWebmentionHandleAccepts(t *testing.T) {
repo := getTestRepo(owl.RepoConfig{}) repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser("test-1") user, _ := repo.CreateUser("test-1")
@ -51,11 +44,9 @@ func TestWebmentionHandleAccepts(t *testing.T) {
source := "https://example.com" source := "https://example.com"
rr, err := setupWebmentionTest(repo, user, target, source) rr, err := setupWebmentionTest(repo, user, target, source)
if err != nil { assertions.AssertNoError(t, err, "Error setting up webmention test")
t.Fatal(err)
}
assertStatus(t, rr, http.StatusAccepted) assertions.AssertStatus(t, rr, http.StatusAccepted)
} }
@ -69,16 +60,10 @@ func TestWebmentionWrittenToPost(t *testing.T) {
source := "https://example.com" source := "https://example.com"
rr, err := setupWebmentionTest(repo, user, target, source) rr, err := setupWebmentionTest(repo, user, target, source)
if err != nil { assertions.AssertNoError(t, err, "Error setting up webmention test")
t.Fatal(err)
}
// Check the status code is what we expect. assertions.AssertStatus(t, rr, http.StatusAccepted)
assertStatus(t, rr, http.StatusAccepted) assertions.AssertLen(t, post.IncomingWebmentions(), 1)
if len(post.IncomingWebmentions()) != 1 {
t.Errorf("no webmention written to post")
}
} }
// //
@ -98,11 +83,9 @@ func TestWebmentionSourceValidation(t *testing.T) {
source := "ftp://example.com" source := "ftp://example.com"
rr, err := setupWebmentionTest(repo, user, target, source) rr, err := setupWebmentionTest(repo, user, target, source)
if err != nil { assertions.AssertNoError(t, err, "Error setting up webmention test")
t.Fatal(err)
}
assertStatus(t, rr, http.StatusBadRequest) assertions.AssertStatus(t, rr, http.StatusBadRequest)
} }
func TestWebmentionTargetValidation(t *testing.T) { func TestWebmentionTargetValidation(t *testing.T) {
@ -115,11 +98,9 @@ func TestWebmentionTargetValidation(t *testing.T) {
source := post.FullUrl() source := post.FullUrl()
rr, err := setupWebmentionTest(repo, user, target, source) rr, err := setupWebmentionTest(repo, user, target, source)
if err != nil { assertions.AssertNoError(t, err, "Error setting up webmention test")
t.Fatal(err)
}
assertStatus(t, rr, http.StatusBadRequest) assertions.AssertStatus(t, rr, http.StatusBadRequest)
} }
// The receiver MUST reject the request if the source URL is the same as the target URL. // The receiver MUST reject the request if the source URL is the same as the target URL.
@ -134,11 +115,9 @@ func TestWebmentionSameTargetAndSource(t *testing.T) {
source := post.FullUrl() source := post.FullUrl()
rr, err := setupWebmentionTest(repo, user, target, source) rr, err := setupWebmentionTest(repo, user, target, source)
if err != nil { assertions.AssertNoError(t, err, "Error setting up webmention test")
t.Fatal(err)
}
assertStatus(t, rr, http.StatusBadRequest) assertions.AssertStatus(t, rr, http.StatusBadRequest)
} }
// The receiver SHOULD check that target is a valid resource for which it can accept Webmentions. // The receiver SHOULD check that target is a valid resource for which it can accept Webmentions.
@ -154,11 +133,9 @@ func TestValidationOfTarget(t *testing.T) {
source := post.FullUrl() source := post.FullUrl()
rr, err := setupWebmentionTest(repo, user, target, source) rr, err := setupWebmentionTest(repo, user, target, source)
if err != nil { assertions.AssertNoError(t, err, "Error setting up webmention test")
t.Fatal(err)
}
assertStatus(t, rr, http.StatusBadRequest) assertions.AssertStatus(t, rr, http.StatusBadRequest)
} }
func TestAcceptWebmentionForAlias(t *testing.T) { func TestAcceptWebmentionForAlias(t *testing.T) {
@ -179,9 +156,7 @@ func TestAcceptWebmentionForAlias(t *testing.T) {
source := "https://example.com" source := "https://example.com"
rr, err := setupWebmentionTest(repo, user, target, source) rr, err := setupWebmentionTest(repo, user, target, source)
if err != nil { assertions.AssertNoError(t, err, "Error setting up webmention test")
t.Fatal(err)
}
assertStatus(t, rr, http.StatusAccepted) assertions.AssertStatus(t, rr, http.StatusAccepted)
} }

View File

@ -2,6 +2,7 @@ package owl_test
import ( import (
"h4kor/owl-blogs" "h4kor/owl-blogs"
"h4kor/owl-blogs/priv/assertions"
"os" "os"
"path" "path"
"strconv" "strconv"
@ -121,9 +122,7 @@ func TestRawHTMLIfAllowedByRepo(t *testing.T) {
os.WriteFile(post.ContentFile(), []byte(content), 0644) os.WriteFile(post.ContentFile(), []byte(content), 0644)
html := post.RenderedContent() html := post.RenderedContent()
html_str := html.String() html_str := html.String()
if !strings.Contains(html_str, "<script>") { assertions.AssertContains(t, html_str, "<script>")
t.Error("HTML should be allowed")
}
} }
func TestLoadMeta(t *testing.T) { func TestLoadMeta(t *testing.T) {
@ -144,25 +143,13 @@ func TestLoadMeta(t *testing.T) {
err := post.LoadMeta() err := post.LoadMeta()
if err != nil { assertions.AssertNoError(t, err, "Error loading meta")
t.Errorf("Got Error: %v", err)
}
if post.Meta().Title != "test" { assertions.AssertEqual(t, post.Meta().Title, "test")
t.Errorf("Expected title: %s, got %s", "test", post.Meta().Title) assertions.AssertLen(t, post.Meta().Aliases, 1)
} assertions.AssertEqual(t, post.Meta().Draft, true)
assertions.AssertEqual(t, post.Meta().Date.Format(time.RFC1123Z), "Wed, 17 Aug 2022 10:50:02 +0000")
if len(post.Meta().Aliases) != 1 || post.Meta().Aliases[0] != "foo/bar/" { assertions.AssertEqual(t, post.Meta().Draft, true)
t.Errorf("Expected title: %v, got %v", []string{"foo/bar/"}, post.Meta().Aliases)
}
if post.Meta().Date.Format(time.RFC1123Z) != "Wed, 17 Aug 2022 10:50:02 +0000" {
t.Errorf("Expected title: %s, got %s", "Wed, 17 Aug 2022 10:50:02 +0000", post.Meta().Title)
}
if post.Meta().Draft != true {
t.Errorf("Expected title: %v, got %v", true, post.Meta().Draft)
}
} }
/// ///
@ -177,17 +164,10 @@ func TestPersistIncomingWebmention(t *testing.T) {
Source: "http://example.com/source", Source: "http://example.com/source",
} }
err := post.PersistIncomingWebmention(webmention) err := post.PersistIncomingWebmention(webmention)
if err != nil { assertions.AssertNoError(t, err, "Error persisting webmention")
t.Errorf("Got error: %v", err)
}
mentions := post.IncomingWebmentions() mentions := post.IncomingWebmentions()
if len(mentions) != 1 { assertions.AssertLen(t, mentions, 1)
t.Errorf("Expected 1 webmention, got %d", len(mentions)) assertions.AssertEqual(t, mentions[0].Source, webmention.Source)
}
if mentions[0].Source != webmention.Source {
t.Errorf("Expected source: %s, got %s", webmention.Source, mentions[0].Source)
}
} }
func TestAddIncomingWebmentionCreatesFile(t *testing.T) { func TestAddIncomingWebmentionCreatesFile(t *testing.T) {
@ -198,14 +178,10 @@ func TestAddIncomingWebmentionCreatesFile(t *testing.T) {
post, _ := user.CreateNewPost("testpost", false) post, _ := user.CreateNewPost("testpost", false)
err := post.AddIncomingWebmention("https://example.com") err := post.AddIncomingWebmention("https://example.com")
if err != nil { assertions.AssertNoError(t, err, "Error adding webmention")
t.Errorf("Got Error: %v", err)
}
mentions := post.IncomingWebmentions() mentions := post.IncomingWebmentions()
if len(mentions) != 1 { assertions.AssertLen(t, mentions, 1)
t.Errorf("Expected 1 webmention, got %d", len(mentions))
}
} }
func TestAddIncomingWebmentionNotOverwritingWebmention(t *testing.T) { func TestAddIncomingWebmentionNotOverwritingWebmention(t *testing.T) {
@ -223,13 +199,9 @@ func TestAddIncomingWebmentionNotOverwritingWebmention(t *testing.T) {
post.AddIncomingWebmention("https://example.com") post.AddIncomingWebmention("https://example.com")
mentions := post.IncomingWebmentions() mentions := post.IncomingWebmentions()
if len(mentions) != 1 { assertions.AssertLen(t, mentions, 1)
t.Errorf("Expected 1 webmention, got %d", len(mentions))
}
if mentions[0].ApprovalStatus != "approved" { assertions.AssertEqual(t, mentions[0].ApprovalStatus, "approved")
t.Errorf("Expected approval status: %s, got %s", "approved", mentions[0].ApprovalStatus)
}
} }
func TestEnrichAddsTitle(t *testing.T) { func TestEnrichAddsTitle(t *testing.T) {
@ -243,13 +215,8 @@ func TestEnrichAddsTitle(t *testing.T) {
post.EnrichWebmention(owl.WebmentionIn{Source: "https://example.com"}) post.EnrichWebmention(owl.WebmentionIn{Source: "https://example.com"})
mentions := post.IncomingWebmentions() mentions := post.IncomingWebmentions()
if len(mentions) != 1 { assertions.AssertLen(t, mentions, 1)
t.Errorf("Expected 1 webmention, got %d", len(mentions)) assertions.AssertEqual(t, mentions[0].Title, "Mock Title")
}
if mentions[0].Title != "Mock Title" {
t.Errorf("Expected title: %s, got %s", "Mock Title", mentions[0].Title)
}
} }
func TestApprovedIncomingWebmentions(t *testing.T) { func TestApprovedIncomingWebmentions(t *testing.T) {
@ -282,16 +249,10 @@ func TestApprovedIncomingWebmentions(t *testing.T) {
post.PersistIncomingWebmention(webmention) post.PersistIncomingWebmention(webmention)
webmentions := post.ApprovedIncomingWebmentions() webmentions := post.ApprovedIncomingWebmentions()
if len(webmentions) != 2 { assertions.AssertLen(t, webmentions, 2)
t.Errorf("Expected 2 webmentions, got %d", len(webmentions))
}
if webmentions[0].Source != "http://example.com/source" { assertions.AssertEqual(t, webmentions[0].Source, "http://example.com/source")
t.Errorf("Expected source: %s, got %s", "http://example.com/source", webmentions[0].Source) assertions.AssertEqual(t, webmentions[1].Source, "http://example.com/source3")
}
if webmentions[1].Source != "http://example.com/source3" {
t.Errorf("Expected source: %s, got %s", "http://example.com/source3", webmentions[1].Source)
}
} }
@ -310,12 +271,8 @@ func TestScanningForLinks(t *testing.T) {
post.ScanForLinks() post.ScanForLinks()
webmentions := post.OutgoingWebmentions() webmentions := post.OutgoingWebmentions()
if len(webmentions) != 1 { assertions.AssertLen(t, webmentions, 1)
t.Errorf("Expected 1 webmention, got %d", len(webmentions)) assertions.AssertEqual(t, webmentions[0].Target, "https://example.com/hello")
}
if webmentions[0].Target != "https://example.com/hello" {
t.Errorf("Expected target: %s, got %s", "https://example.com/hello", webmentions[0].Target)
}
} }
func TestScanningForLinksDoesNotAddDuplicates(t *testing.T) { func TestScanningForLinksDoesNotAddDuplicates(t *testing.T) {
@ -336,12 +293,8 @@ func TestScanningForLinksDoesNotAddDuplicates(t *testing.T) {
post.ScanForLinks() post.ScanForLinks()
post.ScanForLinks() post.ScanForLinks()
webmentions := post.OutgoingWebmentions() webmentions := post.OutgoingWebmentions()
if len(webmentions) != 1 { assertions.AssertLen(t, webmentions, 1)
t.Errorf("Expected 1 webmention, got %d", len(webmentions)) assertions.AssertEqual(t, webmentions[0].Target, "https://example.com/hello")
}
if webmentions[0].Target != "https://example.com/hello" {
t.Errorf("Expected target: %s, got %s", "https://example.com/hello", webmentions[0].Target)
}
} }
func TestScanningForLinksDoesAddReplyUrl(t *testing.T) { func TestScanningForLinksDoesAddReplyUrl(t *testing.T) {
@ -361,12 +314,8 @@ func TestScanningForLinksDoesAddReplyUrl(t *testing.T) {
post.ScanForLinks() post.ScanForLinks()
webmentions := post.OutgoingWebmentions() webmentions := post.OutgoingWebmentions()
if len(webmentions) != 1 { assertions.AssertLen(t, webmentions, 1)
t.Errorf("Expected 1 webmention, got %d", len(webmentions)) assertions.AssertEqual(t, webmentions[0].Target, "https://example.com/reply")
}
if webmentions[0].Target != "https://example.com/reply" {
t.Errorf("Expected target: %s, got %s", "https://example.com/reply", webmentions[0].Target)
}
} }
func TestCanSendWebmention(t *testing.T) { func TestCanSendWebmention(t *testing.T) {
@ -381,20 +330,12 @@ func TestCanSendWebmention(t *testing.T) {
} }
err := post.SendWebmention(webmention) err := post.SendWebmention(webmention)
if err != nil { assertions.AssertNoError(t, err, "Error sending webmention")
t.Errorf("Error sending webmention: %v", err)
}
webmentions := post.OutgoingWebmentions() webmentions := post.OutgoingWebmentions()
if len(webmentions) != 1 { assertions.AssertLen(t, webmentions, 1)
t.Errorf("Expected 1 webmention, got %d", len(webmentions)) assertions.AssertEqual(t, webmentions[0].Target, "http://example.com")
}
if webmentions[0].Target != "http://example.com" {
t.Errorf("Expected target: %s, got %s", "http://example.com", webmentions[0].Target)
}
if webmentions[0].LastSentAt.IsZero() { if webmentions[0].LastSentAt.IsZero() {
t.Errorf("Expected LastSentAt to be set") t.Errorf("Expected LastSentAt to be set")
} }
@ -423,13 +364,8 @@ func TestSendWebmentionOnlyScansOncePerWeek(t *testing.T) {
webmentions = post.OutgoingWebmentions() webmentions = post.OutgoingWebmentions()
if len(webmentions) != 1 { assertions.AssertLen(t, webmentions, 1)
t.Errorf("Expected 1 webmention, got %d", len(webmentions)) assertions.AssertEqual(t, webmentions[0].ScannedAt, webmention.ScannedAt)
}
if webmentions[0].ScannedAt != webmention.ScannedAt {
t.Errorf("Expected ScannedAt to be unchanged. Expected: %v, got %v", webmention.ScannedAt, webmentions[0].ScannedAt)
}
} }
func TestSendingMultipleWebmentions(t *testing.T) { func TestSendingMultipleWebmentions(t *testing.T) {
@ -456,9 +392,7 @@ func TestSendingMultipleWebmentions(t *testing.T) {
webmentions := post.OutgoingWebmentions() webmentions := post.OutgoingWebmentions()
if len(webmentions) != 20 { assertions.AssertLen(t, webmentions, 20)
t.Errorf("Expected 20 webmentions, got %d", len(webmentions))
}
} }
func TestReceivingMultipleWebmentions(t *testing.T) { func TestReceivingMultipleWebmentions(t *testing.T) {
@ -482,9 +416,7 @@ func TestReceivingMultipleWebmentions(t *testing.T) {
webmentions := post.IncomingWebmentions() webmentions := post.IncomingWebmentions()
if len(webmentions) != 20 { assertions.AssertLen(t, webmentions, 20)
t.Errorf("Expected 20 webmentions, got %d", len(webmentions))
}
} }
@ -515,16 +447,10 @@ func TestSendingAndReceivingMultipleWebmentions(t *testing.T) {
wg.Wait() wg.Wait()
ins := post.IncomingWebmentions() ins := post.IncomingWebmentions()
if len(ins) != 20 {
t.Errorf("Expected 20 webmentions, got %d", len(ins))
}
outs := post.OutgoingWebmentions() outs := post.OutgoingWebmentions()
if len(outs) != 20 { assertions.AssertLen(t, ins, 20)
t.Errorf("Expected 20 webmentions, got %d", len(outs)) assertions.AssertLen(t, outs, 20)
}
} }
func TestComplexParallelWebmentions(t *testing.T) { func TestComplexParallelWebmentions(t *testing.T) {
@ -564,16 +490,10 @@ func TestComplexParallelWebmentions(t *testing.T) {
wg.Wait() wg.Wait()
ins := post.IncomingWebmentions() ins := post.IncomingWebmentions()
if len(ins) != 20 {
t.Errorf("Expected 20 webmentions, got %d", len(ins))
}
outs := post.OutgoingWebmentions() outs := post.OutgoingWebmentions()
if len(outs) != 20 { assertions.AssertLen(t, ins, 20)
t.Errorf("Expected 20 webmentions, got %d", len(outs)) assertions.AssertLen(t, outs, 20)
}
} }
// func TestComplexParallelSimulatedProcessesWebmentions(t *testing.T) { // func TestComplexParallelSimulatedProcessesWebmentions(t *testing.T) {

View File

@ -0,0 +1,50 @@
package assertions
import (
"net/http/httptest"
"strings"
"testing"
)
func AssertContains(t *testing.T, containing string, search string) {
t.Helper()
if !strings.Contains(containing, search) {
t.Errorf("Expected '%s' to contain '%s'", containing, search)
}
}
func AssertNoError(t *testing.T, err error, message string) {
t.Helper()
if err != nil {
t.Errorf(message+": %s", err.Error())
}
}
func AssertLen[T any](t *testing.T, list []T, expected int) {
t.Helper()
if len(list) != expected {
t.Errorf("Expected list to have length %d, got %d", expected, len(list))
}
}
func AssertMapLen[T any, S comparable](t *testing.T, list map[S]T, expected int) {
t.Helper()
if len(list) != expected {
t.Errorf("Expected list to have length %d, got %d", expected, len(list))
}
}
func AssertEqual[T comparable](t *testing.T, actual T, expected T) {
t.Helper()
if actual != expected {
t.Errorf("Expected '%v', got '%v'", expected, actual)
}
}
func AssertStatus(t *testing.T, rr *httptest.ResponseRecorder, expStatus int) {
if status := rr.Code; status != expStatus {
t.Errorf("handler returned wrong status code: got %v want %v",
status, expStatus)
return
}
}

View File

@ -2,6 +2,7 @@ package owl_test
import ( import (
"h4kor/owl-blogs" "h4kor/owl-blogs"
"h4kor/owl-blogs/priv/assertions"
"os" "os"
"path" "path"
"strings" "strings"
@ -14,14 +15,8 @@ func TestCanRenderPost(t *testing.T) {
post, _ := user.CreateNewPost("testpost", false) post, _ := user.CreateNewPost("testpost", false)
result, err := owl.RenderPost(post) result, err := owl.RenderPost(post)
if err != nil { assertions.AssertNoError(t, err, "Error rendering post")
t.Error("Error rendering post: " + err.Error()) assertions.AssertContains(t, result, "testpost")
return
}
if !strings.Contains(result, "<h1 class=\"p-name\">testpost</h1>") {
t.Error("Post title not rendered as h1. Got: " + result)
}
} }
@ -37,14 +32,8 @@ func TestRenderOneMe(t *testing.T) {
post, _ := user.CreateNewPost("testpost", false) post, _ := user.CreateNewPost("testpost", false)
result, err := owl.RenderPost(post) result, err := owl.RenderPost(post)
if err != nil { assertions.AssertNoError(t, err, "Error rendering post")
t.Error("Error rendering post: " + err.Error()) assertions.AssertContains(t, result, "href=\"https://twitter.com/testhandle\" rel=\"me\"")
return
}
if !strings.Contains(result, "href=\"https://twitter.com/testhandle\" rel=\"me\"") {
t.Error("Twitter handle not rendered. Got: " + result)
}
} }
@ -64,17 +53,9 @@ func TestRenderTwoMe(t *testing.T) {
post, _ := user.CreateNewPost("testpost", false) post, _ := user.CreateNewPost("testpost", false)
result, err := owl.RenderPost(post) result, err := owl.RenderPost(post)
if err != nil { assertions.AssertNoError(t, err, "Error rendering post")
t.Error("Error rendering post: " + err.Error()) assertions.AssertContains(t, result, "href=\"https://twitter.com/testhandle\" rel=\"me\"")
return assertions.AssertContains(t, result, "href=\"https://github.com/testhandle\" rel=\"me\"")
}
if !strings.Contains(result, "href=\"https://twitter.com/testhandle\" rel=\"me\"") {
t.Error("Twitter handle not rendered. Got: " + result)
}
if !strings.Contains(result, "href=\"https://github.com/testhandle\" rel=\"me\"") {
t.Error("Github handle not rendered. Got: " + result)
}
} }
@ -82,15 +63,9 @@ func TestRenderPostHEntry(t *testing.T) {
user := getTestUser() user := getTestUser()
post, _ := user.CreateNewPost("testpost", false) post, _ := user.CreateNewPost("testpost", false)
result, _ := owl.RenderPost(post) result, _ := owl.RenderPost(post)
if !strings.Contains(result, "class=\"h-entry\"") { assertions.AssertContains(t, result, "class=\"h-entry\"")
t.Error("h-entry container not rendered. Got: " + result) assertions.AssertContains(t, result, "class=\"p-name\"")
} assertions.AssertContains(t, result, "class=\"e-content\"")
if !strings.Contains(result, "class=\"p-name\"") {
t.Error("p-name not rendered. Got: " + result)
}
if !strings.Contains(result, "class=\"e-content\"") {
t.Error("e-content not rendered. Got: " + result)
}
} }
@ -99,14 +74,8 @@ func TestRendererUsesBaseTemplate(t *testing.T) {
post, _ := user.CreateNewPost("testpost", false) post, _ := user.CreateNewPost("testpost", false)
result, err := owl.RenderPost(post) result, err := owl.RenderPost(post)
if err != nil { assertions.AssertNoError(t, err, "Error rendering post")
t.Error("Error rendering post: " + err.Error()) assertions.AssertContains(t, result, "<html")
return
}
if !strings.Contains(result, "<html") {
t.Error("Base template not used. Got: " + result)
}
} }
func TestCanRenderIndexPage(t *testing.T) { func TestCanRenderIndexPage(t *testing.T) {
@ -114,12 +83,8 @@ func TestCanRenderIndexPage(t *testing.T) {
user.CreateNewPost("testpost1", false) user.CreateNewPost("testpost1", false)
user.CreateNewPost("testpost2", false) user.CreateNewPost("testpost2", false)
result, _ := owl.RenderIndexPage(user) result, _ := owl.RenderIndexPage(user)
if !strings.Contains(result, "testpost1") { assertions.AssertContains(t, result, "testpost1")
t.Error("Post title not rendered. Got: " + result) assertions.AssertContains(t, result, "testpost2")
}
if !strings.Contains(result, "testpost2") {
t.Error("Post title not rendered. Got: " + result)
}
} }
func TestIndexPageContainsHFeedContainer(t *testing.T) { func TestIndexPageContainsHFeedContainer(t *testing.T) {
@ -127,9 +92,7 @@ func TestIndexPageContainsHFeedContainer(t *testing.T) {
user.CreateNewPost("testpost1", false) user.CreateNewPost("testpost1", false)
result, _ := owl.RenderIndexPage(user) result, _ := owl.RenderIndexPage(user)
if !strings.Contains(result, "<div class=\"h-feed\">") { assertions.AssertContains(t, result, "<div class=\"h-feed\">")
t.Error("h-feed container not rendered. Got: " + result)
}
} }
func TestIndexPageContainsHEntryAndUUrl(t *testing.T) { func TestIndexPageContainsHEntryAndUUrl(t *testing.T) {
@ -137,12 +100,8 @@ func TestIndexPageContainsHEntryAndUUrl(t *testing.T) {
user.CreateNewPost("testpost1", false) user.CreateNewPost("testpost1", false)
result, _ := owl.RenderIndexPage(user) result, _ := owl.RenderIndexPage(user)
if !strings.Contains(result, "class=\"h-entry\"") { assertions.AssertContains(t, result, "class=\"h-entry\"")
t.Error("h-entry container not rendered. Got: " + result) assertions.AssertContains(t, result, "class=\"u-url\"")
}
if !strings.Contains(result, "class=\"u-url\"") {
t.Error("u-url not rendered. Got: " + result)
}
} }
@ -165,16 +124,9 @@ func TestRenderUserList(t *testing.T) {
repo.CreateUser("user2") repo.CreateUser("user2")
result, err := owl.RenderUserList(repo) result, err := owl.RenderUserList(repo)
if err != nil { assertions.AssertNoError(t, err, "Error rendering user list")
t.Error("Error rendering user list: " + err.Error()) assertions.AssertContains(t, result, "user1")
} assertions.AssertContains(t, result, "user2")
if !strings.Contains(result, "user1") {
t.Error("Post title not rendered. Got: " + result)
}
if !strings.Contains(result, "user2") {
t.Error("Post title not rendered. Got: " + result)
}
} }
func TestRendersHeaderTitle(t *testing.T) { func TestRendersHeaderTitle(t *testing.T) {
@ -187,15 +139,9 @@ func TestRendersHeaderTitle(t *testing.T) {
post, _ := user.CreateNewPost("testpost", false) post, _ := user.CreateNewPost("testpost", false)
result, _ := owl.RenderPost(post) result, _ := owl.RenderPost(post)
if !strings.Contains(result, "Test Title") { assertions.AssertContains(t, result, "Test Title")
t.Error("Header title not rendered. Got: " + result) assertions.AssertContains(t, result, "Test SubTitle")
} assertions.AssertContains(t, result, "#ff1337")
if !strings.Contains(result, "Test SubTitle") {
t.Error("Header subtitle not rendered. Got: " + result)
}
if !strings.Contains(result, "#ff1337") {
t.Error("Header color not rendered. Got: " + result)
}
} }
func TestRenderPostIncludesRelToWebMention(t *testing.T) { func TestRenderPostIncludesRelToWebMention(t *testing.T) {
@ -203,13 +149,9 @@ func TestRenderPostIncludesRelToWebMention(t *testing.T) {
post, _ := user.CreateNewPost("testpost", false) post, _ := user.CreateNewPost("testpost", false)
result, _ := owl.RenderPost(post) result, _ := owl.RenderPost(post)
if !strings.Contains(result, "rel=\"webmention\"") { assertions.AssertContains(t, result, "rel=\"webmention\"")
t.Error("webmention rel not rendered. Got: " + result)
}
if !strings.Contains(result, "href=\""+user.WebmentionUrl()+"\"") { assertions.AssertContains(t, result, "href=\""+user.WebmentionUrl()+"\"")
t.Error("webmention href not rendered. Got: " + result)
}
} }
func TestRenderPostAddsLinksToApprovedWebmention(t *testing.T) { func TestRenderPostAddsLinksToApprovedWebmention(t *testing.T) {
@ -230,12 +172,8 @@ func TestRenderPostAddsLinksToApprovedWebmention(t *testing.T) {
post.PersistIncomingWebmention(webmention) post.PersistIncomingWebmention(webmention)
result, _ := owl.RenderPost(post) result, _ := owl.RenderPost(post)
if !strings.Contains(result, "http://example.com/source3") { assertions.AssertContains(t, result, "http://example.com/source3")
t.Error("webmention not rendered. Got: " + result) assertions.AssertContains(t, result, "Test Title")
}
if !strings.Contains(result, "Test Title") {
t.Error("webmention title not rendered. Got: " + result)
}
if strings.Contains(result, "http://example.com/source4") { if strings.Contains(result, "http://example.com/source4") {
t.Error("unapproved webmention rendered. Got: " + result) t.Error("unapproved webmention rendered. Got: " + result)
} }
@ -258,13 +196,8 @@ func TestRenderIncludesFullUrl(t *testing.T) {
post, _ := user.CreateNewPost("testpost", false) post, _ := user.CreateNewPost("testpost", false)
result, _ := owl.RenderPost(post) result, _ := owl.RenderPost(post)
if !strings.Contains(result, "class=\"u-url\"") { assertions.AssertContains(t, result, "class=\"u-url\"")
t.Error("u-url not rendered. Got: " + result) assertions.AssertContains(t, result, post.FullUrl())
}
if !strings.Contains(result, post.FullUrl()) {
t.Error("Full url not rendered. Got: " + result)
t.Error("Expected: " + post.FullUrl())
}
} }
func TestAddAvatarIfExist(t *testing.T) { func TestAddAvatarIfExist(t *testing.T) {
@ -272,9 +205,7 @@ func TestAddAvatarIfExist(t *testing.T) {
os.WriteFile(path.Join(user.MediaDir(), "avatar.png"), []byte("test"), 0644) os.WriteFile(path.Join(user.MediaDir(), "avatar.png"), []byte("test"), 0644)
result, _ := owl.RenderIndexPage(user) result, _ := owl.RenderIndexPage(user)
if !strings.Contains(result, "avatar.png") { assertions.AssertContains(t, result, "avatar.png")
t.Error("Avatar not rendered. Got: " + result)
}
} }
func TestAuthorNameInPost(t *testing.T) { func TestAuthorNameInPost(t *testing.T) {
@ -288,9 +219,7 @@ func TestAuthorNameInPost(t *testing.T) {
post, _ := user.CreateNewPost("testpost", false) post, _ := user.CreateNewPost("testpost", false)
result, _ := owl.RenderPost(post) result, _ := owl.RenderPost(post)
if !strings.Contains(result, "Test Author") { assertions.AssertContains(t, result, "Test Author")
t.Error("Author Name not included. Got: " + result)
}
} }
func TestRenderReplyWithoutText(t *testing.T) { func TestRenderReplyWithoutText(t *testing.T) {
@ -309,9 +238,7 @@ func TestRenderReplyWithoutText(t *testing.T) {
os.WriteFile(post.ContentFile(), []byte(content), 0644) os.WriteFile(post.ContentFile(), []byte(content), 0644)
result, _ := owl.RenderPost(post) result, _ := owl.RenderPost(post)
if !strings.Contains(result, "https://example.com/post") { assertions.AssertContains(t, result, "https://example.com/post")
t.Error("Reply url not rendered. Got: " + result)
}
} }
func TestRenderReplyWithText(t *testing.T) { func TestRenderReplyWithText(t *testing.T) {
@ -330,13 +257,9 @@ func TestRenderReplyWithText(t *testing.T) {
os.WriteFile(post.ContentFile(), []byte(content), 0644) os.WriteFile(post.ContentFile(), []byte(content), 0644)
result, _ := owl.RenderPost(post) result, _ := owl.RenderPost(post)
if !strings.Contains(result, "https://example.com/post") { assertions.AssertContains(t, result, "https://example.com/post")
t.Error("Reply url not rendered. Got: " + result)
}
if !strings.Contains(result, "This is a reply") { assertions.AssertContains(t, result, "This is a reply")
t.Error("Reply text not rendered. Got: " + result)
}
} }
func TestOpenGraphTags(t *testing.T) { func TestOpenGraphTags(t *testing.T) {
@ -351,28 +274,14 @@ func TestOpenGraphTags(t *testing.T) {
content += "\n" content += "\n"
content += "Hi \n" content += "Hi \n"
err := os.WriteFile(post.ContentFile(), []byte(content), 0644) os.WriteFile(post.ContentFile(), []byte(content), 0644)
if err != nil {
t.Error(err)
}
post, _ = user.GetPost(post.Id()) post, _ = user.GetPost(post.Id())
result, _ := owl.RenderPost(post) result, _ := owl.RenderPost(post)
if !strings.Contains(result, "<meta property=\"og:title\" content=\"The Rock\" />") { assertions.AssertContains(t, result, "<meta property=\"og:title\" content=\"The Rock\" />")
t.Error("incorrent og:title . Got: " + result) assertions.AssertContains(t, result, "<meta property=\"og:description\" content=\"Dwayne Johnson\" />")
} assertions.AssertContains(t, result, "<meta property=\"og:type\" content=\"article\" />")
assertions.AssertContains(t, result, "<meta property=\"og:url\" content=\""+post.FullUrl()+"\" />")
if !strings.Contains(result, "<meta property=\"og:description\" content=\"Dwayne Johnson\" />") {
t.Error("incorrent og:description . Got: " + result)
}
if !strings.Contains(result, "<meta property=\"og:type\" content=\"article\" />") {
t.Error("incorrent og:type . Got: " + result)
}
if !strings.Contains(result, "<meta property=\"og:url\" content=\""+post.FullUrl()+"\" />") {
t.Error("incorrent og:url . Got: " + result)
}
} }
@ -381,7 +290,5 @@ func TestAddFaviconIfExist(t *testing.T) {
os.WriteFile(path.Join(user.MediaDir(), "favicon.png"), []byte("test"), 0644) os.WriteFile(path.Join(user.MediaDir(), "favicon.png"), []byte("test"), 0644)
result, _ := owl.RenderIndexPage(user) result, _ := owl.RenderIndexPage(user)
if !strings.Contains(result, "favicon.png") { assertions.AssertContains(t, result, "favicon.png")
t.Error("favicon not rendered. Got: " + result)
}
} }

View File

@ -2,6 +2,7 @@ package owl_test
import ( import (
"h4kor/owl-blogs" "h4kor/owl-blogs"
"h4kor/owl-blogs/priv/assertions"
"os" "os"
"path" "path"
"testing" "testing"
@ -10,9 +11,7 @@ import (
func TestCanCreateRepository(t *testing.T) { func TestCanCreateRepository(t *testing.T) {
repoName := testRepoName() repoName := testRepoName()
_, err := owl.CreateRepository(repoName, owl.RepoConfig{}) _, err := owl.CreateRepository(repoName, owl.RepoConfig{})
if err != nil { assertions.AssertNoError(t, err, "Error creating repository: ")
t.Error("Error creating repository: ", err.Error())
}
} }
@ -88,9 +87,7 @@ func TestCanListRepoUsers(t *testing.T) {
user2, _ := repo.CreateUser(randomUserName()) user2, _ := repo.CreateUser(randomUserName())
// Create a new post // Create a new post
users, _ := repo.Users() users, _ := repo.Users()
if len(users) != 2 { assertions.AssertLen(t, users, 2)
t.Error("Wrong number of users returned, expected 2, got ", len(users))
}
for _, user := range users { for _, user := range users {
if user.Name() != user1.Name() && user.Name() != user2.Name() { if user.Name() != user1.Name() && user.Name() != user2.Name() {
t.Error("User found: " + user.Name()) t.Error("User found: " + user.Name())
@ -104,9 +101,7 @@ func TestCanOpenRepository(t *testing.T) {
repo, _ := owl.CreateRepository(repoName, owl.RepoConfig{}) repo, _ := owl.CreateRepository(repoName, owl.RepoConfig{})
// Open the repository // Open the repository
repo2, err := owl.OpenRepository(repoName) repo2, err := owl.OpenRepository(repoName)
if err != nil { assertions.AssertNoError(t, err, "Error opening repository: ")
t.Error("Error opening repository: ", err.Error())
}
if repo2.Dir() != repo.Dir() { if repo2.Dir() != repo.Dir() {
t.Error("Repository directories do not match") t.Error("Repository directories do not match")
} }
@ -125,9 +120,7 @@ func TestGetUser(t *testing.T) {
user, _ := repo.CreateUser(randomUserName()) user, _ := repo.CreateUser(randomUserName())
// Get the user // Get the user
user2, err := repo.GetUser(user.Name()) user2, err := repo.GetUser(user.Name())
if err != nil { assertions.AssertNoError(t, err, "Error getting user: ")
t.Error("Error getting user: ", err.Error())
}
if user2.Name() != user.Name() { if user2.Name() != user.Name() {
t.Error("User names do not match") t.Error("User names do not match")
} }
@ -171,9 +164,7 @@ func TestNewRepoGetsStaticFilesPicoCSSWithContent(t *testing.T) {
// Create a new user // Create a new user
repo := getTestRepo(owl.RepoConfig{}) repo := getTestRepo(owl.RepoConfig{})
file, err := os.Open(path.Join(repo.StaticDir(), "pico.min.css")) file, err := os.Open(path.Join(repo.StaticDir(), "pico.min.css"))
if err != nil { assertions.AssertNoError(t, err, "Error opening pico.min.css")
t.Error("Error opening pico.min.css")
}
// check that the file has content // check that the file has content
stat, _ := file.Stat() stat, _ := file.Stat()
if stat.Size() == 0 { if stat.Size() == 0 {
@ -194,9 +185,7 @@ func TestCanGetRepoTemplate(t *testing.T) {
repo := getTestRepo(owl.RepoConfig{}) repo := getTestRepo(owl.RepoConfig{})
// Get the user // Get the user
template, err := repo.Template() template, err := repo.Template()
if err != nil { assertions.AssertNoError(t, err, "Error getting template: ")
t.Error("Error getting template: ", err.Error())
}
if template == "" { if template == "" {
t.Error("Template not returned") t.Error("Template not returned")
} }
@ -215,9 +204,7 @@ func TestCanOpenRepositoryInSingleUserMode(t *testing.T) {
repo, _ := owl.OpenRepository(repoName) repo, _ := owl.OpenRepository(repoName)
users, _ := repo.Users() users, _ := repo.Users()
if len(users) != 1 { assertions.AssertLen(t, users, 1)
t.Error("Wrong number of users returned, expected 1, got ", len(users))
}
if users[0].Name() != userName { if users[0].Name() != userName {
t.Error("User name does not match") t.Error("User name does not match")
} }
@ -253,19 +240,12 @@ func TestCanGetMapWithAllPostAliases(t *testing.T) {
os.WriteFile(post.ContentFile(), []byte(content), 0644) os.WriteFile(post.ContentFile(), []byte(content), 0644)
posts, _ := user.Posts() posts, _ := user.Posts()
if len(posts) != 1 { assertions.AssertLen(t, posts, 1)
t.Error("Wrong number of posts returned, expected 1, got ", len(posts))
}
var aliases map[string]*owl.Post var aliases map[string]*owl.Post
aliases, err := repo.PostAliases() aliases, err := repo.PostAliases()
if err != nil { assertions.AssertNoError(t, err, "Error getting post aliases: ")
t.Error("Error getting post aliases: ", err.Error()) assertions.AssertMapLen(t, aliases, 2)
}
if len(aliases) != 2 {
t.Error("Wrong number of aliases returned, expected 2, got ", len(aliases))
t.Error("Aliases: ", aliases)
}
if aliases["/foo/bar"] == nil { if aliases["/foo/bar"] == nil {
t.Error("Alias '/foo/bar' not found") t.Error("Alias '/foo/bar' not found")
} }
@ -298,19 +278,12 @@ func TestAliasesHaveCorrectPost(t *testing.T) {
os.WriteFile(post2.ContentFile(), []byte(content), 0644) os.WriteFile(post2.ContentFile(), []byte(content), 0644)
posts, _ := user.Posts() posts, _ := user.Posts()
if len(posts) != 2 { assertions.AssertLen(t, posts, 2)
t.Error("Wrong number of posts returned, expected 1, got ", len(posts))
}
var aliases map[string]*owl.Post var aliases map[string]*owl.Post
aliases, err := repo.PostAliases() aliases, err := repo.PostAliases()
if err != nil { assertions.AssertNoError(t, err, "Error getting post aliases: ")
t.Error("Error getting post aliases: ", err.Error()) assertions.AssertMapLen(t, aliases, 2)
}
if len(aliases) != 2 {
t.Error("Wrong number of aliases returned, expected 2, got ", len(aliases))
t.Error("Aliases: ", aliases)
}
if aliases["/foo/1"].Id() != post1.Id() { if aliases["/foo/1"].Id() != post1.Id() {
t.Error("Alias '/foo/1' points to wrong post: ", aliases["/foo/1"].Id()) t.Error("Alias '/foo/1' points to wrong post: ", aliases["/foo/1"].Id())
} }

View File

@ -2,8 +2,8 @@ package owl_test
import ( import (
"h4kor/owl-blogs" "h4kor/owl-blogs"
"h4kor/owl-blogs/priv/assertions"
"os" "os"
"strings"
"testing" "testing"
) )
@ -14,16 +14,9 @@ func TestRenderRSSFeedMeta(t *testing.T) {
SubTitle: "Test SubTitle", SubTitle: "Test SubTitle",
}) })
res, err := owl.RenderRSSFeed(user) res, err := owl.RenderRSSFeed(user)
if err != nil { assertions.AssertNoError(t, err, "Error rendering RSS feed")
t.Error("Error rendering RSS feed: " + err.Error()) assertions.AssertContains(t, res, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
return assertions.AssertContains(t, res, "<rss version=\"2.0\">")
}
if !strings.Contains(res, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>") {
t.Error("xml version not rendered. Got: " + res)
}
if !strings.Contains(res, "<rss version=\"2.0\">") {
t.Error("rss version not rendered. Got: " + res)
}
} }
@ -34,19 +27,10 @@ func TestRenderRSSFeedUserData(t *testing.T) {
SubTitle: "Test SubTitle", SubTitle: "Test SubTitle",
}) })
res, err := owl.RenderRSSFeed(user) res, err := owl.RenderRSSFeed(user)
if err != nil { assertions.AssertNoError(t, err, "Error rendering RSS feed")
t.Error("Error rendering RSS feed: " + err.Error()) assertions.AssertContains(t, res, "Test Title")
return assertions.AssertContains(t, res, "Test SubTitle")
} assertions.AssertContains(t, res, "http://localhost:8080/user/")
if !strings.Contains(res, "Test Title") {
t.Error("Title not rendered. Got: " + res)
}
if !strings.Contains(res, "Test SubTitle") {
t.Error("SubTitle not rendered. Got: " + res)
}
if !strings.Contains(res, "http://localhost:8080/user/") {
t.Error("SubTitle not rendered. Got: " + res)
}
} }
func TestRenderRSSFeedPostData(t *testing.T) { func TestRenderRSSFeedPostData(t *testing.T) {
@ -61,19 +45,10 @@ func TestRenderRSSFeedPostData(t *testing.T) {
os.WriteFile(post.ContentFile(), []byte(content), 0644) os.WriteFile(post.ContentFile(), []byte(content), 0644)
res, err := owl.RenderRSSFeed(user) res, err := owl.RenderRSSFeed(user)
if err != nil { assertions.AssertNoError(t, err, "Error rendering RSS feed")
t.Error("Error rendering RSS feed: " + err.Error()) assertions.AssertContains(t, res, "Test Post")
return assertions.AssertContains(t, res, post.FullUrl())
} assertions.AssertContains(t, res, "Thu, 01 Jan 2015 00:00:00 +0000")
if !strings.Contains(res, "Test Post") {
t.Error("Title not rendered. Got: " + res)
}
if !strings.Contains(res, post.FullUrl()) {
t.Error("SubTitle not rendered. Got: " + res)
}
if !strings.Contains(res, "Thu, 01 Jan 2015 00:00:00 +0000") {
t.Error("Date not rendered. Got: " + res)
}
} }
func TestRenderRSSFeedPostDataWithoutDate(t *testing.T) { func TestRenderRSSFeedPostDataWithoutDate(t *testing.T) {
@ -87,14 +62,7 @@ func TestRenderRSSFeedPostDataWithoutDate(t *testing.T) {
os.WriteFile(post.ContentFile(), []byte(content), 0644) os.WriteFile(post.ContentFile(), []byte(content), 0644)
res, err := owl.RenderRSSFeed(user) res, err := owl.RenderRSSFeed(user)
if err != nil { assertions.AssertNoError(t, err, "Error rendering RSS feed")
t.Error("Error rendering RSS feed: " + err.Error()) assertions.AssertContains(t, res, "Test Post")
return assertions.AssertContains(t, res, post.FullUrl())
}
if !strings.Contains(res, "Test Post") {
t.Error("Title not rendered. Got: " + res)
}
if !strings.Contains(res, post.FullUrl()) {
t.Error("SubTitle not rendered. Got: " + res)
}
} }

View File

@ -3,6 +3,7 @@ package owl_test
import ( import (
"fmt" "fmt"
"h4kor/owl-blogs" "h4kor/owl-blogs"
"h4kor/owl-blogs/priv/assertions"
"os" "os"
"path" "path"
"testing" "testing"
@ -15,9 +16,7 @@ func TestCreateNewPostCreatesEntryInPublic(t *testing.T) {
// Create a new post // Create a new post
user.CreateNewPost("testpost", false) user.CreateNewPost("testpost", false)
files, err := os.ReadDir(path.Join(user.Dir(), "public")) files, err := os.ReadDir(path.Join(user.Dir(), "public"))
if err != nil { assertions.AssertNoError(t, err, "Error reading directory")
t.Error("Error reading directory")
}
if len(files) < 1 { if len(files) < 1 {
t.Error("Post not created") t.Error("Post not created")
} }
@ -55,9 +54,7 @@ func TestCreateNewPostMultipleCalls(t *testing.T) {
user.CreateNewPost("testpost", false) user.CreateNewPost("testpost", false)
user.CreateNewPost("testpost", false) user.CreateNewPost("testpost", false)
files, err := os.ReadDir(path.Join(user.Dir(), "public")) files, err := os.ReadDir(path.Join(user.Dir(), "public"))
if err != nil { assertions.AssertNoError(t, err, "Error reading directory")
t.Error("Error reading directory")
}
if len(files) < 3 { if len(files) < 3 {
t.Errorf("Only %d posts created", len(files)) t.Errorf("Only %d posts created", len(files))
} }
@ -72,12 +69,8 @@ func TestCanListUserPosts(t *testing.T) {
user.CreateNewPost("testpost", false) user.CreateNewPost("testpost", false)
user.CreateNewPost("testpost", false) user.CreateNewPost("testpost", false)
posts, err := user.Posts() posts, err := user.Posts()
if err != nil { assertions.AssertNoError(t, err, "Error reading posts")
t.Error("Error reading posts") assertions.AssertLen(t, posts, 3)
}
if len(posts) != 3 {
t.Error("No posts found")
}
} }
func TestCannotListUserPostsInSubdirectories(t *testing.T) { func TestCannotListUserPostsInSubdirectories(t *testing.T) {
@ -162,9 +155,7 @@ func TestListUserPostsDoesNotIncludeDrafts(t *testing.T) {
os.WriteFile(post.ContentFile(), []byte(content), 0644) os.WriteFile(post.ContentFile(), []byte(content), 0644)
posts, _ := user.Posts() posts, _ := user.Posts()
if len(posts) != 0 { assertions.AssertLen(t, posts, 0)
t.Error("Found draft post")
}
} }
func TestListUsersDraftsExcludedRealWorld(t *testing.T) { func TestListUsersDraftsExcludedRealWorld(t *testing.T) {
@ -190,9 +181,7 @@ func TestListUsersDraftsExcludedRealWorld(t *testing.T) {
os.WriteFile(post.ContentFile(), []byte(content), 0644) os.WriteFile(post.ContentFile(), []byte(content), 0644)
posts, _ := user.Posts() posts, _ := user.Posts()
if len(posts) != 0 { assertions.AssertLen(t, posts, 0)
t.Error("Found draft post")
}
} }
func TestCanLoadPost(t *testing.T) { func TestCanLoadPost(t *testing.T) {
@ -320,9 +309,7 @@ func TestAvatarSetIfFileExist(t *testing.T) {
func TestPostNameIllegalFileName(t *testing.T) { func TestPostNameIllegalFileName(t *testing.T) {
user := getTestUser() user := getTestUser()
_, err := user.CreateNewPost("testpost?///", false) _, err := user.CreateNewPost("testpost?///", false)
if err != nil { assertions.AssertNoError(t, err, "Should not have failed")
t.Error("Should not have failed")
}
} }
func TestFaviconIfNotExist(t *testing.T) { func TestFaviconIfNotExist(t *testing.T) {

View File

@ -3,6 +3,7 @@ package owl_test
import ( import (
"bytes" "bytes"
"h4kor/owl-blogs" "h4kor/owl-blogs"
"h4kor/owl-blogs/priv/assertions"
"io" "io"
"net/http" "net/http"
"net/url" "net/url"
@ -28,12 +29,8 @@ func TestParseValidHEntry(t *testing.T) {
parser := &owl.OwlHtmlParser{} parser := &owl.OwlHtmlParser{}
entry, err := parser.ParseHEntry(&http.Response{Body: io.NopCloser(bytes.NewReader(html))}) entry, err := parser.ParseHEntry(&http.Response{Body: io.NopCloser(bytes.NewReader(html))})
if err != nil { assertions.AssertNoError(t, err, "Unable to parse feed")
t.Errorf("Unable to parse feed: %v", err) assertions.AssertEqual(t, entry.Title, "Foo")
}
if entry.Title != "Foo" {
t.Errorf("Wrong Title. Expected %v, got %v", "Foo", entry.Title)
}
} }
func TestParseValidHEntryWithoutTitle(t *testing.T) { func TestParseValidHEntryWithoutTitle(t *testing.T) {
@ -41,12 +38,8 @@ func TestParseValidHEntryWithoutTitle(t *testing.T) {
parser := &owl.OwlHtmlParser{} parser := &owl.OwlHtmlParser{}
entry, err := parser.ParseHEntry(&http.Response{Body: io.NopCloser(bytes.NewReader(html))}) entry, err := parser.ParseHEntry(&http.Response{Body: io.NopCloser(bytes.NewReader(html))})
if err != nil { assertions.AssertNoError(t, err, "Unable to parse feed")
t.Errorf("Unable to parse feed: %v", err) assertions.AssertEqual(t, entry.Title, "")
}
if entry.Title != "" {
t.Errorf("Wrong Title. Expected %v, got %v", "Foo", entry.Title)
}
} }
func TestGetWebmentionEndpointLink(t *testing.T) { func TestGetWebmentionEndpointLink(t *testing.T) {
@ -54,12 +47,9 @@ func TestGetWebmentionEndpointLink(t *testing.T) {
parser := &owl.OwlHtmlParser{} parser := &owl.OwlHtmlParser{}
endpoint, err := parser.GetWebmentionEndpoint(constructResponse(html)) endpoint, err := parser.GetWebmentionEndpoint(constructResponse(html))
if err != nil { assertions.AssertNoError(t, err, "Unable to parse feed")
t.Errorf("Unable to parse feed: %v", err)
} assertions.AssertEqual(t, endpoint, "http://example.com/webmention")
if endpoint != "http://example.com/webmention" {
t.Errorf("Wrong endpoint. Expected %v, got %v", "http://example.com/webmention", endpoint)
}
} }
func TestGetWebmentionEndpointLinkA(t *testing.T) { func TestGetWebmentionEndpointLinkA(t *testing.T) {
@ -67,12 +57,8 @@ func TestGetWebmentionEndpointLinkA(t *testing.T) {
parser := &owl.OwlHtmlParser{} parser := &owl.OwlHtmlParser{}
endpoint, err := parser.GetWebmentionEndpoint(constructResponse(html)) endpoint, err := parser.GetWebmentionEndpoint(constructResponse(html))
if err != nil { assertions.AssertNoError(t, err, "Unable to parse feed")
t.Errorf("Unable to parse feed: %v", err) assertions.AssertEqual(t, endpoint, "http://example.com/webmention")
}
if endpoint != "http://example.com/webmention" {
t.Errorf("Wrong endpoint. Expected %v, got %v", "http://example.com/webmention", endpoint)
}
} }
func TestGetWebmentionEndpointLinkAFakeWebmention(t *testing.T) { func TestGetWebmentionEndpointLinkAFakeWebmention(t *testing.T) {
@ -80,12 +66,8 @@ func TestGetWebmentionEndpointLinkAFakeWebmention(t *testing.T) {
parser := &owl.OwlHtmlParser{} parser := &owl.OwlHtmlParser{}
endpoint, err := parser.GetWebmentionEndpoint(constructResponse(html)) endpoint, err := parser.GetWebmentionEndpoint(constructResponse(html))
if err != nil { assertions.AssertNoError(t, err, "Unable to parse feed")
t.Errorf("Unable to parse feed: %v", err) assertions.AssertEqual(t, endpoint, "http://example.com/webmention")
}
if endpoint != "http://example.com/webmention" {
t.Errorf("Wrong endpoint. Expected %v, got %v", "http://example.com/webmention", endpoint)
}
} }
func TestGetWebmentionEndpointLinkHeader(t *testing.T) { func TestGetWebmentionEndpointLinkHeader(t *testing.T) {
@ -95,12 +77,8 @@ func TestGetWebmentionEndpointLinkHeader(t *testing.T) {
resp.Header = http.Header{"Link": []string{"<http://example.com/webmention>; rel=\"webmention\""}} resp.Header = http.Header{"Link": []string{"<http://example.com/webmention>; rel=\"webmention\""}}
endpoint, err := parser.GetWebmentionEndpoint(resp) endpoint, err := parser.GetWebmentionEndpoint(resp)
if err != nil { assertions.AssertNoError(t, err, "Unable to parse feed")
t.Errorf("Unable to parse feed: %v", err) assertions.AssertEqual(t, endpoint, "http://example.com/webmention")
}
if endpoint != "http://example.com/webmention" {
t.Errorf("Wrong endpoint. Expected %v, got %v", "http://example.com/webmention", endpoint)
}
} }
func TestGetWebmentionEndpointLinkHeaderCommas(t *testing.T) { func TestGetWebmentionEndpointLinkHeaderCommas(t *testing.T) {
@ -112,12 +90,8 @@ func TestGetWebmentionEndpointLinkHeaderCommas(t *testing.T) {
} }
endpoint, err := parser.GetWebmentionEndpoint(resp) endpoint, err := parser.GetWebmentionEndpoint(resp)
if err != nil { assertions.AssertNoError(t, err, "Unable to parse feed")
t.Errorf("Unable to parse feed: %v", err) assertions.AssertEqual(t, endpoint, "https://webmention.rocks/test/19/webmention")
}
if endpoint != "https://webmention.rocks/test/19/webmention" {
t.Errorf("Wrong endpoint. Expected %v, got %v", "https://webmention.rocks/test/19/webmention", endpoint)
}
} }
func TestGetWebmentionEndpointRelativeLink(t *testing.T) { func TestGetWebmentionEndpointRelativeLink(t *testing.T) {
@ -125,12 +99,8 @@ func TestGetWebmentionEndpointRelativeLink(t *testing.T) {
parser := &owl.OwlHtmlParser{} parser := &owl.OwlHtmlParser{}
endpoint, err := parser.GetWebmentionEndpoint(constructResponse(html)) endpoint, err := parser.GetWebmentionEndpoint(constructResponse(html))
if err != nil { assertions.AssertNoError(t, err, "Unable to parse feed")
t.Errorf("Unable to parse feed: %v", err) assertions.AssertEqual(t, endpoint, "http://example.com/webmention")
}
if endpoint != "http://example.com/webmention" {
t.Errorf("Wrong endpoint. Expected %v, got %v", "http://example.com/webmention", endpoint)
}
} }
func TestGetWebmentionEndpointRelativeLinkInHeader(t *testing.T) { func TestGetWebmentionEndpointRelativeLinkInHeader(t *testing.T) {
@ -140,12 +110,8 @@ func TestGetWebmentionEndpointRelativeLinkInHeader(t *testing.T) {
resp.Header = http.Header{"Link": []string{"</webmention>; rel=\"webmention\""}} resp.Header = http.Header{"Link": []string{"</webmention>; rel=\"webmention\""}}
endpoint, err := parser.GetWebmentionEndpoint(resp) endpoint, err := parser.GetWebmentionEndpoint(resp)
if err != nil { assertions.AssertNoError(t, err, "Unable to parse feed")
t.Errorf("Unable to parse feed: %v", err) assertions.AssertEqual(t, endpoint, "http://example.com/webmention")
}
if endpoint != "http://example.com/webmention" {
t.Errorf("Wrong endpoint. Expected %v, got %v", "http://example.com/webmention", endpoint)
}
} }
// func TestRealWorldWebmention(t *testing.T) { // func TestRealWorldWebmention(t *testing.T) {