git.iko.soy/typeable/oauth2-proxy
on remove-gap-headers help sign in
typeable/oauth2-proxy/templates_test.go
62 lines 1.6 kB history raw
1package main
3import (
4 "bytes"
5 "io/ioutil"
6 "log"
7 "os"
8 "path/filepath"
9 "testing"
11 "github.com/stretchr/testify/assert"
14func TestLoadTemplates(t *testing.T) {
15 data := struct {
16 TestString string
17 }{
18 TestString: "Testing",
19 }
21 templates := loadTemplates("")
22 assert.NotEqual(t, templates, nil)
24 var defaultSignin bytes.Buffer
25 templates.ExecuteTemplate(&defaultSignin, "sign_in.html", data)
26 assert.Equal(t, "\n<!DOCTYPE html>", defaultSignin.String()[0:16])
28 var defaultError bytes.Buffer
29 templates.ExecuteTemplate(&defaultError, "error.html", data)
30 assert.Equal(t, "\n<!DOCTYPE html>", defaultError.String()[0:16])
32 dir, err := ioutil.TempDir("", "templatetest")
33 if err != nil {
34 log.Fatal(err)
35 }
36 defer os.RemoveAll(dir)
38 templateHTML := `{{.TestString}} {{.TestString | ToLower}} {{.TestString | ToUpper}}`
39 signInFile := filepath.Join(dir, "sign_in.html")
40 if err := ioutil.WriteFile(signInFile, []byte(templateHTML), 0666); err != nil {
41 log.Fatal(err)
42 }
43 errorFile := filepath.Join(dir, "error.html")
44 if err := ioutil.WriteFile(errorFile, []byte(templateHTML), 0666); err != nil {
45 log.Fatal(err)
46 }
47 templates = loadTemplates(dir)
48 assert.NotEqual(t, templates, nil)
50 var sitpl bytes.Buffer
51 templates.ExecuteTemplate(&sitpl, "sign_in.html", data)
52 assert.Equal(t, "Testing testing TESTING", sitpl.String())
54 var errtpl bytes.Buffer
55 templates.ExecuteTemplate(&errtpl, "error.html", data)
56 assert.Equal(t, "Testing testing TESTING", errtpl.String())
59func TestTemplatesCompile(t *testing.T) {
60 templates := getTemplates()
61 assert.NotEqual(t, templates, nil)