2023-04-10 20:39:49 +00:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/multiformats/go-multiaddr"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/waku-org/go-waku/tests"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/node"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetMessages(t *testing.T) {
|
|
|
|
|
|
|
|
db := MemoryDB(t)
|
|
|
|
|
|
|
|
node1, err := node.New(node.WithWakuStore(), node.WithMessageProvider(db))
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = node1.Start(context.Background())
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer node1.Stop()
|
|
|
|
|
|
|
|
topic1 := "1"
|
|
|
|
pubsubTopic1 := "topic1"
|
|
|
|
|
2023-04-19 20:54:33 +00:00
|
|
|
now := utils.GetUnixEpoch()
|
|
|
|
msg1 := tests.CreateWakuMessage(topic1, now+1)
|
|
|
|
msg2 := tests.CreateWakuMessage(topic1, now+2)
|
|
|
|
msg3 := tests.CreateWakuMessage(topic1, now+3)
|
2023-04-10 20:39:49 +00:00
|
|
|
|
2023-05-05 09:49:15 +00:00
|
|
|
node1.Broadcaster().Submit(protocol.NewEnvelope(msg1, utils.GetUnixEpoch(), pubsubTopic1))
|
|
|
|
node1.Broadcaster().Submit(protocol.NewEnvelope(msg2, utils.GetUnixEpoch(), pubsubTopic1))
|
|
|
|
node1.Broadcaster().Submit(protocol.NewEnvelope(msg3, utils.GetUnixEpoch(), pubsubTopic1))
|
2023-04-10 20:39:49 +00:00
|
|
|
|
|
|
|
n1HostInfo, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/p2p/%s", node1.Host().ID().Pretty()))
|
|
|
|
n1Addr := node1.ListenAddresses()[0].Encapsulate(n1HostInfo)
|
|
|
|
|
|
|
|
node2, err := node.New()
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = node2.Start(context.Background())
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer node2.Stop()
|
|
|
|
router := chi.NewRouter()
|
|
|
|
|
|
|
|
_ = NewStoreService(node2, router)
|
|
|
|
|
|
|
|
// TEST: get cursor
|
|
|
|
// TEST: get no messages
|
|
|
|
|
|
|
|
// First page
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
queryParams := url.Values{
|
|
|
|
"peerAddr": {n1Addr.String()},
|
|
|
|
"pubsubTopic": {pubsubTopic1},
|
|
|
|
"pageSize": {"2"},
|
|
|
|
}
|
|
|
|
path := ROUTE_STORE_MESSAGESV1 + "?" + queryParams.Encode()
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, path, nil)
|
|
|
|
router.ServeHTTP(rr, req)
|
|
|
|
require.Equal(t, http.StatusOK, rr.Code)
|
|
|
|
|
|
|
|
response := StoreResponse{}
|
|
|
|
err = json.Unmarshal(rr.Body.Bytes(), &response)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, response.Messages, 2)
|
|
|
|
|
|
|
|
// Second page
|
|
|
|
rr = httptest.NewRecorder()
|
|
|
|
queryParams = url.Values{
|
|
|
|
"peerAddr": {n1Addr.String()},
|
|
|
|
"pubsubTopic": {pubsubTopic1},
|
|
|
|
"senderTime": {response.Cursor.SenderTime},
|
|
|
|
"storeTime": {response.Cursor.StoreTime},
|
|
|
|
"digest": {base64.URLEncoding.EncodeToString(response.Cursor.Digest)},
|
|
|
|
"pageSize": {"2"},
|
|
|
|
}
|
|
|
|
path = ROUTE_STORE_MESSAGESV1 + "?" + queryParams.Encode()
|
|
|
|
req, _ = http.NewRequest(http.MethodGet, path, nil)
|
|
|
|
router.ServeHTTP(rr, req)
|
|
|
|
require.Equal(t, http.StatusOK, rr.Code)
|
|
|
|
|
|
|
|
response = StoreResponse{}
|
|
|
|
err = json.Unmarshal(rr.Body.Bytes(), &response)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, response.Messages, 1)
|
|
|
|
require.Nil(t, response.Cursor)
|
|
|
|
}
|