Tests for regex redirects

This commit is contained in:
Jan-Lukas Else 2021-08-18 10:28:17 +02:00
parent 0bfde0bb83
commit b78c044020
4 changed files with 82 additions and 7 deletions

77
regexRedirects_test.go Normal file
View File

@ -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))
})
}

View File

@ -178,8 +178,8 @@ func (a *goBlog) writeSitemapXML(w http.ResponseWriter, sm interface{}) {
buf.WriteString(`<?xml-stylesheet type="text/xsl" href="`)
buf.WriteString(a.assetFileName("sitemap.xsl"))
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 = `

View File

@ -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

View File

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