func TestMiddleware_writeHeaderNotCalled(t *testing.T) {
	metrics := []*Metric{
		{
			Name:     "sql-1",
			Duration: 100 * time.Millisecond,
			Desc:     "MySQL; lookup Server",
		},
	}
	ts := httptest.NewServer(Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		h := FromContext(r.Context())
		if h == nil {
			t.Fatal("expected *Header to be present in context")
		}
		h.Metrics = metrics
		w.Write([]byte(responseBody))
	}), nil))
	defer ts.Close()
	res, err := http.Get(ts.URL)
	if err != nil {
		t.Fatal(err)
	}
	_, present := map[string][]string(res.Header)[HeaderKey]
	if !present {
		t.Fatal("expected header to be present")
	}
	expected := (&Header{Metrics: metrics}).String()
	actual := res.Header.Get(HeaderKey)
	if actual != expected {
		t.Fatalf("got wrong value, expected != actual: %q != %q", expected, actual)
	}
}
We need to test this separately since the httptest.ResponseRecorder
doesn't properly reflect that headers can't be set after writing data,
so we have to use a real server.
package servertiming
import (
	"net/http"
	"net/http/httptest"
	"testing"
	"time"
)
const (
	responseBody   = "response"
	responseStatus = http.StatusCreated
)
func TestMiddleware(t *testing.T) {
	cases := []struct {
		Name             string
		Opts             *MiddlewareOpts
		Metrics          []*Metric
		SkipWriteHeaders bool
		Expected         bool
	}{
		{
			Name:     "nil metrics",
			Metrics:  nil,
			Expected: false,
		},
		{
			Name:     "empty metrics",
			Metrics:  []*Metric{},
			Expected: false,
		},
		{
			Name: "single metric disable headers option",
			Opts: &MiddlewareOpts{DisableHeaders: true},
			Metrics: []*Metric{
				{
					Name:     "sql-1",
					Duration: 100 * time.Millisecond,
					Desc:     "MySQL; lookup Server",
				},
			},
			Expected: false,
		},
		{
			Name: "single metric",
			Metrics: []*Metric{
				{
					Name:     "sql-1",
					Duration: 100 * time.Millisecond,
					Desc:     "MySQL; lookup Server",
				},
			},
			Expected: true,
		},
		{
			Name: "single metric without invoking WriteHeaders in handler",
			Metrics: []*Metric{
				{
					Name:     "sql-1",
					Duration: 100 * time.Millisecond,
					Desc:     "MySQL; lookup Server",
				},
			},
			Expected:         true,
			SkipWriteHeaders: true,
		},
	}
	for _, tt := range cases {
		t.Run(tt.Name, func(t *testing.T) {
			r := httptest.NewRequest("GET", "/", nil)
			rec := httptest.NewRecorder()
			handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				h := FromContext(r.Context())
				if h == nil {
					t.Fatal("expected *Header to be present in context")
				}
				h.Metrics = tt.Metrics
				if !tt.SkipWriteHeaders {
					w.WriteHeader(responseStatus)
				}
				w.Write([]byte(responseBody))
			})
			Middleware(handler, tt.Opts).ServeHTTP(rec, r)
			_, present := map[string][]string(rec.Header())[HeaderKey]
			if present != tt.Expected {
				t.Fatalf("expected header to be present: %v, but wasn't", tt.Expected)
			}
			expected := (&Header{Metrics: tt.Metrics}).String()
			if tt.Opts != nil && tt.Opts.DisableHeaders == true {
				expected = ""
			}
			actual := rec.Header().Get(HeaderKey)
			if actual != expected {
				t.Fatalf("got wrong value, expected != actual: %q != %q", expected, actual)
			}
			expectedStatus := responseStatus
			if tt.SkipWriteHeaders {
				expectedStatus = http.StatusOK
			}
			if actualStatus := rec.Result().StatusCode; expectedStatus != actualStatus {
				t.Fatalf("got unexpected status code value, expected != actual: %q != %q", expectedStatus, actualStatus)
			}
			if responseBody != rec.Body.String() {
				t.Fatalf("got unexpected body, expected != actual: %q != %q", responseBody, rec.Body.String())
			}
		})
	}
}