mirror of https://github.com/jlelse/GoBlog
More tests
This commit is contained in:
parent
33e9d53a93
commit
d48f4f556a
|
@ -44,7 +44,7 @@ func Test_authMiddleware(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = app.initDatabase(false)
|
_ = app.initDatabase(false)
|
||||||
app.initComponents()
|
app.initComponents(false)
|
||||||
|
|
||||||
app.d = http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
app.d = http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||||
_, _ = rw.Write([]byte("ABC Test"))
|
_, _ = rw.Write([]byte("ABC Test"))
|
||||||
|
|
|
@ -43,7 +43,7 @@ func Test_blogStats(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = app.initDatabase(false)
|
_ = app.initDatabase(false)
|
||||||
app.initComponents()
|
app.initComponents(false)
|
||||||
|
|
||||||
// Insert post
|
// Insert post
|
||||||
|
|
||||||
|
|
|
@ -32,7 +32,7 @@ func Test_captchaMiddleware(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = app.initDatabase(false)
|
_ = app.initDatabase(false)
|
||||||
app.initComponents()
|
app.initComponents(false)
|
||||||
|
|
||||||
h := app.captchaMiddleware(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
h := app.captchaMiddleware(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||||
_, _ = rw.Write([]byte("ABC Test"))
|
_, _ = rw.Write([]byte("ABC Test"))
|
||||||
|
|
|
@ -37,7 +37,7 @@ func Test_comments(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = app.initDatabase(false)
|
_ = app.initDatabase(false)
|
||||||
app.initComponents()
|
app.initComponents(false)
|
||||||
|
|
||||||
t.Run("Successful comment", func(t *testing.T) {
|
t.Run("Successful comment", func(t *testing.T) {
|
||||||
|
|
||||||
|
|
|
@ -31,7 +31,7 @@ func Test_errors(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = app.initDatabase(false)
|
_ = app.initDatabase(false)
|
||||||
app.initComponents()
|
app.initComponents(false)
|
||||||
|
|
||||||
t.Run("Test 404, no HTML", func(t *testing.T) {
|
t.Run("Test 404, no HTML", func(t *testing.T) {
|
||||||
h := http.HandlerFunc(app.serve404)
|
h := http.HandlerFunc(app.serve404)
|
||||||
|
|
|
@ -0,0 +1,105 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func initTestHttpLogs(logFile string) (http.Handler, error) {
|
||||||
|
|
||||||
|
app := &goBlog{
|
||||||
|
cfg: &config{
|
||||||
|
Server: &configServer{
|
||||||
|
Logging: true,
|
||||||
|
LogFile: logFile,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
err := app.initHTTPLog()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return app.logMiddleware(testHttpHandler()), nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func testHttpHandler() http.Handler {
|
||||||
|
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||||
|
_, _ = rw.Write([]byte("Test"))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_httpLogs(t *testing.T) {
|
||||||
|
|
||||||
|
// Init
|
||||||
|
|
||||||
|
logFile := filepath.Join(t.TempDir(), "access.log")
|
||||||
|
handler, err := initTestHttpLogs(logFile)
|
||||||
|
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// Do fake request
|
||||||
|
|
||||||
|
req := httptest.NewRequest(http.MethodGet, "/testpath", nil)
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
|
||||||
|
handler.ServeHTTP(rec, req)
|
||||||
|
|
||||||
|
// Check response
|
||||||
|
|
||||||
|
res := rec.Result()
|
||||||
|
resBody, _ := io.ReadAll(res.Body)
|
||||||
|
resBodyStr := string(resBody)
|
||||||
|
|
||||||
|
assert.Equal(t, http.StatusOK, res.StatusCode)
|
||||||
|
assert.Contains(t, resBodyStr, "Test")
|
||||||
|
|
||||||
|
// Check log
|
||||||
|
|
||||||
|
logBytes, err := os.ReadFile(logFile)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
logString := string(logBytes)
|
||||||
|
assert.Contains(t, logString, "GET /testpath")
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func Benchmark_httpLogs(b *testing.B) {
|
||||||
|
|
||||||
|
// Init
|
||||||
|
|
||||||
|
logFile := filepath.Join(b.TempDir(), "access.log")
|
||||||
|
logHandler, err := initTestHttpLogs(logFile)
|
||||||
|
require.NoError(b, err)
|
||||||
|
|
||||||
|
noLogHandler := testHttpHandler()
|
||||||
|
|
||||||
|
// Run benchmarks
|
||||||
|
|
||||||
|
b.Run("With logging", func(b *testing.B) {
|
||||||
|
b.RunParallel(func(p *testing.PB) {
|
||||||
|
for p.Next() {
|
||||||
|
logHandler.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/testpath", nil))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
b.Run("Without logging", func(b *testing.B) {
|
||||||
|
b.RunParallel(func(p *testing.PB) {
|
||||||
|
for p.Next() {
|
||||||
|
noLogHandler.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/testpath", nil))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
12
main.go
12
main.go
|
@ -116,7 +116,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize components
|
// Initialize components
|
||||||
app.initComponents()
|
app.initComponents(true)
|
||||||
|
|
||||||
// Start cron hooks
|
// Start cron hooks
|
||||||
app.startHourlyHooks()
|
app.startHourlyHooks()
|
||||||
|
@ -132,10 +132,12 @@ func main() {
|
||||||
app.shutdown.Wait()
|
app.shutdown.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *goBlog) initComponents() {
|
func (app *goBlog) initComponents(logging bool) {
|
||||||
var err error
|
var err error
|
||||||
// Log start
|
// Log start
|
||||||
log.Println("Initialize components...")
|
if logging {
|
||||||
|
log.Println("Initialize components...")
|
||||||
|
}
|
||||||
app.initMarkdown()
|
app.initMarkdown()
|
||||||
if err = app.initTemplateAssets(); err != nil { // Needs minify
|
if err = app.initTemplateAssets(); err != nil { // Needs minify
|
||||||
app.logErrAndQuit("Failed to init template assets:", err.Error())
|
app.logErrAndQuit("Failed to init template assets:", err.Error())
|
||||||
|
@ -170,7 +172,9 @@ func (app *goBlog) initComponents() {
|
||||||
app.initBlogStats()
|
app.initBlogStats()
|
||||||
app.initSessions()
|
app.initSessions()
|
||||||
// Log finish
|
// Log finish
|
||||||
log.Println("Initialized components")
|
if logging {
|
||||||
|
log.Println("Initialized components")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *goBlog) logErrAndQuit(v ...interface{}) {
|
func (a *goBlog) logErrAndQuit(v ...interface{}) {
|
||||||
|
|
|
@ -0,0 +1,106 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/v5/middleware"
|
||||||
|
"github.com/justinas/alice"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_privateMode(t *testing.T) {
|
||||||
|
|
||||||
|
// Init
|
||||||
|
|
||||||
|
app := &goBlog{
|
||||||
|
cfg: &config{
|
||||||
|
Db: &configDb{
|
||||||
|
File: filepath.Join(t.TempDir(), "db.db"),
|
||||||
|
},
|
||||||
|
Server: &configServer{},
|
||||||
|
PrivateMode: &configPrivateMode{
|
||||||
|
Enabled: true,
|
||||||
|
},
|
||||||
|
User: &configUser{
|
||||||
|
Name: "Test",
|
||||||
|
Nick: "test",
|
||||||
|
Password: "testpw",
|
||||||
|
AppPasswords: []*configAppPassword{
|
||||||
|
{
|
||||||
|
Username: "testapp",
|
||||||
|
Password: "pw",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
DefaultBlog: "en",
|
||||||
|
Blogs: map[string]*configBlog{
|
||||||
|
"en": {
|
||||||
|
Lang: "en",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = app.initDatabase(false)
|
||||||
|
app.initComponents(false)
|
||||||
|
|
||||||
|
handler := alice.New(middleware.WithValue(blogKey, "en"), app.privateModeHandler).ThenFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||||
|
_, _ = rw.Write([]byte("Awesome"))
|
||||||
|
})
|
||||||
|
|
||||||
|
// Test check
|
||||||
|
|
||||||
|
assert.True(t, app.isPrivate())
|
||||||
|
|
||||||
|
// Test successful request
|
||||||
|
|
||||||
|
req := httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||||
|
req.SetBasicAuth("testapp", "pw")
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
|
||||||
|
handler.ServeHTTP(rec, req)
|
||||||
|
|
||||||
|
res := rec.Result()
|
||||||
|
resBody, _ := io.ReadAll(res.Body)
|
||||||
|
resBodyStr := string(resBody)
|
||||||
|
|
||||||
|
assert.Equal(t, http.StatusOK, res.StatusCode)
|
||||||
|
assert.Equal(t, "Awesome", resBodyStr)
|
||||||
|
|
||||||
|
// Test unauthenticated request
|
||||||
|
|
||||||
|
req = httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||||
|
rec = httptest.NewRecorder()
|
||||||
|
|
||||||
|
handler.ServeHTTP(rec, req)
|
||||||
|
|
||||||
|
res = rec.Result()
|
||||||
|
resBody, _ = io.ReadAll(res.Body)
|
||||||
|
resBodyStr = string(resBody)
|
||||||
|
|
||||||
|
assert.Equal(t, http.StatusOK, res.StatusCode) // Not 401, to be compatible with some apps
|
||||||
|
assert.NotEqual(t, "Awesome", resBodyStr)
|
||||||
|
assert.NotContains(t, resBodyStr, "Awesome")
|
||||||
|
assert.Contains(t, resBodyStr, "Login")
|
||||||
|
|
||||||
|
// Disable private mode
|
||||||
|
|
||||||
|
app.cfg.PrivateMode.Enabled = false
|
||||||
|
|
||||||
|
req = httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||||
|
rec = httptest.NewRecorder()
|
||||||
|
|
||||||
|
handler.ServeHTTP(rec, req)
|
||||||
|
|
||||||
|
res = rec.Result()
|
||||||
|
resBody, _ = io.ReadAll(res.Body)
|
||||||
|
resBodyStr = string(resBody)
|
||||||
|
|
||||||
|
assert.Equal(t, http.StatusOK, res.StatusCode)
|
||||||
|
assert.Equal(t, "Awesome", resBodyStr)
|
||||||
|
|
||||||
|
}
|
|
@ -29,7 +29,7 @@ func Test_webmentions(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = app.initDatabase(false)
|
_ = app.initDatabase(false)
|
||||||
app.initComponents()
|
app.initComponents(false)
|
||||||
|
|
||||||
_ = app.db.insertWebmention(&mention{
|
_ = app.db.insertWebmention(&mention{
|
||||||
Source: "https://example.net/test",
|
Source: "https://example.net/test",
|
||||||
|
|
Loading…
Reference in New Issue