File tree Expand file tree Collapse file tree 5 files changed +32
-12
lines changed
examples/custom-instrumentation Expand file tree Collapse file tree 5 files changed +32
-12
lines changed Original file line number Diff line number Diff line change 1
1
module httpbin-instrumentation
2
2
3
- go 1.22
3
+ go 1.22.0
4
4
5
5
require (
6
6
github.com/DataDog/datadog-go v4.8.3+incompatible
Original file line number Diff line number Diff line change 1
1
module github.com/mccutchen/go-httpbin/v2
2
2
3
- go 1.22
3
+ go 1.22.0
Original file line number Diff line number Diff line change @@ -99,9 +99,10 @@ func New(opts ...OptionFunc) *HTTPBin {
99
99
}
100
100
101
101
// 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 )
102
105
h .statusSpecialCases = createSpecialCases (h .prefix )
103
- h .indexHTML = h .mustRenderTemplate ("index.html.tmpl" )
104
- h .formsPostHTML = h .mustRenderTemplate ("forms-post.html.tmpl" )
105
106
106
107
// compute max Server-Sent Event count based on max request size and rough
107
108
// estimate of a single event's size on the wire
Original file line number Diff line number Diff line change @@ -24,14 +24,10 @@ func mustStaticAsset(name string) []byte {
24
24
return b
25
25
}
26
26
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" )
32
29
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 {
35
31
panic (err )
36
32
}
37
33
return buf .Bytes ()
Original file line number Diff line number Diff line change @@ -2,7 +2,8 @@ package httpbin
2
2
3
3
import "testing"
4
4
5
- // Silly test just to satisfy coverage
5
+ // Silly tests just to increase code coverage scores
6
+
6
7
func TestMustStaticAsset (t * testing.T ) {
7
8
defer func () {
8
9
// recover from panic if one occured. Set err to nil otherwise.
@@ -12,3 +13,25 @@ func TestMustStaticAsset(t *testing.T) {
12
13
}()
13
14
mustStaticAsset ("xxxyyyzzz" )
14
15
}
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
+ }
You can’t perform that action at this time.
0 commit comments