diff --git a/command/agent/stats_endpoint.go b/command/agent/stats_endpoint.go index 5dabdf9f9..0cf5b8dc5 100644 --- a/command/agent/stats_endpoint.go +++ b/command/agent/stats_endpoint.go @@ -6,6 +6,10 @@ import ( "strconv" ) +const ( + invalidSinceErrPrefix = "can't read the since query parameter" +) + func (s *HTTPServer) ClientStatsRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) { if s.agent.client == nil { return nil, clientNotRunning @@ -18,7 +22,7 @@ func (s *HTTPServer) ClientStatsRequest(resp http.ResponseWriter, req *http.Requ ts = true since, err = strconv.Atoi(sinceTime) if err != nil { - return nil, CodedError(400, fmt.Sprintf("can't read the since query parameter: %v", err)) + return nil, CodedError(400, fmt.Sprintf("%s: %v", invalidSinceErrPrefix, err)) } } diff --git a/command/agent/stats_endpoint_test.go b/command/agent/stats_endpoint_test.go new file mode 100644 index 000000000..6f9649ab6 --- /dev/null +++ b/command/agent/stats_endpoint_test.go @@ -0,0 +1,23 @@ +package agent + +import ( + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +func TestClientStatsRequest(t *testing.T) { + httpTest(t, nil, func(s *TestServer) { + req, err := http.NewRequest("GET", "/v1/client/stats/?since=foo", nil) + if err != nil { + t.Fatalf("err: %v", err) + } + + respW := httptest.NewRecorder() + _, err = s.Server.ClientStatsRequest(respW, req) + if !strings.ContainsAny(err.Error(), invalidSinceErrPrefix) { + t.Fatalf("unexpected err: %v", err) + } + }) +}