Add tests for sitemap and default "tags" taxonomy

pull/10/head
Jan-Lukas Else 12 months ago
parent 193d658a62
commit cdf5fd12ce
  1. 6
      config.go
  2. 86
      posts_test.go
  3. 98
      sitemap_test.go

@ -447,6 +447,12 @@ func createDefaultBlog() *configBlog {
Title: "Posts",
},
},
Taxonomies: []*configTaxonomy{
{
Name: "tags",
Title: "Tags",
},
},
DefaultSection: "posts",
}
}

@ -1,15 +1,17 @@
package main
import (
"io"
"context"
"net/http"
"testing"
"github.com/carlmjohnson/requests"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_serveDate(t *testing.T) {
var err error
app := &goBlog{
cfg: createDefaultTestConfig(t),
@ -17,9 +19,11 @@ func Test_serveDate(t *testing.T) {
_ = app.initConfig()
_ = app.initDatabase(false)
app.initComponents(false)
app.d, _ = app.buildRouter()
err := app.createPost(&post{
app.d, err = app.buildRouter()
require.NoError(t, err)
err = app.createPost(&post{
Path: "/testpost",
Section: "posts",
Status: "published",
@ -30,70 +34,68 @@ func Test_serveDate(t *testing.T) {
require.NoError(t, err)
req, _ := http.NewRequest(http.MethodGet, "http://localhost:8080/2020/10/15", nil)
res, err := doHandlerRequest(req, app.d)
client := &http.Client{
Transport: &handlerRoundTripper{
handler: app.d,
},
}
require.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)
var resString string
resBody, _ := io.ReadAll(res.Body)
resString := string(resBody)
err = requests.
URL("http://localhost:8080/2020/10/15").
CheckStatus(http.StatusOK).
ToString(&resString).
Client(client).Fetch(context.Background())
require.NoError(t, err)
assert.Contains(t, resString, "Test Post")
assert.Contains(t, resString, "<h1 class=p-name>2020-10-15</h1>")
req, _ = http.NewRequest(http.MethodGet, "http://localhost:8080/2020/10", nil)
res, err = doHandlerRequest(req, app.d)
err = requests.
URL("http://localhost:8080/2020/10").
CheckStatus(http.StatusOK).
ToString(&resString).
Client(client).Fetch(context.Background())
require.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)
resBody, _ = io.ReadAll(res.Body)
resString = string(resBody)
assert.Contains(t, resString, "Test Post")
assert.Contains(t, resString, "<h1 class=p-name>2020-10</h1>")
req, _ = http.NewRequest(http.MethodGet, "http://localhost:8080/2020", nil)
res, err = doHandlerRequest(req, app.d)
err = requests.
URL("http://localhost:8080/2020").
CheckStatus(http.StatusOK).
ToString(&resString).
Client(client).Fetch(context.Background())
require.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)
resBody, _ = io.ReadAll(res.Body)
resString = string(resBody)
assert.Contains(t, resString, "Test Post")
assert.Contains(t, resString, "<h1 class=p-name>2020</h1>")
req, _ = http.NewRequest(http.MethodGet, "http://localhost:8080/x/10", nil)
res, err = doHandlerRequest(req, app.d)
err = requests.
URL("http://localhost:8080/x/10").
CheckStatus(http.StatusOK).
ToString(&resString).
Client(client).Fetch(context.Background())
require.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)
resBody, _ = io.ReadAll(res.Body)
resString = string(resBody)
assert.Contains(t, resString, "Test Post")
assert.Contains(t, resString, "<h1 class=p-name>XXXX-10</h1>")
req, _ = http.NewRequest(http.MethodGet, "http://localhost:8080/x/x/15", nil)
res, err = doHandlerRequest(req, app.d)
err = requests.
URL("http://localhost:8080/x/x/15").
CheckStatus(http.StatusOK).
ToString(&resString).
Client(client).Fetch(context.Background())
require.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)
resBody, _ = io.ReadAll(res.Body)
resString = string(resBody)
assert.Contains(t, resString, "Test Post")
assert.Contains(t, resString, "<h1 class=p-name>XXXX-XX-15</h1>")
req, _ = http.NewRequest(http.MethodGet, "http://localhost:8080/x", nil)
res, err = doHandlerRequest(req, app.d)
err = requests.
URL("http://localhost:8080/x").
CheckStatus(http.StatusNotFound).
ToString(&resString).
Client(client).Fetch(context.Background())
require.NoError(t, err)
assert.Equal(t, http.StatusNotFound, res.StatusCode)
}

@ -0,0 +1,98 @@
package main
import (
"context"
"net/http"
"testing"
"github.com/carlmjohnson/requests"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_sitemap(t *testing.T) {
var err error
app := &goBlog{
cfg: createDefaultTestConfig(t),
}
_ = app.initConfig()
_ = app.initDatabase(false)
app.initComponents(false)
app.d, err = app.buildRouter()
require.NoError(t, err)
err = app.createPost(&post{
Path: "/testpost",
Section: "posts",
Status: "published",
Published: "2020-10-15T10:00:00Z",
Parameters: map[string][]string{
"title": {"Test Post"},
"tags": {"Test"},
},
Content: "Test Content",
})
require.NoError(t, err)
client := &http.Client{
Transport: &handlerRoundTripper{
handler: app.d,
},
}
var resString string
err = requests.
URL("http://localhost:8080/sitemap.xml").
CheckStatus(http.StatusOK).
ToString(&resString).
Client(client).Fetch(context.Background())
require.NoError(t, err)
assert.Contains(t, resString, "http://localhost:8080/sitemap-blog.xml")
err = requests.
URL("http://localhost:8080/sitemap-blog.xml").
CheckStatus(http.StatusOK).
ToString(&resString).
Client(client).Fetch(context.Background())
require.NoError(t, err)
assert.Contains(t, resString, "http://localhost:8080/sitemap-blog-posts.xml")
assert.Contains(t, resString, "http://localhost:8080/sitemap-blog-features.xml")
assert.Contains(t, resString, "http://localhost:8080/sitemap-blog-archives.xml")
err = requests.
URL("http://localhost:8080/sitemap-blog-posts.xml").
CheckStatus(http.StatusOK).
ToString(&resString).
Client(client).Fetch(context.Background())
require.NoError(t, err)
assert.Contains(t, resString, "http://localhost:8080/testpost")
err = requests.
URL("http://localhost:8080/sitemap-blog-archives.xml").
CheckStatus(http.StatusOK).
ToString(&resString).
Client(client).Fetch(context.Background())
require.NoError(t, err)
assert.Contains(t, resString, "http://localhost:8080/2020/10/15</loc>")
assert.Contains(t, resString, "http://localhost:8080/2020/10</loc>")
assert.Contains(t, resString, "http://localhost:8080/2020</loc>")
assert.Contains(t, resString, "http://localhost:8080/x/10/15</loc>")
assert.Contains(t, resString, "http://localhost:8080/x/x/15</loc>")
assert.Contains(t, resString, "http://localhost:8080/tags/test</loc>")
err = requests.
URL("http://localhost:8080/sitemap-blog-features.xml").
CheckStatus(http.StatusOK).
ToString(&resString).
Client(client).Fetch(context.Background())
require.NoError(t, err)
assert.Contains(t, resString, "http://localhost:8080</loc>")
}
Loading…
Cancel
Save