Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
mybee
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
vicotor
mybee
Commits
b1eda29c
Commit
b1eda29c
authored
Jan 27, 2020
by
Janos Guljas
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add pingpong api tests
parent
b06608ad
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
175 additions
and
4 deletions
+175
-4
api.go
pkg/api/api.go
+1
-3
api_test.go
pkg/api/api_test.go
+108
-0
pingpong.go
pkg/api/pingpong.go
+6
-0
pingpong_test.go
pkg/api/pingpong_test.go
+60
-0
node.go
pkg/node/node.go
+0
-1
No files found.
pkg/api/api.go
View file @
b1eda29c
...
...
@@ -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
{
...
...
pkg/api/api_test.go
0 → 100644
View file @
b1eda29c
// 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
}
pkg/api/pingpong.go
View file @
b1eda29c
...
...
@@ -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
}
...
...
pkg/api/pingpong_test.go
0 → 100644
View file @
b1eda29c
// 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
(),
})
})
}
pkg/node/node.go
View file @
b1eda29c
...
...
@@ -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
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment