Commit cc9f2dd6 authored by Janoš Guljaš's avatar Janoš Guljaš Committed by GitHub

add debugapi p2p peers endpoint (#24)

parent 972caa1f
......@@ -7,4 +7,5 @@ package debugapi
type (
StatusResponse = statusResponse
PeerConnectResponse = peerConnectResponse
PeersResponse = peersResponse
)
......@@ -62,3 +62,13 @@ func (s *server) peerDisconnectHandler(w http.ResponseWriter, r *http.Request) {
jsonhttp.OK(w, nil)
}
type peersResponse struct {
Peers []p2p.Peer `json:"peers"`
}
func (s *server) peersHandler(w http.ResponseWriter, r *http.Request) {
jsonhttp.OK(w, peersResponse{
Peers: s.P2P.Peers(),
})
}
......@@ -105,3 +105,27 @@ func TestDisconnect(t *testing.T) {
})
})
}
func TestPeer(t *testing.T) {
overlay := swarm.MustParseHexAddress("ca1e9f3938cc1425c6061b96ad9eb93e134dfe8734ad490164ef20af9d1cf59c")
client, cleanup := newTestServer(t, testServerOptions{
P2P: mock.New(mock.WithPeersFunc(func() []p2p.Peer {
return []p2p.Peer{{Address: overlay}}
})),
})
defer cleanup()
t.Run("ok", func(t *testing.T) {
jsonhttptest.ResponseDirect(t, client, http.MethodGet, "/peers", nil, http.StatusOK, debugapi.PeersResponse{
Peers: []p2p.Peer{{Address: overlay}},
})
})
t.Run("get method not allowed", func(t *testing.T) {
jsonhttptest.ResponseDirect(t, client, http.MethodPost, "/peers", nil, http.StatusMethodNotAllowed, jsonhttp.StatusResponse{
Code: http.StatusMethodNotAllowed,
Message: http.StatusText(http.StatusMethodNotAllowed),
})
})
}
......@@ -43,6 +43,9 @@ func (s *server) setupRouting() {
router.Handle("/connect/{multi-address:.+}", jsonhttp.MethodHandler{
"POST": http.HandlerFunc(s.peerConnectHandler),
})
router.Handle("/peers", jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.peersHandler),
})
router.Handle("/peers/{address}", jsonhttp.MethodHandler{
"DELETE": http.HandlerFunc(s.peerDisconnectHandler),
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment