diff --git a/config.go b/config.go index 0db2c44..56b4f1f 100644 --- a/config.go +++ b/config.go @@ -447,6 +447,12 @@ func createDefaultBlog() *configBlog { Title: "Posts", }, }, + Taxonomies: []*configTaxonomy{ + { + Name: "tags", + Title: "Tags", + }, + }, DefaultSection: "posts", } } diff --git a/posts_test.go b/posts_test.go index 81af580..80bd3c9 100644 --- a/posts_test.go +++ b/posts_test.go @@ -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, + }, + } + 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) - 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, "

2020-10-15

") - 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, "

2020-10

") - 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, "

2020

") - 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, "

XXXX-10

") - 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, "

XXXX-XX-15

") - 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) - } diff --git a/sitemap_test.go b/sitemap_test.go new file mode 100644 index 0000000..3dfe504 --- /dev/null +++ b/sitemap_test.go @@ -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") + assert.Contains(t, resString, "http://localhost:8080/2020/10") + assert.Contains(t, resString, "http://localhost:8080/2020") + assert.Contains(t, resString, "http://localhost:8080/x/10/15") + assert.Contains(t, resString, "http://localhost:8080/x/x/15") + assert.Contains(t, resString, "http://localhost:8080/tags/test") + + 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") +}