mirror of https://github.com/jlelse/GoBlog
Tests for regex redirects
This commit is contained in:
parent
0bfde0bb83
commit
b78c044020
|
@ -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))
|
||||
})
|
||||
|
||||
}
|
|
@ -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 = `
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue