pprof_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2018 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package pprof
  5. import (
  6. "bytes"
  7. "io"
  8. "net/http"
  9. "net/http/httptest"
  10. "runtime/pprof"
  11. "testing"
  12. )
  13. // TestDescriptions checks that the profile names under runtime/pprof package
  14. // have a key in the description map.
  15. func TestDescriptions(t *testing.T) {
  16. for _, p := range pprof.Profiles() {
  17. _, ok := profileDescriptions[p.Name()]
  18. if ok != true {
  19. t.Errorf("%s does not exist in profileDescriptions map\n", p.Name())
  20. }
  21. }
  22. }
  23. func TestHandlers(t *testing.T) {
  24. testCases := []struct {
  25. path string
  26. handler http.HandlerFunc
  27. statusCode int
  28. contentType string
  29. contentDisposition string
  30. resp []byte
  31. }{
  32. {"/debug/pprof/<script>scripty<script>", Index, http.StatusNotFound, "text/plain; charset=utf-8", "", []byte("Unknown profile\n")},
  33. {"/debug/pprof/heap", Index, http.StatusOK, "application/octet-stream", `attachment; filename="heap"`, nil},
  34. {"/debug/pprof/heap?debug=1", Index, http.StatusOK, "text/plain; charset=utf-8", "", nil},
  35. {"/debug/pprof/cmdline", Cmdline, http.StatusOK, "text/plain; charset=utf-8", "", nil},
  36. {"/debug/pprof/profile?seconds=1", Profile, http.StatusOK, "application/octet-stream", `attachment; filename="profile"`, nil},
  37. {"/debug/pprof/symbol", Symbol, http.StatusOK, "text/plain; charset=utf-8", "", nil},
  38. {"/debug/pprof/trace", Trace, http.StatusOK, "application/octet-stream", `attachment; filename="trace"`, nil},
  39. }
  40. for _, tc := range testCases {
  41. t.Run(tc.path, func(t *testing.T) {
  42. req := httptest.NewRequest("GET", "http://example.com"+tc.path, nil)
  43. w := httptest.NewRecorder()
  44. tc.handler(w, req)
  45. resp := w.Result()
  46. if got, want := resp.StatusCode, tc.statusCode; got != want {
  47. t.Errorf("status code: got %d; want %d", got, want)
  48. }
  49. body, err := io.ReadAll(resp.Body)
  50. if err != nil {
  51. t.Errorf("when reading response body, expected non-nil err; got %v", err)
  52. }
  53. if got, want := resp.Header.Get("X-Content-Type-Options"), "nosniff"; got != want {
  54. t.Errorf("X-Content-Type-Options: got %q; want %q", got, want)
  55. }
  56. if got, want := resp.Header.Get("Content-Type"), tc.contentType; got != want {
  57. t.Errorf("Content-Type: got %q; want %q", got, want)
  58. }
  59. if got, want := resp.Header.Get("Content-Disposition"), tc.contentDisposition; got != want {
  60. t.Errorf("Content-Disposition: got %q; want %q", got, want)
  61. }
  62. if resp.StatusCode == http.StatusOK {
  63. return
  64. }
  65. if got, want := resp.Header.Get("X-Go-Pprof"), "1"; got != want {
  66. t.Errorf("X-Go-Pprof: got %q; want %q", got, want)
  67. }
  68. if !bytes.Equal(body, tc.resp) {
  69. t.Errorf("response: got %q; want %q", body, tc.resp)
  70. }
  71. })
  72. }
  73. }