diff --git a/examples/custom-instrumentation/go.mod b/examples/custom-instrumentation/go.mod index cdcdf3a..a016833 100644 --- a/examples/custom-instrumentation/go.mod +++ b/examples/custom-instrumentation/go.mod @@ -1,6 +1,6 @@ module httpbin-instrumentation -go 1.22 +go 1.22.0 require ( github.com/DataDog/datadog-go v4.8.3+incompatible diff --git a/go.mod b/go.mod index 63ccbdb..59c3732 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/mccutchen/go-httpbin/v2 -go 1.22 +go 1.22.0 diff --git a/httpbin/httpbin.go b/httpbin/httpbin.go index c3bcea0..6d04b23 100644 --- a/httpbin/httpbin.go +++ b/httpbin/httpbin.go @@ -99,9 +99,10 @@ func New(opts ...OptionFunc) *HTTPBin { } // pre-compute some configuration values and pre-render templates + tmplData := struct{ Prefix string }{Prefix: h.prefix} + h.indexHTML = mustRenderTemplate("index.html.tmpl", tmplData) + h.formsPostHTML = mustRenderTemplate("forms-post.html.tmpl", tmplData) h.statusSpecialCases = createSpecialCases(h.prefix) - h.indexHTML = h.mustRenderTemplate("index.html.tmpl") - h.formsPostHTML = h.mustRenderTemplate("forms-post.html.tmpl") // compute max Server-Sent Event count based on max request size and rough // estimate of a single event's size on the wire diff --git a/httpbin/static_assets.go b/httpbin/static_assets.go index e51efa2..c20d65e 100644 --- a/httpbin/static_assets.go +++ b/httpbin/static_assets.go @@ -24,14 +24,10 @@ func mustStaticAsset(name string) []byte { return b } -func (h *HTTPBin) mustRenderTemplate(name string) []byte { - t, err := template.New(name).Parse(string(mustStaticAsset(name))) - if err != nil { - panic(err) - } +func mustRenderTemplate(name string, data any) []byte { + t := template.Must(template.New(name).Parse(string(mustStaticAsset(name)))).Option("missingkey=error") var buf bytes.Buffer - ctx := struct{ Prefix string }{Prefix: h.prefix} - if err := t.Execute(&buf, ctx); err != nil { + if err := t.Execute(&buf, data); err != nil { panic(err) } return buf.Bytes() diff --git a/httpbin/static_assets_test.go b/httpbin/static_assets_test.go index 2213749..891f575 100644 --- a/httpbin/static_assets_test.go +++ b/httpbin/static_assets_test.go @@ -2,7 +2,8 @@ package httpbin import "testing" -// Silly test just to satisfy coverage +// Silly tests just to increase code coverage scores + func TestMustStaticAsset(t *testing.T) { defer func() { // recover from panic if one occured. Set err to nil otherwise. @@ -12,3 +13,25 @@ func TestMustStaticAsset(t *testing.T) { }() mustStaticAsset("xxxyyyzzz") } + +func TestMustRenderTemplate(t *testing.T) { + t.Run("invalid template name", func(t *testing.T) { + defer func() { + // recover from panic if one occured. Set err to nil otherwise. + if err := recover(); err == nil { + t.Fatalf("expected to recover from panic, got nil") + } + }() + mustRenderTemplate("xxxyyyzzz", nil) + }) + + t.Run("invalid template data", func(t *testing.T) { + defer func() { + // recover from panic if one occured. Set err to nil otherwise. + if err := recover(); err == nil { + t.Fatalf("expected to recover from panic, got nil") + } + }() + mustRenderTemplate("index.html.tmpl", nil) + }) +}