fixed a bug in pagination
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
@@ -87,3 +88,39 @@ func TestPagination_CursorBoundToChat(t *testing.T) {
|
|||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPagination_IncompleteCursor(t *testing.T) {
|
||||||
|
r := newTestRig(t)
|
||||||
|
alice, _, _, chat := bootstrap(r)
|
||||||
|
r.send(alice, chat.ID, "m0")
|
||||||
|
|
||||||
|
enc := func(s string) string { return base64.RawURLEncoding.EncodeToString([]byte(s)) }
|
||||||
|
msgScope := `"s":"chat:` + chat.ID + `"`
|
||||||
|
chatsScope := `"s":"chats:` + alice.ID + `"`
|
||||||
|
ts := `"t":"2026-01-01T00:00:00Z"`
|
||||||
|
id := `"i":"` + chat.ID + `"`
|
||||||
|
|
||||||
|
cases := map[string]string{
|
||||||
|
"только scope": enc(`{` + msgScope + `}`),
|
||||||
|
"без i": enc(`{` + ts + `,` + msgScope + `}`),
|
||||||
|
"без t": enc(`{` + id + `,` + msgScope + `}`),
|
||||||
|
"i не UUID": enc(`{` + ts + `,"i":"zzz",` + msgScope + `}`),
|
||||||
|
"нулевое t": enc(`{"t":"0001-01-01T00:00:00Z",` + id + `,` + msgScope + `}`),
|
||||||
|
"лишнее поле": enc(`{` + ts + `,` + id + `,` + msgScope + `,"x":1}`),
|
||||||
|
"данные после объекта": enc(`{` + ts + `,` + id + `,` + msgScope + `}}`),
|
||||||
|
"base64 с паддингом": base64.URLEncoding.EncodeToString([]byte(`{` + ts + `,` + id + `,` + msgScope + `}`)),
|
||||||
|
"пустой объект": enc(`{}`),
|
||||||
|
"null": enc(`null`),
|
||||||
|
}
|
||||||
|
for name, cur := range cases {
|
||||||
|
resp := r.do(http.MethodGet, "/api/v1/chats/"+chat.ID+"/messages?cursor="+url.QueryEscape(cur), alice.ID, nil, nil)
|
||||||
|
if resp.StatusCode != http.StatusBadRequest {
|
||||||
|
t.Errorf("messages, %s: ожидался 400, получен %d", name, resp.StatusCode)
|
||||||
|
}
|
||||||
|
resp.Body.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
resp := r.do(http.MethodGet, "/api/v1/chats?cursor="+url.QueryEscape(enc(`{`+chatsScope+`}`)), alice.ID, nil, nil)
|
||||||
|
expectStatus(t, resp, http.StatusBadRequest)
|
||||||
|
resp.Body.Close()
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Page[T any] struct {
|
type Page[T any] struct {
|
||||||
@@ -51,5 +53,15 @@ func DecodeCursor(s string) (Cursor, error) {
|
|||||||
if err := json.Unmarshal(raw, &c); err != nil {
|
if err := json.Unmarshal(raw, &c); err != nil {
|
||||||
return Cursor{}, ErrInvalidArgument
|
return Cursor{}, ErrInvalidArgument
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if c.CreatedAt.IsZero() || c.Scope == "" {
|
||||||
|
return Cursor{}, ErrInvalidArgument
|
||||||
|
}
|
||||||
|
if _, err := uuid.Parse(c.ID); err != nil || len(c.ID) != 36 {
|
||||||
|
return Cursor{}, ErrInvalidArgument
|
||||||
|
}
|
||||||
|
if EncodeCursor(c) != s {
|
||||||
|
return Cursor{}, ErrInvalidArgument
|
||||||
|
}
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user