go-http-peering/server/advertise_test.go

159 lines
3.3 KiB
Go

package server
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"forge.cadoles.com/wpetit/go-http-peering/crypto"
peering "forge.cadoles.com/wpetit/go-http-peering"
"forge.cadoles.com/wpetit/go-http-peering/memory"
)
func TestAdvertiseHandlerBadRequest(t *testing.T) {
store := memory.NewStore()
handler := AdvertiseHandler(store)
req := httptest.NewRequest("POST", peering.AdvertisePath, nil)
w := httptest.NewRecorder()
handler(w, req)
res := w.Result()
if g, e := res.StatusCode, http.StatusBadRequest; g != e {
t.Errorf("res.StatusCode: got '%v', expected '%v'", g, e)
}
peers, err := store.List()
if err != nil {
t.Fatal(err)
}
if g, e := len(peers), 0; g != e {
t.Errorf("len(peers): got '%v', expected '%v'", g, e)
}
}
func TestAdvertiseHandlerInvalidPublicKeyFormat(t *testing.T) {
store := memory.NewStore()
handler := AdvertiseHandler(store)
advertising := &peering.AdvertisingRequest{
ID: peering.NewPeerID(),
PublicKey: []byte("Test"),
}
body, err := json.Marshal(advertising)
if err != nil {
t.Fatal(err)
}
req := httptest.NewRequest("POST", peering.AdvertisePath, bytes.NewReader(body))
w := httptest.NewRecorder()
handler(w, req)
res := w.Result()
if g, e := res.StatusCode, http.StatusBadRequest; g != e {
t.Errorf("res.StatusCode: got '%v', expected '%v'", g, e)
}
peers, err := store.List()
if err != nil {
t.Fatal(err)
}
if g, e := len(peers), 0; g != e {
t.Errorf("len(peers): got '%v', expected '%v'", g, e)
}
}
func TestAdvertiseHandlerExistingPeer(t *testing.T) {
store := memory.NewStore()
handler := AdvertiseHandler(store)
pk := mustGeneratePrivateKey()
pem, err := crypto.EncodePublicKeyToPEM(pk.Public())
if err != nil {
t.Fatal(err)
}
peerID := peering.NewPeerID()
advertising := &peering.AdvertisingRequest{
ID: peerID,
PublicKey: pem,
}
body, err := json.Marshal(advertising)
if err != nil {
t.Fatal(err)
}
req := httptest.NewRequest("POST", peering.AdvertisePath, bytes.NewReader(body))
w := httptest.NewRecorder()
handler(w, req)
req = httptest.NewRequest("POST", peering.AdvertisePath, bytes.NewReader(body))
w = httptest.NewRecorder()
handler(w, req)
res := w.Result()
if g, e := res.StatusCode, http.StatusConflict; g != e {
t.Errorf("res.StatusCode: got '%v', expected '%v'", g, e)
}
}
func TestAdvertiseHandlerValidRequest(t *testing.T) {
store := memory.NewStore()
handler := AdvertiseHandler(store)
pk := mustGeneratePrivateKey()
pem, err := crypto.EncodePublicKeyToPEM(pk.Public())
if err != nil {
t.Fatal(err)
}
peerID := peering.NewPeerID()
advertising := &peering.AdvertisingRequest{
ID: peerID,
PublicKey: pem,
}
body, err := json.Marshal(advertising)
if err != nil {
t.Fatal(err)
}
req := httptest.NewRequest("POST", peering.AdvertisePath, bytes.NewReader(body))
w := httptest.NewRecorder()
handler(w, req)
res := w.Result()
if g, e := res.StatusCode, http.StatusCreated; g != e {
t.Errorf("res.StatusCode: got '%v', expected '%v'", g, e)
}
peer, err := store.Get(peerID)
if err != nil {
t.Fatal(err)
}
if g, e := peer.PublicKey, advertising.PublicKey; !bytes.Equal(peer.PublicKey, advertising.PublicKey) {
t.Errorf("peer.PublicKey: got '%v', expected '%v'", g, e)
}
}