pingpong_test.go 2.82 KB
Newer Older
Janos Guljas's avatar
Janos Guljas committed
1 2 3 4
// 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.

5
package debugapi_test
Janos Guljas's avatar
Janos Guljas committed
6 7 8 9 10 11 12 13

import (
	"context"
	"errors"
	"net/http"
	"testing"
	"time"

14
	"github.com/ethersphere/bee/pkg/debugapi"
Janos Guljas's avatar
Janos Guljas committed
15
	"github.com/ethersphere/bee/pkg/jsonhttp"
16
	"github.com/ethersphere/bee/pkg/jsonhttp/jsonhttptest"
Janos Guljas's avatar
Janos Guljas committed
17 18
	"github.com/ethersphere/bee/pkg/p2p"
	pingpongmock "github.com/ethersphere/bee/pkg/pingpong/mock"
19
	"github.com/ethersphere/bee/pkg/swarm"
Janos Guljas's avatar
Janos Guljas committed
20 21 22 23
)

func TestPingpong(t *testing.T) {
	rtt := time.Minute
24 25 26
	peerID := swarm.MustParseHexAddress("ca1e9f3938cc1425c6061b96ad9eb93e134dfe8734ad490164ef20af9d1cf59c")
	unknownPeerID := swarm.MustParseHexAddress("ca1e9f3938cc1425c6061b96ad9eb93e134dfe8734ad490164ef20af9d1cf59e")
	errorPeerID := swarm.MustParseHexAddress("ca1e9f3938cc1425c6061b96ad9eb93e134dfe8734ad490164ef20af9d1cf59a")
Janos Guljas's avatar
Janos Guljas committed
27 28
	testErr := errors.New("test error")

29 30
	pingpongService := pingpongmock.New(func(ctx context.Context, address swarm.Address, msgs ...string) (time.Duration, error) {
		if address.Equal(errorPeerID) {
Janos Guljas's avatar
Janos Guljas committed
31 32
			return 0, testErr
		}
33
		if !address.Equal(peerID) {
Janos Guljas's avatar
Janos Guljas committed
34 35 36 37 38
			return 0, p2p.ErrPeerNotFound
		}
		return rtt, nil
	})

39
	ts := newTestServer(t, testServerOptions{
Janos Guljas's avatar
Janos Guljas committed
40 41 42 43
		Pingpong: pingpongService,
	})

	t.Run("ok", func(t *testing.T) {
44 45 46 47 48
		jsonhttptest.Request(t, ts.Client, http.MethodPost, "/pingpong/"+peerID.String(), http.StatusOK,
			jsonhttptest.WithExpectedJSONResponse(debugapi.PingpongResponse{
				RTT: rtt.String(),
			}),
		)
Janos Guljas's avatar
Janos Guljas committed
49 50 51
	})

	t.Run("peer not found", func(t *testing.T) {
52 53 54 55 56 57
		jsonhttptest.Request(t, ts.Client, http.MethodPost, "/pingpong/"+unknownPeerID.String(), http.StatusNotFound,
			jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{
				Code:    http.StatusNotFound,
				Message: "peer not found",
			}),
		)
Janos Guljas's avatar
Janos Guljas committed
58 59
	})

60
	t.Run("invalid peer address", func(t *testing.T) {
61 62 63 64 65 66
		jsonhttptest.Request(t, ts.Client, http.MethodPost, "/pingpong/invalid-address", http.StatusBadRequest,
			jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{
				Code:    http.StatusBadRequest,
				Message: "invalid peer address",
			}),
		)
67 68
	})

Janos Guljas's avatar
Janos Guljas committed
69
	t.Run("error", func(t *testing.T) {
70 71 72 73 74 75
		jsonhttptest.Request(t, ts.Client, http.MethodPost, "/pingpong/"+errorPeerID.String(), http.StatusInternalServerError,
			jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{
				Code:    http.StatusInternalServerError,
				Message: http.StatusText(http.StatusInternalServerError), // do not leak internal error
			}),
		)
Janos Guljas's avatar
Janos Guljas committed
76
	})
77 78

	t.Run("get method not allowed", func(t *testing.T) {
79 80 81 82 83 84
		jsonhttptest.Request(t, ts.Client, http.MethodGet, "/pingpong/"+peerID.String(), http.StatusMethodNotAllowed,
			jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{
				Code:    http.StatusMethodNotAllowed,
				Message: http.StatusText(http.StatusMethodNotAllowed),
			}),
		)
85
	})
Janos Guljas's avatar
Janos Guljas committed
86
}