github.com/guyezi/gofrontend@v0.0.0-20200228202240-7a62a49e62c0/libgo/go/net/http/pprof/pprof_test.go (about)

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