Skip to content

Commit

Permalink
Treat zero as invalid values for limit if passed via REST (#158)
Browse files Browse the repository at this point in the history
* Treat 0 as invalid values if requested via REST

* UT fixes
  • Loading branch information
Evgeniy-L authored Mar 28, 2019
1 parent ab24c49 commit d10b6c4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
4 changes: 2 additions & 2 deletions query/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
25 changes: 17 additions & 8 deletions query/pagination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down

0 comments on commit d10b6c4

Please sign in to comment.