Skip to content

Commit

Permalink
refactor: small tweak to template rendering helpers (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
mccutchen authored Sep 13, 2024
1 parent 0e8cd05 commit 61d7feb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
2 changes: 1 addition & 1 deletion examples/custom-instrumentation/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module httpbin-instrumentation

go 1.22
go 1.22.0

require (
github.com/DataDog/datadog-go v4.8.3+incompatible
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/mccutchen/go-httpbin/v2

go 1.22
go 1.22.0
5 changes: 3 additions & 2 deletions httpbin/httpbin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 3 additions & 7 deletions httpbin/static_assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
25 changes: 24 additions & 1 deletion httpbin/static_assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
})
}

0 comments on commit 61d7feb

Please sign in to comment.