github.com/epfl-dcsl/gotee@v0.0.0-20200909122901-014b35f5e5e9/src/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  	"testing"
    13  )
    14  
    15  func TestHandlers(t *testing.T) {
    16  	testCases := []struct {
    17  		path               string
    18  		handler            http.HandlerFunc
    19  		statusCode         int
    20  		contentType        string
    21  		contentDisposition string
    22  		resp               []byte
    23  	}{
    24  		{"/debug/pprof/<script>scripty<script>", Index, http.StatusNotFound, "text/plain; charset=utf-8", "", []byte("Unknown profile\n")},
    25  		{"/debug/pprof/heap", Index, http.StatusOK, "application/octet-stream", `attachment; filename="heap"`, nil},
    26  		{"/debug/pprof/heap?debug=1", Index, http.StatusOK, "text/plain; charset=utf-8", "", nil},
    27  		{"/debug/pprof/cmdline", Cmdline, http.StatusOK, "text/plain; charset=utf-8", "", nil},
    28  		{"/debug/pprof/profile?seconds=1", Profile, http.StatusOK, "application/octet-stream", `attachment; filename="profile"`, nil},
    29  		{"/debug/pprof/symbol", Symbol, http.StatusOK, "text/plain; charset=utf-8", "", nil},
    30  		{"/debug/pprof/trace", Trace, http.StatusOK, "application/octet-stream", `attachment; filename="trace"`, nil},
    31  	}
    32  	for _, tc := range testCases {
    33  		t.Run(tc.path, func(t *testing.T) {
    34  			req := httptest.NewRequest("GET", "http://example.com"+tc.path, nil)
    35  			w := httptest.NewRecorder()
    36  			tc.handler(w, req)
    37  
    38  			resp := w.Result()
    39  			if got, want := resp.StatusCode, tc.statusCode; got != want {
    40  				t.Errorf("status code: got %d; want %d", got, want)
    41  			}
    42  
    43  			body, err := ioutil.ReadAll(resp.Body)
    44  			if err != nil {
    45  				t.Errorf("when reading response body, expected non-nil err; got %v", err)
    46  			}
    47  			if got, want := resp.Header.Get("X-Content-Type-Options"), "nosniff"; got != want {
    48  				t.Errorf("X-Content-Type-Options: got %q; want %q", got, want)
    49  			}
    50  			if got, want := resp.Header.Get("Content-Type"), tc.contentType; got != want {
    51  				t.Errorf("Content-Type: got %q; want %q", got, want)
    52  			}
    53  			if got, want := resp.Header.Get("Content-Disposition"), tc.contentDisposition; got != want {
    54  				t.Errorf("Content-Disposition: got %q; want %q", got, want)
    55  			}
    56  
    57  			if resp.StatusCode == http.StatusOK {
    58  				return
    59  			}
    60  			if got, want := resp.Header.Get("X-Go-Pprof"), "1"; got != want {
    61  				t.Errorf("X-Go-Pprof: got %q; want %q", got, want)
    62  			}
    63  			if !bytes.Equal(body, tc.resp) {
    64  				t.Errorf("response: got %q; want %q", body, tc.resp)
    65  			}
    66  		})
    67  	}
    68  
    69  }