Commit b1eda29c authored by Janos Guljas's avatar Janos Guljas

add pingpong api tests

parent b06608ad
......@@ -7,7 +7,6 @@ package api
import (
"net/http"
"github.com/ethersphere/bee/pkg/p2p"
"github.com/ethersphere/bee/pkg/pingpong"
"github.com/prometheus/client_golang/prometheus"
)
......@@ -24,8 +23,7 @@ type server struct {
}
type Options struct {
P2P p2p.Service
Pingpong *pingpong.Service
Pingpong pingpong.Interface
}
func New(o Options) Service {
......
// 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 api
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/ethersphere/bee/pkg/pingpong"
"resenje.org/web"
)
var _ = testResponseUnmarshal // avoid lint error for unused function
type testServerOptions struct {
Pingpong pingpong.Interface
}
func newTestServer(t *testing.T, o testServerOptions) (client *http.Client, cleanup func()) {
s := New(Options{
Pingpong: o.Pingpong,
})
ts := httptest.NewServer(s)
cleanup = func() {
ts.Close()
}
client = &http.Client{
Transport: web.RoundTripperFunc(func(r *http.Request) (*http.Response, error) {
u, err := url.Parse(ts.URL + r.URL.String())
if err != nil {
return nil, err
}
r.URL = u
return ts.Client().Transport.RoundTrip(r)
}),
}
return client, cleanup
}
func testResponseDirect(t *testing.T, client *http.Client, method, url, body string, responseCode int, response interface{}) {
t.Helper()
resp, err := request(client, method, url, body, responseCode)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != responseCode {
t.Errorf("got response status %s, want %v %s", resp.Status, responseCode, http.StatusText(responseCode))
}
gotBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
wantBytes, err := json.Marshal(response)
if err != nil {
t.Error(err)
}
got := string(bytes.TrimSpace(gotBytes))
want := string(wantBytes)
if got != want {
t.Errorf("got response %s, want %s", got, want)
}
}
func testResponseUnmarshal(t *testing.T, client *http.Client, method, url, body string, responseCode int, response interface{}) {
t.Helper()
resp, err := request(client, method, url, body, responseCode)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != responseCode {
t.Errorf("got response status %s, want %v %s", resp.Status, responseCode, http.StatusText(responseCode))
}
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
t.Fatal(err)
}
}
func request(client *http.Client, method, url, body string, responseCode int) (resp *http.Response, err error) {
req, err := http.NewRequest(method, url, strings.NewReader(body))
if err != nil {
return nil, err
}
resp, err = client.Do(req)
if err != nil {
return nil, err
}
return resp, nil
}
......@@ -5,10 +5,12 @@
package api
import (
"errors"
"net/http"
"time"
"github.com/ethersphere/bee/pkg/jsonhttp"
"github.com/ethersphere/bee/pkg/p2p"
"github.com/gorilla/mux"
)
......@@ -22,6 +24,10 @@ func (s *server) pingpongHandler(w http.ResponseWriter, r *http.Request) {
rtt, err := s.Pingpong.Ping(ctx, peerID, "hey", "there", ",", "how are", "you", "?")
if err != nil {
if errors.Is(err, p2p.ErrPeerNotFound) {
jsonhttp.NotFound(w, "peer not found")
return
}
jsonhttp.InternalServerError(w, err.Error())
return
}
......
// 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 api
import (
"context"
"errors"
"net/http"
"testing"
"time"
"github.com/ethersphere/bee/pkg/jsonhttp"
"github.com/ethersphere/bee/pkg/p2p"
pingpongmock "github.com/ethersphere/bee/pkg/pingpong/mock"
)
func TestPingpong(t *testing.T) {
rtt := time.Minute
peerID := "124762324"
unknownPeerID := "55555555"
errorPeerID := "77777777"
testErr := errors.New("test error")
pingpongService := pingpongmock.New(func(ctx context.Context, address string, msgs ...string) (time.Duration, error) {
if address == errorPeerID {
return 0, testErr
}
if address != peerID {
return 0, p2p.ErrPeerNotFound
}
return rtt, nil
})
client, cleanup := newTestServer(t, testServerOptions{
Pingpong: pingpongService,
})
defer cleanup()
t.Run("ok", func(t *testing.T) {
testResponseDirect(t, client, http.MethodGet, "/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{
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{
Code: http.StatusInternalServerError,
Message: testErr.Error(),
})
})
}
......@@ -77,7 +77,6 @@ func NewBee(o Options) (*Bee, error) {
if o.APIAddr != "" {
// API server
apiService = api.New(api.Options{
P2P: p2ps,
Pingpong: pingPong,
})
apiListener, err := net.Listen("tcp", o.APIAddr)
......
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