Skip to content

Commit

Permalink
removed code duplication in hasndler_test
Browse files Browse the repository at this point in the history
  • Loading branch information
soundvibe committed Nov 12, 2020
1 parent 5814642 commit a7d6696
Showing 1 changed file with 16 additions and 27 deletions.
43 changes: 16 additions & 27 deletions src/query/api/v1/httpd/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,11 @@ func init() {
testWorkerPool.Init()
}

type assertFn func(t *testing.T, prev http.Handler)

type customHandler struct {
t *testing.T
assertFn assertFn
}

func (h *customHandler) Route() string { return "/custom" }
Expand All @@ -389,31 +392,7 @@ func (h *customHandler) Handler(
) (http.Handler, error) {
assert.Equal(h.t, "z", string(opts.TagOptions().MetricName()))
fn := func(w http.ResponseWriter, r *http.Request) {
if prev != nil {
assert.Fail(h.t, "Should not shadow already existing handler")
}
_, err := w.Write([]byte("success!"))
require.NoError(h.t, err)
}

return http.HandlerFunc(fn), nil
}

type customHandlerOverride struct {
t *testing.T
}

func (h *customHandlerOverride) Route() string { return "/custom" }
func (h *customHandlerOverride) Methods() []string { return []string{http.MethodGet} }
func (h *customHandlerOverride) Handler(
opts options.HandlerOptions,
prev http.Handler,
) (http.Handler, error) {
assert.Equal(h.t, "z", string(opts.TagOptions().MetricName()))
fn := func(w http.ResponseWriter, r *http.Request) {
if prev == nil {
assert.Fail(h.t, "Should shadow already existing handler")
}
h.assertFn(h.t, prev)
_, err := w.Write([]byte("success!"))
require.NoError(h.t, err)
}
Expand All @@ -438,8 +417,18 @@ func TestCustomRoutes(t *testing.T) {
graphite.M3WrappedStorageOptions{}, testM3DBOpts)

require.NoError(t, err)
custom := &customHandler{t: t}
custom2 := &customHandlerOverride{t: t}
custom := &customHandler{
t: t,
assertFn: func(t *testing.T, prev http.Handler) {
assert.True(t, prev == nil, "Should not shadow already existing handler")
},
}
custom2 := &customHandler{
t: t,
assertFn: func(t *testing.T, prev http.Handler) {
assert.False(t, prev == nil, "Should shadow already existing handler")
},
}
handler := NewHandler(opts, custom, custom2)
require.NoError(t, err, "unable to setup handler")
err = handler.RegisterRoutes()
Expand Down

0 comments on commit a7d6696

Please sign in to comment.