quartzgun/quartzgun_test.go

53 lines
1.4 KiB
Go

package main
import (
"fmt"
"net/http"
"html/template"
"context"
"nilfm.cc/git/quartzgun/router"
"nilfm.cc/git/quartzgun/renderer"
"nilfm.cc/git/quartzgun/indentalUserDB"
"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")
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("/", AddContent(renderer.Template("testData/templates/test.html")))
rtr.Get("/json", ApiSomething(renderer.JSON("apiData")))
rtr.Get(`/thing/(?P<Thing>\w+)`, renderer.Template("testData/templates/paramTest.html"))
http.ListenAndServe(":8080", rtr)
}