-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add debugapi /addresses endpoint to expose libp2p mutiaddresses (#44)
- Loading branch information
Showing
7 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2020 The Swarm Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package debugapi | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/ethersphere/bee/pkg/jsonhttp" | ||
"github.com/multiformats/go-multiaddr" | ||
) | ||
|
||
type addressesResponse struct { | ||
Addresses []multiaddr.Multiaddr `json:"addresses"` | ||
} | ||
|
||
func (s *server) addressesHandler(w http.ResponseWriter, r *http.Request) { | ||
addresses, err := s.P2P.Addresses() | ||
if err != nil { | ||
s.Logger.Debugf("debug api: p2p addresses: %v", err) | ||
jsonhttp.InternalServerError(w, err) | ||
return | ||
} | ||
jsonhttp.OK(w, addressesResponse{ | ||
Addresses: addresses, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2020 The Swarm Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package debugapi_test | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/ethersphere/bee/pkg/debugapi" | ||
"github.com/ethersphere/bee/pkg/jsonhttp" | ||
"github.com/ethersphere/bee/pkg/jsonhttp/jsonhttptest" | ||
"github.com/ethersphere/bee/pkg/p2p/mock" | ||
"github.com/multiformats/go-multiaddr" | ||
) | ||
|
||
func TestAddresses(t *testing.T) { | ||
addresses := []multiaddr.Multiaddr{ | ||
mustMultiaddr(t, "/ip4/127.0.0.1/tcp/7071/p2p/16Uiu2HAmTBuJT9LvNmBiQiNoTsxE5mtNy6YG3paw79m94CRa9sRb"), | ||
mustMultiaddr(t, "/ip4/192.168.0.101/tcp/7071/p2p/16Uiu2HAmTBuJT9LvNmBiQiNoTsxE5mtNy6YG3paw79m94CRa9sRb"), | ||
mustMultiaddr(t, "/ip4/127.0.0.1/udp/7071/quic/p2p/16Uiu2HAmTBuJT9LvNmBiQiNoTsxE5mtNy6YG3paw79m94CRa9sRb"), | ||
} | ||
|
||
testServer := newTestServer(t, testServerOptions{ | ||
P2P: mock.New(mock.WithAddressesFunc(func() ([]multiaddr.Multiaddr, error) { | ||
return addresses, nil | ||
})), | ||
}) | ||
defer testServer.Cleanup() | ||
|
||
t.Run("ok", func(t *testing.T) { | ||
jsonhttptest.ResponseDirect(t, testServer.Client, http.MethodGet, "/addresses", nil, http.StatusOK, debugapi.AddressesResponse{ | ||
Addresses: addresses, | ||
}) | ||
}) | ||
|
||
t.Run("post method not allowed", func(t *testing.T) { | ||
jsonhttptest.ResponseDirect(t, testServer.Client, http.MethodPost, "/addresses", nil, http.StatusMethodNotAllowed, jsonhttp.StatusResponse{ | ||
Code: http.StatusMethodNotAllowed, | ||
Message: http.StatusText(http.StatusMethodNotAllowed), | ||
}) | ||
}) | ||
} | ||
|
||
func TestAddresses_error(t *testing.T) { | ||
testErr := errors.New("test error") | ||
|
||
testServer := newTestServer(t, testServerOptions{ | ||
P2P: mock.New(mock.WithAddressesFunc(func() ([]multiaddr.Multiaddr, error) { | ||
return nil, testErr | ||
})), | ||
}) | ||
defer testServer.Cleanup() | ||
|
||
jsonhttptest.ResponseDirect(t, testServer.Client, http.MethodGet, "/addresses", nil, http.StatusInternalServerError, jsonhttp.StatusResponse{ | ||
Code: http.StatusInternalServerError, | ||
Message: testErr.Error(), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters