Debian Codegraph @

fs_embed_test.go

raw symbols SCIP
1 // +build go1.16
2
3 package render
4
5 import (
6 "embed"
7 "net/http"
8 "net/http/httptest"
9 "testing"
10 )
11
12 //go:embed testdata/*/*.html testdata/*/*.tmpl testdata/*/*/*.tmpl
13 var EmbedFixtures embed.FS
14
15 func TestEmbedFileSystemTemplateLookup(t *testing.T) {
16 baseDir := "testdata/template-dir-test"
17 fname0Rel := "0"
18 fname1Rel := "subdir/1"
19 fnameShouldParsedRel := "dedicated.tmpl/notbad"
20 dirShouldNotParsedRel := "dedicated"
21
22 r := New(Options{
23 Directory: baseDir,
24 Extensions: []string{".tmpl", ".html"},
25 FileSystem: &EmbedFileSystem{
26 FS: EmbedFixtures,
27 },
28 })
29
30 expect(t, r.TemplateLookup(fname1Rel) != nil, true)
31 expect(t, r.TemplateLookup(fname0Rel) != nil, true)
32 expect(t, r.TemplateLookup(fnameShouldParsedRel) != nil, true)
33 expect(t, r.TemplateLookup(dirShouldNotParsedRel) == nil, true)
34 }
35
36 func TestEmbedFileSystemHTMLBasic(t *testing.T) {
37 render := New(Options{
38 Directory: "testdata/basic",
39 FileSystem: &EmbedFileSystem{
40 FS: EmbedFixtures,
41 },
42 })
43
44 var err error
45 h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
46 err = render.HTML(w, http.StatusOK, "hello", "gophers")
47 })
48
49 res := httptest.NewRecorder()
50 req, _ := http.NewRequestWithContext(ctx, "GET", "/foo", nil)
51 h.ServeHTTP(res, req)
52
53 expectNil(t, err)
54 expect(t, res.Code, 200)
55 expect(t, res.Header().Get(ContentType), ContentHTML+"; charset=UTF-8")
56 expect(t, res.Body.String(), "<h1>Hello gophers</h1>\n")
57 }