Commit d2691baa authored by Janos Guljas's avatar Janos Guljas

allow only post method for api pingpong endpoint

parent 14e71156
......@@ -25,10 +25,10 @@ Use one of the multiaddresses as bootnode for `node 2` in order to connect them:
bee start --api-addr :8502 --p2p-addr :30402 --data-dir data2 --bootnode /ip4/127.0.0.1/tcp/30401/p2p/QmT4TNB4cKYanUjdYodw1Cns8cuVaRVo24hHNYcT7JjkTB
```
Take the address of the connected peer to `node 1` from log line `peer "4932309428148935717" connected` and make an HTTP request to `localhost:{PORT1}/pingpong/{ADDRESS}` like:
Take the address of the connected peer to `node 1` from log line `peer "4932309428148935717" connected` and make an HTTP POST request to `localhost:{PORT1}/pingpong/{ADDRESS}` like:
```sh
curl localhost:8502/pingpong/4932309428148935717
curl -XPOST localhost:8502/pingpong/4932309428148935717
```
## Structure
......
......@@ -7,8 +7,10 @@ package api
import (
"net/http"
"github.com/ethersphere/bee/pkg/jsonhttp"
"github.com/ethersphere/bee/pkg/pingpong"
"github.com/prometheus/client_golang/prometheus"
"resenje.org/web"
)
type Service interface {
......@@ -40,3 +42,9 @@ func New(o Options) Service {
return s
}
type methodHandler map[string]http.Handler
func (h methodHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
web.HandleMethods(h, `{"message":"Method Not Allowed","code":405}`, jsonhttp.DefaultContentTypeHeader, w, r)
}
......@@ -39,22 +39,29 @@ func TestPingpong(t *testing.T) {
defer cleanup()
t.Run("ok", func(t *testing.T) {
testResponseDirect(t, client, http.MethodGet, "/pingpong/"+peerID, "", http.StatusOK, pingpongResponse{
testResponseDirect(t, client, http.MethodPost, "/pingpong/"+peerID, "", http.StatusOK, pingpongResponse{
RTT: rtt,
})
})
t.Run("peer not found", func(t *testing.T) {
testResponseDirect(t, client, http.MethodGet, "/pingpong/"+unknownPeerID, "", http.StatusNotFound, jsonhttp.StatusResponse{
testResponseDirect(t, client, http.MethodPost, "/pingpong/"+unknownPeerID, "", http.StatusNotFound, jsonhttp.StatusResponse{
Code: http.StatusNotFound,
Message: "peer not found",
})
})
t.Run("error", func(t *testing.T) {
testResponseDirect(t, client, http.MethodGet, "/pingpong/"+errorPeerID, "", http.StatusInternalServerError, jsonhttp.StatusResponse{
testResponseDirect(t, client, http.MethodPost, "/pingpong/"+errorPeerID, "", http.StatusInternalServerError, jsonhttp.StatusResponse{
Code: http.StatusInternalServerError,
Message: testErr.Error(),
})
})
t.Run("get method not allowed", func(t *testing.T) {
testResponseDirect(t, client, http.MethodGet, "/pingpong/"+peerID, "", http.StatusMethodNotAllowed, jsonhttp.StatusResponse{
Code: http.StatusMethodNotAllowed,
Message: http.StatusText(http.StatusMethodNotAllowed),
})
})
}
......@@ -20,7 +20,9 @@ func (s *server) setupRouting() {
fmt.Fprintln(w, "User-agent: *\nDisallow: /")
})
baseRouter.HandleFunc("/pingpong/{peer-id}", s.pingpongHandler)
baseRouter.Handle("/pingpong/{peer-id}", methodHandler{
"POST": http.HandlerFunc(s.pingpongHandler),
})
s.Handler = web.ChainHandlers(
handlers.CompressHandler,
......
......@@ -27,21 +27,21 @@ var (
// description of that code or provides more context about the reason for such
// response.
type StatusResponse struct {
Code int `json:"code,omitempty"`
Message string `json:"message,omitempty"`
Code int `json:"code,omitempty"`
}
// Respond writes a JSON-encoded body to http.ResponseWriter.
func Respond(w http.ResponseWriter, statusCode int, response interface{}) {
if response == nil {
response = &StatusResponse{
Code: statusCode,
Message: http.StatusText(statusCode),
Code: statusCode,
}
} else if message, ok := response.(string); ok {
response = &StatusResponse{
Code: statusCode,
Message: message,
Code: statusCode,
}
}
var b bytes.Buffer
......
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