mirror of https://github.com/jlelse/GoBlog
Add tests for sitemap and default "tags" taxonomy
This commit is contained in:
parent
193d658a62
commit
cdf5fd12ce
|
@ -447,6 +447,12 @@ func createDefaultBlog() *configBlog {
|
||||||
Title: "Posts",
|
Title: "Posts",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Taxonomies: []*configTaxonomy{
|
||||||
|
{
|
||||||
|
Name: "tags",
|
||||||
|
Title: "Tags",
|
||||||
|
},
|
||||||
|
},
|
||||||
DefaultSection: "posts",
|
DefaultSection: "posts",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,17 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/carlmjohnson/requests"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_serveDate(t *testing.T) {
|
func Test_serveDate(t *testing.T) {
|
||||||
|
var err error
|
||||||
|
|
||||||
app := &goBlog{
|
app := &goBlog{
|
||||||
cfg: createDefaultTestConfig(t),
|
cfg: createDefaultTestConfig(t),
|
||||||
|
@ -17,9 +19,11 @@ func Test_serveDate(t *testing.T) {
|
||||||
_ = app.initConfig()
|
_ = app.initConfig()
|
||||||
_ = app.initDatabase(false)
|
_ = app.initDatabase(false)
|
||||||
app.initComponents(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",
|
Path: "/testpost",
|
||||||
Section: "posts",
|
Section: "posts",
|
||||||
Status: "published",
|
Status: "published",
|
||||||
|
@ -30,70 +34,68 @@ func Test_serveDate(t *testing.T) {
|
||||||
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
req, _ := http.NewRequest(http.MethodGet, "http://localhost:8080/2020/10/15", nil)
|
client := &http.Client{
|
||||||
res, err := doHandlerRequest(req, app.d)
|
Transport: &handlerRoundTripper{
|
||||||
|
handler: app.d,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var resString string
|
||||||
|
|
||||||
|
err = requests.
|
||||||
|
URL("http://localhost:8080/2020/10/15").
|
||||||
|
CheckStatus(http.StatusOK).
|
||||||
|
ToString(&resString).
|
||||||
|
Client(client).Fetch(context.Background())
|
||||||
require.NoError(t, err)
|
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, "Test Post")
|
||||||
assert.Contains(t, resString, "<h1 class=p-name>2020-10-15</h1>")
|
assert.Contains(t, resString, "<h1 class=p-name>2020-10-15</h1>")
|
||||||
|
|
||||||
req, _ = http.NewRequest(http.MethodGet, "http://localhost:8080/2020/10", nil)
|
err = requests.
|
||||||
res, err = doHandlerRequest(req, app.d)
|
URL("http://localhost:8080/2020/10").
|
||||||
|
CheckStatus(http.StatusOK).
|
||||||
|
ToString(&resString).
|
||||||
|
Client(client).Fetch(context.Background())
|
||||||
require.NoError(t, err)
|
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, "Test Post")
|
||||||
assert.Contains(t, resString, "<h1 class=p-name>2020-10</h1>")
|
assert.Contains(t, resString, "<h1 class=p-name>2020-10</h1>")
|
||||||
|
|
||||||
req, _ = http.NewRequest(http.MethodGet, "http://localhost:8080/2020", nil)
|
err = requests.
|
||||||
res, err = doHandlerRequest(req, app.d)
|
URL("http://localhost:8080/2020").
|
||||||
|
CheckStatus(http.StatusOK).
|
||||||
|
ToString(&resString).
|
||||||
|
Client(client).Fetch(context.Background())
|
||||||
require.NoError(t, err)
|
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, "Test Post")
|
||||||
assert.Contains(t, resString, "<h1 class=p-name>2020</h1>")
|
assert.Contains(t, resString, "<h1 class=p-name>2020</h1>")
|
||||||
|
|
||||||
req, _ = http.NewRequest(http.MethodGet, "http://localhost:8080/x/10", nil)
|
err = requests.
|
||||||
res, err = doHandlerRequest(req, app.d)
|
URL("http://localhost:8080/x/10").
|
||||||
|
CheckStatus(http.StatusOK).
|
||||||
|
ToString(&resString).
|
||||||
|
Client(client).Fetch(context.Background())
|
||||||
require.NoError(t, err)
|
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, "Test Post")
|
||||||
assert.Contains(t, resString, "<h1 class=p-name>XXXX-10</h1>")
|
assert.Contains(t, resString, "<h1 class=p-name>XXXX-10</h1>")
|
||||||
|
|
||||||
req, _ = http.NewRequest(http.MethodGet, "http://localhost:8080/x/x/15", nil)
|
err = requests.
|
||||||
res, err = doHandlerRequest(req, app.d)
|
URL("http://localhost:8080/x/x/15").
|
||||||
|
CheckStatus(http.StatusOK).
|
||||||
|
ToString(&resString).
|
||||||
|
Client(client).Fetch(context.Background())
|
||||||
require.NoError(t, err)
|
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, "Test Post")
|
||||||
assert.Contains(t, resString, "<h1 class=p-name>XXXX-XX-15</h1>")
|
assert.Contains(t, resString, "<h1 class=p-name>XXXX-XX-15</h1>")
|
||||||
|
|
||||||
req, _ = http.NewRequest(http.MethodGet, "http://localhost:8080/x", nil)
|
err = requests.
|
||||||
res, err = doHandlerRequest(req, app.d)
|
URL("http://localhost:8080/x").
|
||||||
|
CheckStatus(http.StatusNotFound).
|
||||||
|
ToString(&resString).
|
||||||
|
Client(client).Fetch(context.Background())
|
||||||
require.NoError(t, err)
|
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…
Reference in New Issue