<commit_before>package mmw

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"testing"
	"time"

	metrics "github.com/armon/go-metrics"
	"github.com/bakins/go-metrics-map"
	h "github.com/bakins/test-helpers"
)

func newRequest(method, url string) *http.Request {
	req, err := http.NewRequest(method, url, nil)
	if err != nil {
		panic(err)
	}
	return req
}

func TestHandler(t *testing.T) {
	s := mapsink.New()

	c := metrics.DefaultConfig("test")
	c.EnableHostname = false
	c.EnableRuntimeMetrics = false

	m, err := metrics.New(c, s)

	h.Ok(t, err)
	handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		w.WriteHeader(200)
		fmt.Fprint(w, "Hello World\n")
	})

	mw := New(m)
	w := httptest.NewRecorder()

	mw.Handler(handler, "testing").ServeHTTP(w, newRequest("GET", "/foo"))

	h.Assert(t, w.Body.String() == "Hello World\n", "body does not match")

	v, ok := s.Get("test.testing.count")
	h.Assert(t, ok, "key not found")
	h.Assert(t, v == 1, "value is not 1")
}

func TestHandlerWrapper(t *testing.T) {
	s := mapsink.New()

	c := metrics.DefaultConfig("test")
	c.EnableHostname = false
	c.EnableRuntimeMetrics = false

	m, err := metrics.New(c, s)

	h.Ok(t, err)
	handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		w.WriteHeader(200)
		fmt.Fprint(w, "Hello World\n")
	})

	mw := New(m)
	w := httptest.NewRecorder()

	f := mw.HandlerWrapper("testing")
	f(handler).ServeHTTP(w, newRequest("GET", "/foo"))

	h.Assert(t, w.Body.String() == "Hello World\n", "body does not match")

	v, ok := s.Get("test.testing.count")
	h.Assert(t, ok, "key not found")
	h.Assert(t, v == 1, "value is not 1")
}

func ExampleMiddleware_Handler() {
	// create a sink to use
	inm := metrics.NewInmemSink(10*time.Millisecond, 50*time.Millisecond)<commit_msg>check for time metric<commit_after>package mmw

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"testing"
	"time"

	metrics "github.com/armon/go-metrics"
	"github.com/bakins/go-metrics-map"
	h "github.com/bakins/test-helpers"
)

func newRequest(method, url string) *http.Request {
	req, err := http.NewRequest(method, url, nil)
	if err != nil {
		panic(err)
	}
	return req
}

func TestHandler(t *testing.T) {
	s := mapsink.New()

	c := metrics.DefaultConfig("test")
	c.EnableHostname = false
	c.EnableRuntimeMetrics = false

	m, err := metrics.New(c, s)

	h.Ok(t, err)
	handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		w.WriteHeader(200)
		fmt.Fprint(w, "Hello World\n")
	})

	mw := New(m)
	w := httptest.NewRecorder()

	mw.Handler(handler, "testing").ServeHTTP(w, newRequest("GET", "/foo"))

	h.Assert(t, w.Body.String() == "Hello World\n", "body does not match")

	v, ok := s.Get("test.testing.count")
	h.Assert(t, ok, "key not found")
	h.Assert(t, v == 1, "value is not 1")

	v, ok = s.Get("test.testing.time")
	h.Assert(t, ok, "key not found")
}

func TestHandlerWrapper(t *testing.T) {
	s := mapsink.New()

	c := metrics.DefaultConfig("test")
	c.EnableHostname = false
	c.EnableRuntimeMetrics = false

	m, err := metrics.New(c, s)

	h.Ok(t, err)
	handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		w.WriteHeader(200)
		fmt.Fprint(w, "Hello World\n")
	})

	mw := New(m)
	w := httptest.NewRecorder()

	f := mw.HandlerWrapper("testing")
	f(handler).ServeHTTP(w, newRequest("GET", "/foo"))

	h.Assert(t, w.Body.String() == "Hello World\n", "body does not match")

	v, ok := s.Get("test.testing.count")
	h.Assert(t, ok, "key not found")
	h.Assert(t, v == 1, "value is not 1")

	v, ok = s.Get("test.testing.time")
	h.Assert(t, ok, "key not found")
}

func ExampleMiddleware_Handler() {
	// create a sink to use
	inm := metrics.NewInmemSink(10*time.Millisecond, 50*time.Millisecond)
{'fraction_non_alphanumeric': 0.18337801608579088, 'fraction_numerical': 0.007506702412868633, 'mean_word_length': 4.139118457300276, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '854656', 'n_tokens_mistral': 1540, 'n_tokens_neox': 1474, 'n_words': 326}