From b78c04402075a99f2d21c411af6fddcbaa101b3c Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Wed, 18 Aug 2021 10:28:17 +0200 Subject: [PATCH] Tests for regex redirects --- regexRedirects_test.go | 77 +++++++++++++++++++++++++++++++++++++++ sitemap.go | 4 +- webmentionVerification.go | 2 +- webmention_test.go | 6 +-- 4 files changed, 82 insertions(+), 7 deletions(-) create mode 100644 regexRedirects_test.go diff --git a/regexRedirects_test.go b/regexRedirects_test.go new file mode 100644 index 0000000..12e7880 --- /dev/null +++ b/regexRedirects_test.go @@ -0,0 +1,77 @@ +package main + +import ( + "io" + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func Test_regexRedirects(t *testing.T) { + + app := &goBlog{ + cfg: &config{ + PathRedirects: []*configRegexRedirect{ + { + From: "\\/index\\.xml", + To: ".rss", + Type: 301, + }, + { + From: "^\\/(abc|def)\\/posts(.*)$", + To: "/$1$2", + }, + }, + }, + } + + err := app.initRegexRedirects() + require.NoError(t, err) + + h := app.checkRegexRedirects(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + _, _ = rw.Write([]byte("OK")) + })) + + t.Run("First redirect", func(t *testing.T) { + req := httptest.NewRequest(http.MethodPost, "/posts/index.xml", nil) + rec := httptest.NewRecorder() + + h.ServeHTTP(rec, req) + + res := rec.Result() + + assert.Equal(t, 301, res.StatusCode) + assert.Equal(t, "/posts.rss", res.Header.Get("Location")) + }) + + t.Run("Second redirect", func(t *testing.T) { + req := httptest.NewRequest(http.MethodPost, "/def/posts/test", nil) + rec := httptest.NewRecorder() + + h.ServeHTTP(rec, req) + + res := rec.Result() + + assert.Equal(t, http.StatusFound, res.StatusCode) + assert.Equal(t, "/def/test", res.Header.Get("Location")) + }) + + t.Run("No redirect", func(t *testing.T) { + req := httptest.NewRequest(http.MethodPost, "/posts", nil) + rec := httptest.NewRecorder() + + h.ServeHTTP(rec, req) + + res := rec.Result() + resBody, _ := io.ReadAll(res.Body) + _ = res.Body.Close() + + assert.Equal(t, http.StatusOK, res.StatusCode) + assert.Empty(t, res.Header.Get("Location")) + assert.Equal(t, "OK", string(resBody)) + }) + +} diff --git a/sitemap.go b/sitemap.go index f9473fa..97a497b 100644 --- a/sitemap.go +++ b/sitemap.go @@ -178,8 +178,8 @@ func (a *goBlog) writeSitemapXML(w http.ResponseWriter, sm interface{}) { buf.WriteString(``) - xml.NewEncoder(&buf).Encode(sm) - a.min.Write(w, contenttype.XML, buf.Bytes()) + _ = xml.NewEncoder(&buf).Encode(sm) + _, _ = a.min.Write(w, contenttype.XML, buf.Bytes()) } const sitemapDatePathsSql = ` diff --git a/webmentionVerification.go b/webmentionVerification.go index acd35d8..c8717a9 100644 --- a/webmentionVerification.go +++ b/webmentionVerification.go @@ -113,7 +113,7 @@ func (a *goBlog) verifyMention(m *mention) error { _, err = a.db.exec("update webmentions set status = @status, title = @title, content = @content, author = @author where lowerx(source) = lowerx(@source) and lowerx(target) = lowerx(@target)", sql.Named("status", newStatus), sql.Named("title", m.Title), sql.Named("content", m.Content), sql.Named("author", m.Author), sql.Named("source", m.Source), sql.Named("target", m.Target)) } else { - a.db.insertWebmention(m, newStatus) + _ = a.db.insertWebmention(m, newStatus) a.sendNotification(fmt.Sprintf("New webmention from %s to %s", m.Source, m.Target)) } return err diff --git a/webmention_test.go b/webmention_test.go index 19c12ff..5cecdf6 100644 --- a/webmention_test.go +++ b/webmention_test.go @@ -31,7 +31,7 @@ func Test_webmentions(t *testing.T) { _ = app.initDatabase(false) app.initComponents() - app.db.insertWebmention(&mention{ + _ = app.db.insertWebmention(&mention{ Source: "https://example.net/test", Target: "https://example.com/täst", Created: time.Now().Unix(), @@ -63,9 +63,7 @@ func Test_webmentions(t *testing.T) { }) require.NoError(t, err) if assert.Len(t, mentions, 1) { - - app.db.approveWebmention(mentions[0].ID) - + _ = app.db.approveWebmention(mentions[0].ID) } mentions = app.db.getWebmentionsByAddress("https://example.com/täst")