quartzgun/quartzgun_test.go

54 lines
1.4 KiB
Go
Raw Normal View History

2022-01-08 20:31:10 +00:00
package main
import (
"context"
"fmt"
"html/template"
2022-01-08 20:31:10 +00:00
"net/http"
"nilfm.cc/git/quartzgun/indentalUserDB"
"nilfm.cc/git/quartzgun/renderer"
"nilfm.cc/git/quartzgun/router"
"testing"
2022-01-08 20:31:10 +00:00
)
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)
2022-01-08 20:31:10 +00:00
}
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)
}
2022-01-08 20:31:10 +00:00
return http.HandlerFunc(handlerFunc)
2022-01-08 20:31:10 +00:00
}
func TestMain(m *testing.M) {
udb := indentalUserDB.CreateIndentalUserDB("testData/userDB.ndtl")
udb.AddUser("nilix", "questing")
sesh, _ := udb.InitiateSession("nilix", "questing")
2022-01-08 20:31:10 +00:00
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")),
}
2022-01-08 20:31:10 +00:00
rtr.Get("/", AddContent(renderer.Template("testData/templates/test.html")))
2022-01-08 20:31:10 +00:00
rtr.Get("/json", ApiSomething(renderer.JSON("apiData")))
2022-01-08 20:31:10 +00:00
rtr.Get(`/thing/(?P<Thing>\w+)`, renderer.Template("testData/templates/paramTest.html"))
2022-01-08 20:31:10 +00:00
http.ListenAndServe(":8080", rtr)
2022-01-08 20:31:10 +00:00
}