From d10b6c4b87d43a7802983a938998b53f7c424dc4 Mon Sep 17 00:00:00 2001 From: Evgeniy Litvin Date: Thu, 28 Mar 2019 23:22:47 +0300 Subject: [PATCH] Treat zero as invalid values for limit if passed via REST (#158) * Treat 0 as invalid values if requested via REST * UT fixes --- query/pagination.go | 4 ++-- query/pagination_test.go | 25 +++++++++++++++++-------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/query/pagination.go b/query/pagination.go index 80f9c8fe..aa6495e7 100644 --- a/query/pagination.go +++ b/query/pagination.go @@ -20,8 +20,8 @@ func ParsePagination(limit, offset, ptoken string) (*Pagination, error) { if limit != "" { if u, err := strconv.ParseInt(limit, 10, 32); err != nil { return nil, fmt.Errorf("pagination: limit - %s", err.(*strconv.NumError).Err) - } else if u < 0 { - return nil, fmt.Errorf("pagination: limit - negative value") + } else if u <= 0 { + return nil, fmt.Errorf("pagination: limit must be a positive value") } else { p.Limit = int32(u) } diff --git a/query/pagination_test.go b/query/pagination_test.go index 59f42f4f..18ff106e 100644 --- a/query/pagination_test.go +++ b/query/pagination_test.go @@ -17,14 +17,23 @@ func TestParsePagination(t *testing.T) { // negative limit _, err = ParsePagination("-1", "0", "ptoken") if err == nil { - t.Error("unexpected nil error - expected: pagination: limit - negative value") + t.Error("unexpected nil error - expected: pagination: limit must be a positive value") } - if err.Error() != "pagination: limit - negative value" { - t.Errorf("invalid error: %s - expected: pagination: limit - negative value", err) + if err.Error() != "pagination: limit must be a positive value" { + t.Errorf("invalid error: %s - expected: pagination: limit must be a positive value", err) + } + + // zero limit + _, err = ParsePagination("0", "0", "ptoken") + if err == nil { + t.Error("unexpected nil error - expected: pagination: limit must be a positive value") + } + if err.Error() != "pagination: limit must be a positive value" { + t.Errorf("invalid error: %s - expected: pagination: limit must be a positive value", err) } // invalid offset - _, err = ParsePagination("0", "0w", "ptoken") + _, err = ParsePagination("", "0w", "ptoken") if err == nil { t.Error("unexpected nil error - expected: pagination: offset - invalid syntax") } @@ -33,7 +42,7 @@ func TestParsePagination(t *testing.T) { } // negative offset - _, err = ParsePagination("0", "-1", "ptoken") + _, err = ParsePagination("", "-1", "ptoken") if err == nil { t.Error("unexpected nil error - expected: pagination: offset - negative value") } @@ -42,7 +51,7 @@ func TestParsePagination(t *testing.T) { } // null offset - p, err := ParsePagination("0", "null", "ptoken") + p, err := ParsePagination("", "null", "ptoken") if err != nil { t.Errorf("unexpected error: %v", err) } @@ -51,14 +60,14 @@ func TestParsePagination(t *testing.T) { } // first page - p, err = ParsePagination("0", "0", "ptoken") + p, err = ParsePagination("", "0", "ptoken") if err != nil { t.Errorf("unexpected error: %s", err) } if !p.FirstPage() { t.Errorf("invalid value of first page: %v - expected: true", p.FirstPage()) } - p, err = ParsePagination("0", "100", "null") + p, err = ParsePagination("", "100", "null") if err != nil { t.Errorf("unexpected error: %s", err) }