2018-02-07 21:21:20 +01:00
|
|
|
package peer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/lbryio/reflector.go/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
var blobs = map[string][]byte{
|
|
|
|
"a": []byte("abcdefg"),
|
|
|
|
"b": []byte("hijklmn"),
|
|
|
|
"c": []byte("opqrstu"),
|
|
|
|
}
|
|
|
|
|
|
|
|
type pair struct {
|
|
|
|
request []byte
|
|
|
|
response []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
var availabilityRequests = []pair{
|
|
|
|
{
|
|
|
|
request: []byte(`{"lbrycrd_address":true,"requested_blobs":["a","b"]}`),
|
|
|
|
response: []byte(`{"lbrycrd_address":"` + LbrycrdAddress + `","available_blobs":["a","b"]}`),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
request: []byte(`{"lbrycrd_address":true,"requested_blobs":["x","a","y"]}`),
|
|
|
|
response: []byte(`{"lbrycrd_address":"` + LbrycrdAddress + `","available_blobs":["a"]}`),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
request: []byte(`{"lbrycrd_address":true,"requested_blobs":[]}`),
|
|
|
|
response: []byte(`{"lbrycrd_address":"` + LbrycrdAddress + `","available_blobs":[]}`),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
func getServer(t *testing.T, withBlobs bool) *Server {
|
2020-10-22 19:49:02 +02:00
|
|
|
st := store.NewMemStore()
|
2018-02-07 21:21:20 +01:00
|
|
|
if withBlobs {
|
|
|
|
for k, v := range blobs {
|
2018-06-15 04:30:37 +02:00
|
|
|
err := st.Put(k, v)
|
|
|
|
if err != nil {
|
2018-05-30 03:38:55 +02:00
|
|
|
t.Error("error during put operation of memory blobstore - ", err)
|
|
|
|
}
|
2018-02-07 21:21:20 +01:00
|
|
|
}
|
|
|
|
}
|
2019-10-03 22:47:23 +02:00
|
|
|
return NewServer(st)
|
2018-02-07 21:21:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAvailabilityRequest_NoBlobs(t *testing.T) {
|
2018-05-30 03:38:55 +02:00
|
|
|
s := getServer(t, false)
|
2018-02-07 21:21:20 +01:00
|
|
|
|
|
|
|
for _, p := range availabilityRequests {
|
|
|
|
response, err := s.handleAvailabilityRequest(p.request)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected no error, got %v", err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(response, []byte(`{"lbrycrd_address":"`+LbrycrdAddress+`","available_blobs":[]}`)) {
|
|
|
|
t.Errorf("Response did not match expected response. Got %s", string(response))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAvailabilityRequest_WithBlobs(t *testing.T) {
|
2018-05-30 03:38:55 +02:00
|
|
|
s := getServer(t, true)
|
2018-02-07 21:21:20 +01:00
|
|
|
|
|
|
|
for _, p := range availabilityRequests {
|
|
|
|
response, err := s.handleAvailabilityRequest(p.request)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected no error, got %v", err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(response, p.response) {
|
|
|
|
t.Errorf("Response did not match expected response.\nExpected: %s\nGot: %s", string(p.response), string(response))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|