knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/observability/runtime/pprof_test.go (about) 1 /* 2 Copyright 2025 The Knative Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package runtime 18 19 import ( 20 "net/http" 21 "net/http/httptest" 22 "testing" 23 ) 24 25 func TestNewProfilingHandler(t *testing.T) { 26 h := NewProfilingHandler() 27 28 w := httptest.NewRecorder() 29 r, _ := http.NewRequest(http.MethodGet, "/debug/pprof/", nil) 30 h.ServeHTTP(w, r) 31 32 resp := w.Result() 33 if got, want := resp.StatusCode, http.StatusNotFound; got != want { 34 t.Errorf("unexpected status code %d want: %d", got, want) 35 } 36 37 w = httptest.NewRecorder() 38 h.SetEnabled(true) 39 h.ServeHTTP(w, r) 40 41 resp = w.Result() 42 if got, want := resp.StatusCode, http.StatusOK; got != want { 43 t.Errorf("unexpected status code %d want: %d", got, want) 44 } 45 } 46 47 func TestNewProfilingServerPort(t *testing.T) { 48 s := NewProfilingServer() 49 50 if got, want := s.Server.Addr, ":8008"; got != want { 51 t.Errorf("uexpected server port %q, want: %q", got, want) 52 } 53 54 t.Setenv(ProfilingPortEnvKey, "8181") 55 s = NewProfilingServer() 56 57 if got, want := s.Server.Addr, ":8181"; got != want { 58 t.Errorf("uexpected server port %q, want: %q", got, want) 59 } 60 }