quartzgun/quartzgun_test.go

71 lines
2.1 KiB
Go

package main
import (
"context"
"fmt"
"forge.lightcrystal.systems/nilix/quartzgun/indentalUserDB"
"forge.lightcrystal.systems/nilix/quartzgun/middleware"
"forge.lightcrystal.systems/nilix/quartzgun/rateLimiter"
"forge.lightcrystal.systems/nilix/quartzgun/renderer"
"forge.lightcrystal.systems/nilix/quartzgun/router"
"html/template"
"net/http"
"testing"
)
func AddContent(next http.Handler) http.Handler {
handlerFunc := func(w http.ResponseWriter, req *http.Request) {
if !req.Form.Has("Content") {
req.Form.Add("Content", "Yesssssss")
}
next.ServeHTTP(w, req)
}
return http.HandlerFunc(handlerFunc)
}
func ApiSomething(next http.Handler) http.Handler {
handlerFunc := func(w http.ResponseWriter, req *http.Request) {
*req = *req.WithContext(context.WithValue(req.Context(), "apiData", "something"))
next.ServeHTTP(w, req)
}
return http.HandlerFunc(handlerFunc)
}
func TestMain(m *testing.M) {
udb := indentalUserDB.CreateIndentalUserDB("testData/userDB.ndtl")
udb.AddUser("nilix", "questing")
sesh, _ := udb.InitiateSession("nilix", "questing", 60)
bouncer := rateLimiter.IpRateLimiter{
map[string]*rateLimiter.RateLimitData{},
5,
2,
}
fmt.Printf("%s // %s\n", sesh, sesh)
rtr := &router.Router{
StaticPaths: map[string]string{
"/static": "testData/static",
},
Fallback: *template.Must(template.ParseFiles("testData/templates/error.html", "testData/templates/footer.html")),
}
rtr.Get("/login", renderer.Template(
"testData/templates/login.html"))
rtr.Post("/login", middleware.Authorize("/", udb, "/login?tryagain=1", 120))
rtr.Post("/provision", middleware.Provision(udb, 60))
rtr.Get("/protected", middleware.Validate(renderer.Template("testData/templates/test.html"), udb, map[string]string{}))
rtr.Get("/", middleware.Protected(
renderer.Template(
"testData/templates/test.html"), http.MethodGet, udb, "/login"))
rtr.Get("/json", middleware.Throttle(ApiSomething(renderer.JSON("apiData")), bouncer.RateLimit))
rtr.Get(`/thing/(?P<Thing>\w+)`, renderer.Template("testData/templates/paramTest.html"))
http.ListenAndServe(":8080", rtr)
}