all repos — quartzgun @ 483e59e2b26f5797f99336bbe325411cf0dcaf77

lightweight web framework in go

quartzgun_test.go (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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)
}