2023-05-01 15:48:53 +02:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
_ "github.com/xiaoqidun/entps"
|
|
|
|
|
2023-05-03 02:18:29 +02:00
|
|
|
"git.dotya.ml/mirre-mt/pcmt/app/settings"
|
2023-05-01 15:48:53 +02:00
|
|
|
"git.dotya.ml/mirre-mt/pcmt/config"
|
2023-05-03 23:21:47 +02:00
|
|
|
"git.dotya.ml/mirre-mt/pcmt/ent/enttest"
|
2023-05-01 15:48:53 +02:00
|
|
|
"git.dotya.ml/mirre-mt/pcmt/slogging"
|
|
|
|
)
|
|
|
|
|
2023-05-03 23:21:47 +02:00
|
|
|
const connstr = "file:ent_tests?mode=memory&_fk=1"
|
|
|
|
|
2023-05-01 15:48:53 +02:00
|
|
|
var conf = &config.Config{
|
2023-05-05 19:31:43 +02:00
|
|
|
Port: 3005,
|
|
|
|
AppName: "pcmt-test",
|
|
|
|
LiveMode: true,
|
|
|
|
DevelMode: false,
|
|
|
|
Session: struct {
|
|
|
|
CookieName string
|
|
|
|
CookieSecret string
|
|
|
|
}{
|
|
|
|
CookieName: "sessionz",
|
|
|
|
CookieSecret: "secret",
|
|
|
|
},
|
2023-05-01 15:48:53 +02:00
|
|
|
}
|
|
|
|
|
2023-05-03 02:18:29 +02:00
|
|
|
var (
|
|
|
|
host = new(string)
|
|
|
|
port = new(int)
|
|
|
|
devel = new(bool)
|
|
|
|
)
|
|
|
|
|
2023-05-01 15:48:53 +02:00
|
|
|
func TestStaticRoute(t *testing.T) {
|
2023-05-03 02:18:29 +02:00
|
|
|
setting := settings.New()
|
2023-05-01 15:48:53 +02:00
|
|
|
a := &App{
|
|
|
|
templatesPath: "../templates",
|
2023-05-08 00:13:33 +02:00
|
|
|
assetsPath: "../assets/public",
|
2023-05-01 15:48:53 +02:00
|
|
|
}
|
2023-05-03 23:21:47 +02:00
|
|
|
log := slogging.Init(false)
|
2023-05-01 15:48:53 +02:00
|
|
|
tstRoute := "/static/"
|
2023-05-03 23:21:47 +02:00
|
|
|
db := enttest.Open(t, "sqlite3", connstr)
|
2023-05-01 15:48:53 +02:00
|
|
|
|
|
|
|
defer db.Close()
|
|
|
|
|
2023-05-03 23:21:47 +02:00
|
|
|
if err := db.Schema.Create(context.Background()); err != nil {
|
|
|
|
t.Fatalf("failed creating schema resources: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-05-03 02:18:29 +02:00
|
|
|
*host = ""
|
|
|
|
*port = 3500
|
|
|
|
*devel = true
|
|
|
|
|
|
|
|
setting.Consolidate(conf, host, port, devel, "test")
|
|
|
|
|
|
|
|
err := a.Init(setting, log, db)
|
2023-05-01 15:48:53 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to initialise app: %v", a)
|
|
|
|
}
|
|
|
|
|
|
|
|
a.SetupRoutes()
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, tstRoute, nil)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
a.E().ServeHTTP(rec, req)
|
|
|
|
|
|
|
|
want := http.StatusMovedPermanently
|
|
|
|
got := rec.Code
|
|
|
|
|
|
|
|
if want != got {
|
|
|
|
t.Fatalf("unexpected status code when testing %s, want: %d, got %d",
|
|
|
|
tstRoute, want, got,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
uri, err := rec.Result().Location()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("error getting response location")
|
|
|
|
}
|
|
|
|
|
|
|
|
if uri.Path != "/assets/" {
|
|
|
|
t.Errorf("unexpected location, want: %s, got: %s", "/assets/", uri.Path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkStatic(b *testing.B) {
|
2023-05-03 02:18:29 +02:00
|
|
|
setting := settings.New()
|
2023-05-01 15:48:53 +02:00
|
|
|
a := &App{
|
|
|
|
templatesPath: "../templates",
|
2023-05-08 00:13:33 +02:00
|
|
|
assetsPath: "../assets/public",
|
2023-05-01 15:48:53 +02:00
|
|
|
}
|
2023-05-03 23:21:47 +02:00
|
|
|
log := slogging.Init(false)
|
|
|
|
db := enttest.Open(b, "sqlite3", connstr)
|
2023-05-01 15:48:53 +02:00
|
|
|
|
|
|
|
defer db.Close()
|
|
|
|
|
2023-05-03 23:21:47 +02:00
|
|
|
if err := db.Schema.Create(context.Background()); err != nil {
|
|
|
|
b.Fatalf("failed creating schema resources: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-05-03 02:18:29 +02:00
|
|
|
setting.Consolidate(conf, host, port, devel, "bench")
|
|
|
|
|
|
|
|
err := a.Init(setting, log, db)
|
2023-05-01 15:48:53 +02:00
|
|
|
if err != nil {
|
|
|
|
b.Errorf("failed to initialise app: %v", a)
|
|
|
|
}
|
|
|
|
|
|
|
|
a.SetupRoutes()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/static/", nil)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
a.E().ServeHTTP(rec, req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkStatic2(b *testing.B) {
|
2023-05-03 02:18:29 +02:00
|
|
|
setting := settings.New()
|
2023-05-01 15:48:53 +02:00
|
|
|
a := &App{
|
|
|
|
templatesPath: "../templates",
|
2023-05-08 00:13:33 +02:00
|
|
|
assetsPath: "../assets/public",
|
2023-05-01 15:48:53 +02:00
|
|
|
}
|
2023-05-03 23:21:47 +02:00
|
|
|
log := slogging.Init(false)
|
|
|
|
db := enttest.Open(b, "sqlite3", connstr)
|
2023-05-01 15:48:53 +02:00
|
|
|
|
|
|
|
defer db.Close()
|
|
|
|
|
2023-05-03 23:21:47 +02:00
|
|
|
if err := db.Schema.Create(context.Background()); err != nil {
|
|
|
|
b.Fatalf("failed creating schema resources: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-05-03 02:18:29 +02:00
|
|
|
setting.Consolidate(conf, host, port, devel, "bench")
|
|
|
|
|
|
|
|
err := a.Init(setting, log, db)
|
2023-05-01 15:48:53 +02:00
|
|
|
if err != nil {
|
|
|
|
b.Errorf("failed to initialise app: %v", a)
|
|
|
|
}
|
|
|
|
|
|
|
|
a.SetupRoutes()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/static2/", nil)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
a.E().ServeHTTP(rec, req)
|
|
|
|
}
|
|
|
|
}
|