From 0df55b6d2fd917ac7f4fd05d129cddb4b0f4a90b Mon Sep 17 00:00:00 2001 From: Philip O'Toole Date: Mon, 11 Dec 2023 13:35:01 -0500 Subject: [PATCH] Fix all HTTP tests --- http/query_params.go | 13 ++++++++++--- http/service_test.go | 33 +++++++++++++++++++++++---------- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/http/query_params.go b/http/query_params.go index 9151ac64..cee0ccc8 100644 --- a/http/query_params.go +++ b/http/query_params.go @@ -1,6 +1,7 @@ package http import ( + "fmt" "net/http" "net/url" "strconv" @@ -29,7 +30,7 @@ func NewQueryParams(r *http.Request) (QueryParams, error) { if ok { _, err := time.ParseDuration(t) if err != nil { - return nil, err + return nil, fmt.Errorf("%s is not a valid duration", k) } } } @@ -37,7 +38,13 @@ func NewQueryParams(r *http.Request) (QueryParams, error) { if ok { _, err := strconv.Atoi(sz) if err != nil { - return nil, err + return nil, fmt.Errorf("chunk_kb is not an integer") + } + } + q, ok := qp["q"] + if ok { + if q == "" { + return nil, fmt.Errorf("query parameter not set") } } return qp, nil @@ -168,7 +175,7 @@ func (qp QueryParams) Timeout(def time.Duration) time.Duration { // Version returns the requested version. func (qp QueryParams) Version() string { - return qp["version"] + return qp["ver"] } // HasKey returns true if the given key is present in the query parameters. diff --git a/http/service_test.go b/http/service_test.go index c846f33e..7c47c4a2 100644 --- a/http/service_test.go +++ b/http/service_test.go @@ -1199,11 +1199,11 @@ func Test_timeoutVersionPrettyQueryParam(t *testing.T) { defStr := "10s" def := mustParseDuration(defStr) tests := []struct { - u string - dur string - ver string - pretty bool - err bool + u string + dur string + ver string + pretty bool + parseErr bool }{ { u: "http://localhost:4001/nodes?pretty&timeout=5s&ver=2", @@ -1235,8 +1235,18 @@ func Test_timeoutVersionPrettyQueryParam(t *testing.T) { ver: "666", }, { - u: "http://localhost:4001/nodes?timeout=zdfjkh", - err: true, + u: "http://localhost:4001/nodes?timeout=zdfjkh", + parseErr: true, + }, + { + u: "http://localhost:4001/db/load?chunk_kb=aaaa", + dur: defStr, + parseErr: true, + }, + { + u: "http://localhost:4001/db/query?q=", + dur: defStr, + parseErr: true, }, } @@ -1248,14 +1258,17 @@ func Test_timeoutVersionPrettyQueryParam(t *testing.T) { } qp, err := NewQueryParams(req) if err != nil { - t.Fatalf("failed to parse query params: %s", err) + if !tt.parseErr { + t.Fatalf(" unexpectedly failed to parse query params on test %d: %s", i, err) + } + continue } if got, exp := qp.Timeout(def), mustParseDuration(tt.dur); got != exp { - t.Fatalf("got wrong timeout, expected %s, got %s", exp, got) + t.Fatalf("got wrong timeout on test %d, expected %s, got %s", i, exp, got) } if got, exp := qp.Version(), tt.ver; got != exp { - t.Fatalf("got wrong version, expected %s, got %s", exp, got) + t.Fatalf("got wrong version on test %d, expected %s, got %s", i, exp, got) } if got, exp := qp.Pretty(), tt.pretty; got != exp { t.Fatalf("got wrong pretty on test %d, expected %t, got %t", i, exp, got)