2020-07-22 00:15:56 +01:00
|
|
|
// Copyright 2018 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package pprof
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-09-15 13:06:59 +01:00
|
|
|
"io"
|
2020-07-22 00:15:56 +01:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"runtime/pprof"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestDescriptions checks that the profile names under runtime/pprof package
|
|
|
|
// have a key in the description map.
|
|
|
|
func TestDescriptions(t *testing.T) {
|
|
|
|
for _, p := range pprof.Profiles() {
|
|
|
|
_, ok := profileDescriptions[p.Name()]
|
|
|
|
if ok != true {
|
|
|
|
t.Errorf("%s does not exist in profileDescriptions map\n", p.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandlers(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
path string
|
|
|
|
handler http.HandlerFunc
|
|
|
|
statusCode int
|
|
|
|
contentType string
|
|
|
|
contentDisposition string
|
|
|
|
resp []byte
|
|
|
|
}{
|
|
|
|
{"/debug/pprof/<script>scripty<script>", Index, http.StatusNotFound, "text/plain; charset=utf-8", "", []byte("Unknown profile\n")},
|
|
|
|
{"/debug/pprof/heap", Index, http.StatusOK, "application/octet-stream", `attachment; filename="heap"`, nil},
|
|
|
|
{"/debug/pprof/heap?debug=1", Index, http.StatusOK, "text/plain; charset=utf-8", "", nil},
|
|
|
|
{"/debug/pprof/cmdline", Cmdline, http.StatusOK, "text/plain; charset=utf-8", "", nil},
|
|
|
|
{"/debug/pprof/profile?seconds=1", Profile, http.StatusOK, "application/octet-stream", `attachment; filename="profile"`, nil},
|
|
|
|
{"/debug/pprof/symbol", Symbol, http.StatusOK, "text/plain; charset=utf-8", "", nil},
|
|
|
|
{"/debug/pprof/trace", Trace, http.StatusOK, "application/octet-stream", `attachment; filename="trace"`, nil},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.path, func(t *testing.T) {
|
|
|
|
req := httptest.NewRequest("GET", "http://example.com"+tc.path, nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
tc.handler(w, req)
|
|
|
|
|
|
|
|
resp := w.Result()
|
|
|
|
if got, want := resp.StatusCode, tc.statusCode; got != want {
|
|
|
|
t.Errorf("status code: got %d; want %d", got, want)
|
|
|
|
}
|
|
|
|
|
2022-09-15 13:06:59 +01:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
2020-07-22 00:15:56 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("when reading response body, expected non-nil err; got %v", err)
|
|
|
|
}
|
|
|
|
if got, want := resp.Header.Get("X-Content-Type-Options"), "nosniff"; got != want {
|
|
|
|
t.Errorf("X-Content-Type-Options: got %q; want %q", got, want)
|
|
|
|
}
|
|
|
|
if got, want := resp.Header.Get("Content-Type"), tc.contentType; got != want {
|
|
|
|
t.Errorf("Content-Type: got %q; want %q", got, want)
|
|
|
|
}
|
|
|
|
if got, want := resp.Header.Get("Content-Disposition"), tc.contentDisposition; got != want {
|
|
|
|
t.Errorf("Content-Disposition: got %q; want %q", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusOK {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if got, want := resp.Header.Get("X-Go-Pprof"), "1"; got != want {
|
|
|
|
t.Errorf("X-Go-Pprof: got %q; want %q", got, want)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(body, tc.resp) {
|
|
|
|
t.Errorf("response: got %q; want %q", body, tc.resp)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|