github.com/ethersphere/bee/v2@v2.2.0/pkg/api/pingpong_test.go (about)

     1  // Copyright 2020 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package api_test
     6  
     7  import (
     8  	"context"
     9  	"errors"
    10  	"net/http"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/ethersphere/bee/v2/pkg/api"
    15  	"github.com/ethersphere/bee/v2/pkg/jsonhttp"
    16  	"github.com/ethersphere/bee/v2/pkg/jsonhttp/jsonhttptest"
    17  	"github.com/ethersphere/bee/v2/pkg/p2p"
    18  	pingpongmock "github.com/ethersphere/bee/v2/pkg/pingpong/mock"
    19  	"github.com/ethersphere/bee/v2/pkg/swarm"
    20  )
    21  
    22  func TestPingpong(t *testing.T) {
    23  	t.Parallel()
    24  
    25  	rtt := time.Minute
    26  	peerID := swarm.MustParseHexAddress("ca1e9f3938cc1425c6061b96ad9eb93e134dfe8734ad490164ef20af9d1cf59c")
    27  	unknownPeerID := swarm.MustParseHexAddress("ca1e9f3938cc1425c6061b96ad9eb93e134dfe8734ad490164ef20af9d1cf59e")
    28  	errorPeerID := swarm.MustParseHexAddress("ca1e9f3938cc1425c6061b96ad9eb93e134dfe8734ad490164ef20af9d1cf59a")
    29  	testErr := errors.New("test error")
    30  
    31  	pingpongService := pingpongmock.New(func(ctx context.Context, address swarm.Address, msgs ...string) (time.Duration, error) {
    32  		if address.Equal(errorPeerID) {
    33  			return 0, testErr
    34  		}
    35  		if !address.Equal(peerID) {
    36  			return 0, p2p.ErrPeerNotFound
    37  		}
    38  		return rtt, nil
    39  	})
    40  
    41  	ts, _, _, _ := newTestServer(t, testServerOptions{
    42  		Pingpong: pingpongService,
    43  	})
    44  
    45  	t.Run("ok", func(t *testing.T) {
    46  		t.Parallel()
    47  
    48  		jsonhttptest.Request(t, ts, http.MethodPost, "/pingpong/"+peerID.String(), http.StatusOK,
    49  			jsonhttptest.WithExpectedJSONResponse(api.PingpongResponse{
    50  				RTT: rtt.String(),
    51  			}),
    52  		)
    53  	})
    54  
    55  	t.Run("peer not found", func(t *testing.T) {
    56  		t.Parallel()
    57  
    58  		jsonhttptest.Request(t, ts, http.MethodPost, "/pingpong/"+unknownPeerID.String(), http.StatusNotFound,
    59  			jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{
    60  				Code:    http.StatusNotFound,
    61  				Message: "peer not found",
    62  			}),
    63  		)
    64  	})
    65  
    66  	t.Run("error", func(t *testing.T) {
    67  		t.Parallel()
    68  
    69  		jsonhttptest.Request(t, ts, http.MethodPost, "/pingpong/"+errorPeerID.String(), http.StatusInternalServerError,
    70  			jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{
    71  				Code:    http.StatusInternalServerError,
    72  				Message: "pingpong: ping failed",
    73  			}),
    74  		)
    75  	})
    76  }
    77  
    78  func Test_pingpongHandler_invalidInputs(t *testing.T) {
    79  	t.Parallel()
    80  
    81  	client, _, _, _ := newTestServer(t, testServerOptions{})
    82  
    83  	tests := []struct {
    84  		name    string
    85  		address string
    86  		want    jsonhttp.StatusResponse
    87  	}{{
    88  		name:    "address - odd hex string",
    89  		address: "123",
    90  		want: jsonhttp.StatusResponse{
    91  			Code:    http.StatusBadRequest,
    92  			Message: "invalid path params",
    93  			Reasons: []jsonhttp.Reason{
    94  				{
    95  					Field: "address",
    96  					Error: api.ErrHexLength.Error(),
    97  				},
    98  			},
    99  		},
   100  	}, {
   101  		name:    "address - invalid hex character",
   102  		address: "123G",
   103  		want: jsonhttp.StatusResponse{
   104  			Code:    http.StatusBadRequest,
   105  			Message: "invalid path params",
   106  			Reasons: []jsonhttp.Reason{
   107  				{
   108  					Field: "address",
   109  					Error: api.HexInvalidByteError('G').Error(),
   110  				},
   111  			},
   112  		},
   113  	}}
   114  
   115  	for _, tc := range tests {
   116  		tc := tc
   117  		t.Run(tc.name, func(t *testing.T) {
   118  			t.Parallel()
   119  
   120  			jsonhttptest.Request(t, client, http.MethodPost, "/pingpong/"+tc.address, tc.want.Code,
   121  				jsonhttptest.WithExpectedJSONResponse(tc.want),
   122  			)
   123  		})
   124  	}
   125  }