Skip to content

Commit

Permalink
Testing pprof's config
Browse files Browse the repository at this point in the history
  • Loading branch information
Dynom committed Jul 28, 2018
1 parent c83f6e2 commit e339116
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
44 changes: 44 additions & 0 deletions server/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,50 @@ func TestNewHTTP(t *testing.T) {
}
}

func TestConfigureProfiler(t *testing.T) {

t.Run("Testing the WriteTimeout", func(t *testing.T) {
s := TySugServer{server: &http.Server{}}
configureProfiler(s, http.NewServeMux())
if s.server.WriteTimeout < 30 {
t.Errorf("Expected a write timeout of at least 30 seconds when debugging is enabled.\n%+v", s)
}
})

t.Run("Testing if we fall back to /debug/", func(t *testing.T) {

w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/debug/pprof/", nil)

mux := http.NewServeMux()
configureProfiler(TySugServer{server: &http.Server{}}, mux)
mux.ServeHTTP(w, r)

if w.Code != 200 {
t.Error("Expected the default route to be available under /debug/")
}
})

t.Run("Testing if listen on the custom prefix", func(t *testing.T) {
const prefix = "213c06c51ed"

w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/"+prefix+"/pprof/", nil)

s := TySugServer{server: &http.Server{}}
s.profConfig.Prefix = prefix

mux := http.NewServeMux()
configureProfiler(s, mux)
mux.ServeHTTP(w, r)

if w.Code != 200 {
t.Errorf("Expected the custom route to be available under /%s/.\n%+v", prefix, w)
}
})

}

func createStubbedTySugRequest(r io.Reader) (tySugRequest, error) {
req := httptest.NewRequest(http.MethodPost, "/", r)
return getRequestFromHTTPRequest(req)
Expand Down
15 changes: 14 additions & 1 deletion server/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ func TestWithInputLimitValidator(t *testing.T) {
if before >= after {
t.Errorf("Expected a validator to have been added (Before: %d, after: %d)", before, after)
}
}

v := s.validators[len(s.validators)-1]
func TestCreateInputLimitValidator(t *testing.T) {
v := createInputLimitValidator(12)
req := tySugRequest{Input: "more than twelve"}
if err := v(req); err == nil {
t.Errorf("Expected the request to be invalid, since the input is more than 12 bytes in size %+v", err)
Expand Down Expand Up @@ -44,6 +46,17 @@ func TestWithCORSOneOrigin(t *testing.T) {
}
}

func TestWithGzipHandler(t *testing.T) {
s := TySugServer{}

hc := len(s.handlers)
WithGzipHandler()(&s)

if l := len(s.handlers); hc > l || l != 1 {
t.Errorf("Expected exactly one handler, instead I got %d.", l)
}
}

func TestWithLogger(t *testing.T) {
s := TySugServer{}

Expand Down

0 comments on commit e339116

Please sign in to comment.