Skip to content

Commit 61d7feb

Browse files
authored
refactor: small tweak to template rendering helpers (#182)
1 parent 0e8cd05 commit 61d7feb

File tree

5 files changed

+32
-12
lines changed

5 files changed

+32
-12
lines changed

examples/custom-instrumentation/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module httpbin-instrumentation
22

3-
go 1.22
3+
go 1.22.0
44

55
require (
66
github.com/DataDog/datadog-go v4.8.3+incompatible

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/mccutchen/go-httpbin/v2
22

3-
go 1.22
3+
go 1.22.0

httpbin/httpbin.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,10 @@ func New(opts ...OptionFunc) *HTTPBin {
9999
}
100100

101101
// pre-compute some configuration values and pre-render templates
102+
tmplData := struct{ Prefix string }{Prefix: h.prefix}
103+
h.indexHTML = mustRenderTemplate("index.html.tmpl", tmplData)
104+
h.formsPostHTML = mustRenderTemplate("forms-post.html.tmpl", tmplData)
102105
h.statusSpecialCases = createSpecialCases(h.prefix)
103-
h.indexHTML = h.mustRenderTemplate("index.html.tmpl")
104-
h.formsPostHTML = h.mustRenderTemplate("forms-post.html.tmpl")
105106

106107
// compute max Server-Sent Event count based on max request size and rough
107108
// estimate of a single event's size on the wire

httpbin/static_assets.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,10 @@ func mustStaticAsset(name string) []byte {
2424
return b
2525
}
2626

27-
func (h *HTTPBin) mustRenderTemplate(name string) []byte {
28-
t, err := template.New(name).Parse(string(mustStaticAsset(name)))
29-
if err != nil {
30-
panic(err)
31-
}
27+
func mustRenderTemplate(name string, data any) []byte {
28+
t := template.Must(template.New(name).Parse(string(mustStaticAsset(name)))).Option("missingkey=error")
3229
var buf bytes.Buffer
33-
ctx := struct{ Prefix string }{Prefix: h.prefix}
34-
if err := t.Execute(&buf, ctx); err != nil {
30+
if err := t.Execute(&buf, data); err != nil {
3531
panic(err)
3632
}
3733
return buf.Bytes()

httpbin/static_assets_test.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ package httpbin
22

33
import "testing"
44

5-
// Silly test just to satisfy coverage
5+
// Silly tests just to increase code coverage scores
6+
67
func TestMustStaticAsset(t *testing.T) {
78
defer func() {
89
// recover from panic if one occured. Set err to nil otherwise.
@@ -12,3 +13,25 @@ func TestMustStaticAsset(t *testing.T) {
1213
}()
1314
mustStaticAsset("xxxyyyzzz")
1415
}
16+
17+
func TestMustRenderTemplate(t *testing.T) {
18+
t.Run("invalid template name", func(t *testing.T) {
19+
defer func() {
20+
// recover from panic if one occured. Set err to nil otherwise.
21+
if err := recover(); err == nil {
22+
t.Fatalf("expected to recover from panic, got nil")
23+
}
24+
}()
25+
mustRenderTemplate("xxxyyyzzz", nil)
26+
})
27+
28+
t.Run("invalid template data", func(t *testing.T) {
29+
defer func() {
30+
// recover from panic if one occured. Set err to nil otherwise.
31+
if err := recover(); err == nil {
32+
t.Fatalf("expected to recover from panic, got nil")
33+
}
34+
}()
35+
mustRenderTemplate("index.html.tmpl", nil)
36+
})
37+
}

0 commit comments

Comments
 (0)