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
aa15b3a4
Unverified
Commit
aa15b3a4
authored
Aug 18, 2020
by
Janoš Guljaš
Committed by
GitHub
Aug 18, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cleanup jsonhttptest package (#577)
parent
24d5b25a
Changes
18
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
18 changed files
with
1298 additions
and
679 deletions
+1298
-679
bytes_test.go
pkg/api/bytes_test.go
+12
-7
bzz_test.go
pkg/api/bzz_test.go
+9
-6
chunk_test.go
pkg/api/chunk_test.go
+44
-29
dirs_test.go
pkg/api/dirs_test.go
+34
-24
file_test.go
pkg/api/file_test.go
+84
-59
tag_test.go
pkg/api/tag_test.go
+221
-173
balances_test.go
pkg/debugapi/balances_test.go
+27
-17
chunk_test.go
pkg/debugapi/chunk_test.go
+30
-20
p2p_test.go
pkg/debugapi/p2p_test.go
+18
-12
peer_test.go
pkg/debugapi/peer_test.go
+58
-38
pin_test.go
pkg/debugapi/pin_test.go
+128
-86
pingpong_test.go
pkg/debugapi/pingpong_test.go
+29
-19
status_test.go
pkg/debugapi/status_test.go
+10
-6
topology_test.go
pkg/debugapi/topology_test.go
+11
-7
welcome_message_test.go
pkg/debugapi/welcome_message_test.go
+13
-5
jsonhttptest.go
pkg/jsonhttp/jsonhttptest/jsonhttptest.go
+201
-134
jsonhttptest_test.go
pkg/jsonhttp/jsonhttptest/jsonhttptest_test.go
+298
-37
testing_mock_test.go
pkg/jsonhttp/jsonhttptest/testing_mock_test.go
+71
-0
No files found.
pkg/api/bytes_test.go
View file @
aa15b3a4
...
@@ -41,9 +41,12 @@ func TestBytes(t *testing.T) {
...
@@ -41,9 +41,12 @@ func TestBytes(t *testing.T) {
}
}
t
.
Run
(
"upload"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"upload"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
client
,
http
.
MethodPost
,
resource
,
bytes
.
NewReader
(
content
),
http
.
StatusOK
,
api
.
BytesPostResponse
{
jsonhttptest
.
Request
(
t
,
client
,
http
.
MethodPost
,
resource
,
http
.
StatusOK
,
jsonhttptest
.
WithRequestBody
(
bytes
.
NewReader
(
content
)),
jsonhttptest
.
WithExpectedJSONResponse
(
api
.
BytesPostResponse
{
Reference
:
swarm
.
MustParseHexAddress
(
expHash
),
Reference
:
swarm
.
MustParseHexAddress
(
expHash
),
})
}),
)
})
})
t
.
Run
(
"download"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"download"
,
func
(
t
*
testing
.
T
)
{
...
@@ -67,9 +70,11 @@ func TestBytes(t *testing.T) {
...
@@ -67,9 +70,11 @@ func TestBytes(t *testing.T) {
})
})
t
.
Run
(
"not found"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"not found"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
client
,
http
.
MethodGet
,
resource
+
"/abcd"
,
nil
,
http
.
StatusNotFound
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
client
,
http
.
MethodGet
,
resource
+
"/abcd"
,
http
.
StatusNotFound
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
"not found"
,
Message
:
"not found"
,
Code
:
http
.
StatusNotFound
,
Code
:
http
.
StatusNotFound
,
})
}),
)
})
})
}
}
pkg/api/bzz_test.go
View file @
aa15b3a4
...
@@ -132,7 +132,9 @@ func TestBzz(t *testing.T) {
...
@@ -132,7 +132,9 @@ func TestBzz(t *testing.T) {
// read file from manifest path
// read file from manifest path
rcvdHeader
:=
jsonhttptest
.
ResponseDirectCheckBinaryResponse
(
t
,
client
,
http
.
MethodGet
,
bzzDownloadResource
(
manifestFileReference
.
String
(),
filePath
),
nil
,
http
.
StatusOK
,
[]
byte
(
sampleHtml
),
nil
)
rcvdHeader
:=
jsonhttptest
.
Request
(
t
,
client
,
http
.
MethodGet
,
bzzDownloadResource
(
manifestFileReference
.
String
(),
filePath
),
http
.
StatusOK
,
jsonhttptest
.
WithExpectedResponse
([]
byte
(
sampleHtml
)),
)
cd
:=
rcvdHeader
.
Get
(
"Content-Disposition"
)
cd
:=
rcvdHeader
.
Get
(
"Content-Disposition"
)
_
,
params
,
err
:=
mime
.
ParseMediaType
(
cd
)
_
,
params
,
err
:=
mime
.
ParseMediaType
(
cd
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -150,11 +152,12 @@ func TestBzz(t *testing.T) {
...
@@ -150,11 +152,12 @@ func TestBzz(t *testing.T) {
// check on invalid path
// check on invalid path
jsonhttptest
.
ResponseDirectSendHeadersAndReceiveHeaders
(
t
,
client
,
http
.
MethodGet
,
bzzDownloadResource
(
manifestFileReference
.
String
(),
missingFilePath
),
nil
,
http
.
StatusBadRequest
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
client
,
http
.
MethodGet
,
bzzDownloadResource
(
manifestFileReference
.
String
(),
missingFilePath
),
http
.
StatusBadRequest
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
"invalid path address"
,
Message
:
"invalid path address"
,
Code
:
http
.
StatusBadRequest
,
Code
:
http
.
StatusBadRequest
,
},
nil
)
}),
)
})
})
}
}
pkg/api/chunk_test.go
View file @
aa15b3a4
...
@@ -44,30 +44,39 @@ func TestChunkUploadDownload(t *testing.T) {
...
@@ -44,30 +44,39 @@ func TestChunkUploadDownload(t *testing.T) {
)
)
t
.
Run
(
"invalid hash"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"invalid hash"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
client
,
http
.
MethodPost
,
resource
(
invalidHash
),
bytes
.
NewReader
(
validContent
),
http
.
StatusBadRequest
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
client
,
http
.
MethodPost
,
resource
(
invalidHash
),
http
.
StatusBadRequest
,
jsonhttptest
.
WithRequestBody
(
bytes
.
NewReader
(
validContent
)),
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
"chunk write error"
,
Message
:
"chunk write error"
,
Code
:
http
.
StatusBadRequest
,
Code
:
http
.
StatusBadRequest
,
})
}),
)
// make sure chunk is not retrievable
// make sure chunk is not retrievable
_
=
request
(
t
,
client
,
http
.
MethodGet
,
resource
(
invalidHash
),
nil
,
http
.
StatusNotFound
)
_
=
request
(
t
,
client
,
http
.
MethodGet
,
resource
(
invalidHash
),
nil
,
http
.
StatusNotFound
)
})
})
t
.
Run
(
"invalid content"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"invalid content"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
client
,
http
.
MethodPost
,
resource
(
validHash
),
bytes
.
NewReader
(
invalidContent
),
http
.
StatusBadRequest
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
client
,
http
.
MethodPost
,
resource
(
invalidHash
),
http
.
StatusBadRequest
,
jsonhttptest
.
WithRequestBody
(
bytes
.
NewReader
(
invalidContent
)),
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
"chunk write error"
,
Message
:
"chunk write error"
,
Code
:
http
.
StatusBadRequest
,
Code
:
http
.
StatusBadRequest
,
})
}),
)
// make sure not retrievable
// make sure not retrievable
_
=
request
(
t
,
client
,
http
.
MethodGet
,
resource
(
validHash
),
nil
,
http
.
StatusNotFound
)
_
=
request
(
t
,
client
,
http
.
MethodGet
,
resource
(
validHash
),
nil
,
http
.
StatusNotFound
)
})
})
t
.
Run
(
"ok"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"ok"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
client
,
http
.
MethodPost
,
resource
(
validHash
),
bytes
.
NewReader
(
validContent
),
http
.
StatusOK
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
client
,
http
.
MethodPost
,
resource
(
validHash
),
http
.
StatusOK
,
jsonhttptest
.
WithRequestBody
(
bytes
.
NewReader
(
validContent
)),
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Code
:
http
.
StatusOK
,
Code
:
http
.
StatusOK
,
})
}),
)
// try to fetch the same chunk
// try to fetch the same chunk
resp
:=
request
(
t
,
client
,
http
.
MethodGet
,
resource
(
validHash
),
nil
,
http
.
StatusOK
)
resp
:=
request
(
t
,
client
,
http
.
MethodGet
,
resource
(
validHash
),
nil
,
http
.
StatusOK
)
...
@@ -82,12 +91,14 @@ func TestChunkUploadDownload(t *testing.T) {
...
@@ -82,12 +91,14 @@ func TestChunkUploadDownload(t *testing.T) {
})
})
t
.
Run
(
"pin-invalid-value"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"pin-invalid-value"
,
func
(
t
*
testing
.
T
)
{
headers
:=
make
(
map
[
string
][]
string
)
jsonhttptest
.
Request
(
t
,
client
,
http
.
MethodPost
,
resource
(
validHash
),
http
.
StatusOK
,
headers
[
api
.
SwarmPinHeader
]
=
[]
string
{
"hdgdh"
}
jsonhttptest
.
WithRequestBody
(
bytes
.
NewReader
(
validContent
)),
jsonhttptest
.
ResponseDirectSendHeadersAndReceiveHeaders
(
t
,
client
,
http
.
MethodPost
,
resource
(
validHash
),
bytes
.
NewReader
(
validContent
),
http
.
StatusOK
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Code
:
http
.
StatusOK
,
Code
:
http
.
StatusOK
,
},
headers
)
}),
jsonhttptest
.
WithRequestHeader
(
api
.
SwarmPinHeader
,
"invalid-pin"
),
)
// Also check if the chunk is NOT pinned
// Also check if the chunk is NOT pinned
if
mockValidatingStorer
.
GetModeSet
(
validHash
)
==
storage
.
ModeSetPin
{
if
mockValidatingStorer
.
GetModeSet
(
validHash
)
==
storage
.
ModeSetPin
{
...
@@ -95,11 +106,13 @@ func TestChunkUploadDownload(t *testing.T) {
...
@@ -95,11 +106,13 @@ func TestChunkUploadDownload(t *testing.T) {
}
}
})
})
t
.
Run
(
"pin-header-missing"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"pin-header-missing"
,
func
(
t
*
testing
.
T
)
{
headers
:=
make
(
map
[
string
][]
string
)
jsonhttptest
.
Request
(
t
,
client
,
http
.
MethodPost
,
resource
(
validHash
),
http
.
StatusOK
,
jsonhttptest
.
ResponseDirectSendHeadersAndReceiveHeaders
(
t
,
client
,
http
.
MethodPost
,
resource
(
validHash
),
bytes
.
NewReader
(
validContent
),
http
.
StatusOK
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
WithRequestBody
(
bytes
.
NewReader
(
validContent
)),
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Code
:
http
.
StatusOK
,
Code
:
http
.
StatusOK
,
},
headers
)
}),
)
// Also check if the chunk is NOT pinned
// Also check if the chunk is NOT pinned
if
mockValidatingStorer
.
GetModeSet
(
validHash
)
==
storage
.
ModeSetPin
{
if
mockValidatingStorer
.
GetModeSet
(
validHash
)
==
storage
.
ModeSetPin
{
...
@@ -107,12 +120,14 @@ func TestChunkUploadDownload(t *testing.T) {
...
@@ -107,12 +120,14 @@ func TestChunkUploadDownload(t *testing.T) {
}
}
})
})
t
.
Run
(
"pin-ok"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"pin-ok"
,
func
(
t
*
testing
.
T
)
{
headers
:=
make
(
map
[
string
][]
string
)
jsonhttptest
.
Request
(
t
,
client
,
http
.
MethodPost
,
resource
(
validHash
),
http
.
StatusOK
,
headers
[
api
.
SwarmPinHeader
]
=
[]
string
{
"True"
}
jsonhttptest
.
WithRequestBody
(
bytes
.
NewReader
(
validContent
)),
jsonhttptest
.
ResponseDirectSendHeadersAndReceiveHeaders
(
t
,
client
,
http
.
MethodPost
,
resource
(
validHash
),
bytes
.
NewReader
(
validContent
),
http
.
StatusOK
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Code
:
http
.
StatusOK
,
Code
:
http
.
StatusOK
,
},
headers
)
}),
jsonhttptest
.
WithRequestHeader
(
api
.
SwarmPinHeader
,
"True"
),
)
// Also check if the chunk is pinned
// Also check if the chunk is pinned
if
mockValidatingStorer
.
GetModePut
(
validHash
)
!=
storage
.
ModePutUploadPin
{
if
mockValidatingStorer
.
GetModePut
(
validHash
)
!=
storage
.
ModePutUploadPin
{
...
...
pkg/api/dirs_test.go
View file @
aa15b3a4
...
@@ -34,23 +34,27 @@ func TestDirs(t *testing.T) {
...
@@ -34,23 +34,27 @@ func TestDirs(t *testing.T) {
)
)
t
.
Run
(
"empty request body"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"empty request body"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirectSendHeadersAndReceiveHeaders
(
t
,
client
,
http
.
MethodPost
,
dirUploadResource
,
bytes
.
NewReader
(
nil
),
http
.
StatusBadRequest
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
client
,
http
.
MethodPost
,
dirUploadResource
,
http
.
StatusBadRequest
,
jsonhttptest
.
WithRequestBody
(
bytes
.
NewReader
(
nil
)),
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
"could not validate request"
,
Message
:
"could not validate request"
,
Code
:
http
.
StatusBadRequest
,
Code
:
http
.
StatusBadRequest
,
},
http
.
Header
{
}),
"Content-Type"
:
{
api
.
ContentTypeTar
}
,
jsonhttptest
.
WithRequestHeader
(
"Content-Type"
,
api
.
ContentTypeTar
)
,
}
)
)
})
})
t
.
Run
(
"non tar file"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"non tar file"
,
func
(
t
*
testing
.
T
)
{
file
:=
bytes
.
NewReader
([]
byte
(
"some data"
))
file
:=
bytes
.
NewReader
([]
byte
(
"some data"
))
jsonhttptest
.
ResponseDirectSendHeadersAndReceiveHeaders
(
t
,
client
,
http
.
MethodPost
,
dirUploadResource
,
file
,
http
.
StatusInternalServerError
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
client
,
http
.
MethodPost
,
dirUploadResource
,
http
.
StatusInternalServerError
,
jsonhttptest
.
WithRequestBody
(
file
),
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
"could not store dir"
,
Message
:
"could not store dir"
,
Code
:
http
.
StatusInternalServerError
,
Code
:
http
.
StatusInternalServerError
,
},
http
.
Header
{
}),
"Content-Type"
:
{
api
.
ContentTypeTar
}
,
jsonhttptest
.
WithRequestHeader
(
"Content-Type"
,
api
.
ContentTypeTar
)
,
}
)
)
})
})
t
.
Run
(
"wrong content type"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"wrong content type"
,
func
(
t
*
testing
.
T
)
{
...
@@ -60,12 +64,14 @@ func TestDirs(t *testing.T) {
...
@@ -60,12 +64,14 @@ func TestDirs(t *testing.T) {
}})
}})
// submit valid tar, but with wrong content-type
// submit valid tar, but with wrong content-type
jsonhttptest
.
ResponseDirectSendHeadersAndReceiveHeaders
(
t
,
client
,
http
.
MethodPost
,
dirUploadResource
,
tarReader
,
http
.
StatusBadRequest
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
client
,
http
.
MethodPost
,
dirUploadResource
,
http
.
StatusBadRequest
,
jsonhttptest
.
WithRequestBody
(
tarReader
),
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
"could not validate request"
,
Message
:
"could not validate request"
,
Code
:
http
.
StatusBadRequest
,
Code
:
http
.
StatusBadRequest
,
},
http
.
Header
{
}),
"Content-Type"
:
{
"other"
}
,
jsonhttptest
.
WithRequestHeader
(
"Content-Type"
,
"other"
)
,
}
)
)
})
})
// valid tars
// valid tars
...
@@ -137,11 +143,13 @@ func TestDirs(t *testing.T) {
...
@@ -137,11 +143,13 @@ func TestDirs(t *testing.T) {
tarReader
:=
tarFiles
(
t
,
tc
.
files
)
tarReader
:=
tarFiles
(
t
,
tc
.
files
)
// verify directory tar upload response
// verify directory tar upload response
jsonhttptest
.
ResponseDirectSendHeadersAndReceiveHeaders
(
t
,
client
,
http
.
MethodPost
,
dirUploadResource
,
tarReader
,
http
.
StatusOK
,
api
.
FileUploadResponse
{
jsonhttptest
.
Request
(
t
,
client
,
http
.
MethodPost
,
dirUploadResource
,
http
.
StatusOK
,
jsonhttptest
.
WithRequestBody
(
tarReader
),
jsonhttptest
.
WithExpectedJSONResponse
(
api
.
FileUploadResponse
{
Reference
:
swarm
.
MustParseHexAddress
(
tc
.
expectedHash
),
Reference
:
swarm
.
MustParseHexAddress
(
tc
.
expectedHash
),
},
http
.
Header
{
}),
"Content-Type"
:
{
api
.
ContentTypeTar
}
,
jsonhttptest
.
WithRequestHeader
(
"Content-Type"
,
api
.
ContentTypeTar
)
,
}
)
)
// create expected manifest
// create expected manifest
expectedManifest
:=
jsonmanifest
.
NewManifest
()
expectedManifest
:=
jsonmanifest
.
NewManifest
()
...
@@ -156,7 +164,9 @@ func TestDirs(t *testing.T) {
...
@@ -156,7 +164,9 @@ func TestDirs(t *testing.T) {
}
}
// verify directory upload manifest through files api
// verify directory upload manifest through files api
jsonhttptest
.
ResponseDirectCheckBinaryResponse
(
t
,
client
,
http
.
MethodGet
,
fileDownloadResource
(
tc
.
expectedHash
),
nil
,
http
.
StatusOK
,
b
,
nil
)
jsonhttptest
.
Request
(
t
,
client
,
http
.
MethodGet
,
fileDownloadResource
(
tc
.
expectedHash
),
http
.
StatusOK
,
jsonhttptest
.
WithExpectedResponse
(
b
),
)
})
})
}
}
}
}
...
...
pkg/api/file_test.go
View file @
aa15b3a4
This diff is collapsed.
Click to expand it.
pkg/api/tag_test.go
View file @
aa15b3a4
This diff is collapsed.
Click to expand it.
pkg/debugapi/balances_test.go
View file @
aa15b3a4
...
@@ -48,7 +48,9 @@ func TestBalances(t *testing.T) {
...
@@ -48,7 +48,9 @@ func TestBalances(t *testing.T) {
// We expect a list of items unordered by peer:
// We expect a list of items unordered by peer:
var
got
*
debugapi
.
BalancesResponse
var
got
*
debugapi
.
BalancesResponse
jsonhttptest
.
ResponseUnmarshal
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/balances"
,
nil
,
http
.
StatusOK
,
&
got
)
jsonhttptest
.
Request
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/balances"
,
http
.
StatusOK
,
jsonhttptest
.
WithUnmarshalJSONResponse
(
&
got
),
)
if
!
equalBalances
(
got
,
expected
)
{
if
!
equalBalances
(
got
,
expected
)
{
t
.
Errorf
(
"got balances: %v, expected: %v"
,
got
,
expected
)
t
.
Errorf
(
"got balances: %v, expected: %v"
,
got
,
expected
)
...
@@ -65,10 +67,12 @@ func TestBalancesError(t *testing.T) {
...
@@ -65,10 +67,12 @@ func TestBalancesError(t *testing.T) {
AccountingOpts
:
[]
mock
.
Option
{
mock
.
WithBalancesFunc
(
balancesFunc
)},
AccountingOpts
:
[]
mock
.
Option
{
mock
.
WithBalancesFunc
(
balancesFunc
)},
})
})
jsonhttptest
.
ResponseDirect
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/balances"
,
nil
,
http
.
StatusInternalServerError
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/balances"
,
http
.
StatusInternalServerError
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
debugapi
.
ErrCantBalances
,
Message
:
debugapi
.
ErrCantBalances
,
Code
:
http
.
StatusInternalServerError
,
Code
:
http
.
StatusInternalServerError
,
})
}),
)
}
}
func
TestBalancesPeers
(
t
*
testing
.
T
)
{
func
TestBalancesPeers
(
t
*
testing
.
T
)
{
...
@@ -80,10 +84,12 @@ func TestBalancesPeers(t *testing.T) {
...
@@ -80,10 +84,12 @@ func TestBalancesPeers(t *testing.T) {
AccountingOpts
:
[]
mock
.
Option
{
mock
.
WithBalanceFunc
(
balanceFunc
)},
AccountingOpts
:
[]
mock
.
Option
{
mock
.
WithBalanceFunc
(
balanceFunc
)},
})
})
jsonhttptest
.
ResponseDirect
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/balances/"
+
peer
,
nil
,
http
.
StatusOK
,
debugapi
.
BalanceResponse
{
jsonhttptest
.
Request
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/balances/"
+
peer
,
http
.
StatusOK
,
jsonhttptest
.
WithExpectedJSONResponse
(
debugapi
.
BalanceResponse
{
Peer
:
peer
,
Peer
:
peer
,
Balance
:
1000000000000000000
,
Balance
:
1000000000000000000
,
})
}),
)
}
}
func
TestBalancesPeersError
(
t
*
testing
.
T
)
{
func
TestBalancesPeersError
(
t
*
testing
.
T
)
{
...
@@ -96,10 +102,12 @@ func TestBalancesPeersError(t *testing.T) {
...
@@ -96,10 +102,12 @@ func TestBalancesPeersError(t *testing.T) {
AccountingOpts
:
[]
mock
.
Option
{
mock
.
WithBalanceFunc
(
balanceFunc
)},
AccountingOpts
:
[]
mock
.
Option
{
mock
.
WithBalanceFunc
(
balanceFunc
)},
})
})
jsonhttptest
.
ResponseDirect
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/balances/"
+
peer
,
nil
,
http
.
StatusInternalServerError
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/balances/"
+
peer
,
http
.
StatusInternalServerError
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
debugapi
.
ErrCantBalance
,
Message
:
debugapi
.
ErrCantBalance
,
Code
:
http
.
StatusInternalServerError
,
Code
:
http
.
StatusInternalServerError
,
})
}),
)
}
}
func
TestBalancesInvalidAddress
(
t
*
testing
.
T
)
{
func
TestBalancesInvalidAddress
(
t
*
testing
.
T
)
{
...
@@ -107,10 +115,12 @@ func TestBalancesInvalidAddress(t *testing.T) {
...
@@ -107,10 +115,12 @@ func TestBalancesInvalidAddress(t *testing.T) {
testServer
:=
newTestServer
(
t
,
testServerOptions
{})
testServer
:=
newTestServer
(
t
,
testServerOptions
{})
jsonhttptest
.
ResponseDirect
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/balances/"
+
peer
,
nil
,
http
.
StatusNotFound
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/balances/"
+
peer
,
http
.
StatusNotFound
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
debugapi
.
ErrInvaliAddress
,
Message
:
debugapi
.
ErrInvaliAddress
,
Code
:
http
.
StatusNotFound
,
Code
:
http
.
StatusNotFound
,
})
}),
)
}
}
func
equalBalances
(
a
,
b
*
debugapi
.
BalancesResponse
)
bool
{
func
equalBalances
(
a
,
b
*
debugapi
.
BalancesResponse
)
bool
{
...
...
pkg/debugapi/chunk_test.go
View file @
aa15b3a4
...
@@ -31,31 +31,39 @@ func TestHasChunkHandler(t *testing.T) {
...
@@ -31,31 +31,39 @@ func TestHasChunkHandler(t *testing.T) {
}
}
t
.
Run
(
"ok"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"ok"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/chunks/"
+
key
.
String
(),
nil
,
http
.
StatusOK
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/chunks/"
+
key
.
String
(),
http
.
StatusOK
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Code
:
http
.
StatusOK
,
Code
:
http
.
StatusOK
,
})
}),
)
})
})
t
.
Run
(
"not found"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"not found"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/chunks/abbbbb"
,
nil
,
http
.
StatusNotFound
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/chunks/abbbbb"
,
http
.
StatusNotFound
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
http
.
StatusText
(
http
.
StatusNotFound
),
Message
:
http
.
StatusText
(
http
.
StatusNotFound
),
Code
:
http
.
StatusNotFound
,
Code
:
http
.
StatusNotFound
,
})
}),
)
})
})
t
.
Run
(
"bad address"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"bad address"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/chunks/abcd1100zz"
,
nil
,
http
.
StatusBadRequest
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/chunks/abcd1100zz"
,
http
.
StatusBadRequest
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
"bad address"
,
Message
:
"bad address"
,
Code
:
http
.
StatusBadRequest
,
Code
:
http
.
StatusBadRequest
,
})
}),
)
})
})
t
.
Run
(
"remove-chunk"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"remove-chunk"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
testServer
.
Client
,
http
.
MethodDelete
,
"/chunks/"
+
key
.
String
(),
nil
,
http
.
StatusOK
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
testServer
.
Client
,
http
.
MethodDelete
,
"/chunks/"
+
key
.
String
(),
http
.
StatusOK
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Code
:
http
.
StatusOK
,
Code
:
http
.
StatusOK
,
})
}),
)
yes
,
err
:=
mockStorer
.
Has
(
context
.
Background
(),
key
)
yes
,
err
:=
mockStorer
.
Has
(
context
.
Background
(),
key
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatal
(
err
)
t
.
Fatal
(
err
)
...
@@ -67,10 +75,12 @@ func TestHasChunkHandler(t *testing.T) {
...
@@ -67,10 +75,12 @@ func TestHasChunkHandler(t *testing.T) {
t
.
Run
(
"remove-not-present-chunk"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"remove-not-present-chunk"
,
func
(
t
*
testing
.
T
)
{
notPresentChunkAddress
:=
"deadbeef"
notPresentChunkAddress
:=
"deadbeef"
jsonhttptest
.
ResponseDirect
(
t
,
testServer
.
Client
,
http
.
MethodDelete
,
"/chunks/"
+
notPresentChunkAddress
,
nil
,
http
.
StatusOK
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
testServer
.
Client
,
http
.
MethodDelete
,
"/chunks/"
+
notPresentChunkAddress
,
http
.
StatusOK
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Code
:
http
.
StatusOK
,
Code
:
http
.
StatusOK
,
})
}),
)
yes
,
err
:=
mockStorer
.
Has
(
context
.
Background
(),
swarm
.
NewAddress
([]
byte
(
notPresentChunkAddress
)))
yes
,
err
:=
mockStorer
.
Has
(
context
.
Background
(),
swarm
.
NewAddress
([]
byte
(
notPresentChunkAddress
)))
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatal
(
err
)
t
.
Fatal
(
err
)
...
...
pkg/debugapi/p2p_test.go
View file @
aa15b3a4
...
@@ -33,17 +33,21 @@ func TestAddresses(t *testing.T) {
...
@@ -33,17 +33,21 @@ func TestAddresses(t *testing.T) {
})
})
t
.
Run
(
"ok"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"ok"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/addresses"
,
nil
,
http
.
StatusOK
,
debugapi
.
AddressesResponse
{
jsonhttptest
.
Request
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/addresses"
,
http
.
StatusOK
,
jsonhttptest
.
WithExpectedJSONResponse
(
debugapi
.
AddressesResponse
{
Overlay
:
overlay
,
Overlay
:
overlay
,
Underlay
:
addresses
,
Underlay
:
addresses
,
})
}),
)
})
})
t
.
Run
(
"post method not allowed"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"post method not allowed"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
testServer
.
Client
,
http
.
MethodPost
,
"/addresses"
,
nil
,
http
.
StatusMethodNotAllowed
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
testServer
.
Client
,
http
.
MethodPost
,
"/addresses"
,
http
.
StatusMethodNotAllowed
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Code
:
http
.
StatusMethodNotAllowed
,
Code
:
http
.
StatusMethodNotAllowed
,
Message
:
http
.
StatusText
(
http
.
StatusMethodNotAllowed
),
Message
:
http
.
StatusText
(
http
.
StatusMethodNotAllowed
),
})
}),
)
})
})
}
}
...
@@ -56,8 +60,10 @@ func TestAddresses_error(t *testing.T) {
...
@@ -56,8 +60,10 @@ func TestAddresses_error(t *testing.T) {
})),
})),
})
})
jsonhttptest
.
ResponseDirect
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/addresses"
,
nil
,
http
.
StatusInternalServerError
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/addresses"
,
http
.
StatusInternalServerError
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Code
:
http
.
StatusInternalServerError
,
Code
:
http
.
StatusInternalServerError
,
Message
:
testErr
.
Error
(),
Message
:
testErr
.
Error
(),
})
}),
)
}
}
pkg/debugapi/peer_test.go
View file @
aa15b3a4
...
@@ -55,26 +55,32 @@ func TestConnect(t *testing.T) {
...
@@ -55,26 +55,32 @@ func TestConnect(t *testing.T) {
})
})
t
.
Run
(
"ok"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"ok"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
testServer
.
Client
,
http
.
MethodPost
,
"/connect"
+
underlay
,
nil
,
http
.
StatusOK
,
debugapi
.
PeerConnectResponse
{
jsonhttptest
.
Request
(
t
,
testServer
.
Client
,
http
.
MethodPost
,
"/connect"
+
underlay
,
http
.
StatusOK
,
jsonhttptest
.
WithExpectedJSONResponse
(
debugapi
.
PeerConnectResponse
{
Address
:
overlay
.
String
(),
Address
:
overlay
.
String
(),
})
}),
)
if
testServer
.
P2PMock
.
ConnectNotifyCalls
()
!=
1
{
if
testServer
.
P2PMock
.
ConnectNotifyCalls
()
!=
1
{
t
.
Fatal
(
"connect notify not called"
)
t
.
Fatal
(
"connect notify not called"
)
}
}
})
})
t
.
Run
(
"error"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"error"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
testServer
.
Client
,
http
.
MethodPost
,
"/connect"
+
errorUnderlay
,
nil
,
http
.
StatusInternalServerError
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
testServer
.
Client
,
http
.
MethodPost
,
"/connect"
+
errorUnderlay
,
http
.
StatusInternalServerError
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Code
:
http
.
StatusInternalServerError
,
Code
:
http
.
StatusInternalServerError
,
Message
:
testErr
.
Error
(),
Message
:
testErr
.
Error
(),
})
}),
)
})
})
t
.
Run
(
"get method not allowed"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"get method not allowed"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/connect"
+
underlay
,
nil
,
http
.
StatusMethodNotAllowed
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/connect"
+
underlay
,
http
.
StatusMethodNotAllowed
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Code
:
http
.
StatusMethodNotAllowed
,
Code
:
http
.
StatusMethodNotAllowed
,
Message
:
http
.
StatusText
(
http
.
StatusMethodNotAllowed
),
Message
:
http
.
StatusText
(
http
.
StatusMethodNotAllowed
),
})
}),
)
})
})
t
.
Run
(
"error - add peer"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"error - add peer"
,
func
(
t
*
testing
.
T
)
{
...
@@ -87,10 +93,12 @@ func TestConnect(t *testing.T) {
...
@@ -87,10 +93,12 @@ func TestConnect(t *testing.T) {
})),
})),
})
})
jsonhttptest
.
ResponseDirect
(
t
,
testServer
.
Client
,
http
.
MethodPost
,
"/connect"
+
errorUnderlay
,
nil
,
http
.
StatusInternalServerError
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
testServer
.
Client
,
http
.
MethodPost
,
"/connect"
+
errorUnderlay
,
http
.
StatusInternalServerError
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Code
:
http
.
StatusInternalServerError
,
Code
:
http
.
StatusInternalServerError
,
Message
:
testErr
.
Error
(),
Message
:
testErr
.
Error
(),
})
}),
)
})
})
}
}
...
@@ -115,31 +123,39 @@ func TestDisconnect(t *testing.T) {
...
@@ -115,31 +123,39 @@ func TestDisconnect(t *testing.T) {
})
})
t
.
Run
(
"ok"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"ok"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
testServer
.
Client
,
http
.
MethodDelete
,
"/peers/"
+
address
.
String
(),
nil
,
http
.
StatusOK
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
testServer
.
Client
,
http
.
MethodDelete
,
"/peers/"
+
address
.
String
(),
http
.
StatusOK
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Code
:
http
.
StatusOK
,
Code
:
http
.
StatusOK
,
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Message
:
http
.
StatusText
(
http
.
StatusOK
),
})
}),
)
})
})
t
.
Run
(
"unknown"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"unknown"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
testServer
.
Client
,
http
.
MethodDelete
,
"/peers/"
+
unknownAdddress
.
String
(),
nil
,
http
.
StatusBadRequest
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
testServer
.
Client
,
http
.
MethodDelete
,
"/peers/"
+
unknownAdddress
.
String
(),
http
.
StatusBadRequest
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Code
:
http
.
StatusBadRequest
,
Code
:
http
.
StatusBadRequest
,
Message
:
"peer not found"
,
Message
:
"peer not found"
,
})
}),
)
})
})
t
.
Run
(
"invalid peer address"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"invalid peer address"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
testServer
.
Client
,
http
.
MethodDelete
,
"/peers/invalid-address"
,
nil
,
http
.
StatusBadRequest
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
testServer
.
Client
,
http
.
MethodDelete
,
"/peers/invalid-address"
,
http
.
StatusBadRequest
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Code
:
http
.
StatusBadRequest
,
Code
:
http
.
StatusBadRequest
,
Message
:
"invalid peer address"
,
Message
:
"invalid peer address"
,
})
}),
)
})
})
t
.
Run
(
"error"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"error"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
testServer
.
Client
,
http
.
MethodDelete
,
"/peers/"
+
errorAddress
.
String
(),
nil
,
http
.
StatusInternalServerError
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
testServer
.
Client
,
http
.
MethodDelete
,
"/peers/"
+
errorAddress
.
String
(),
http
.
StatusInternalServerError
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Code
:
http
.
StatusInternalServerError
,
Code
:
http
.
StatusInternalServerError
,
Message
:
testErr
.
Error
(),
Message
:
testErr
.
Error
(),
})
}),
)
})
})
}
}
...
@@ -152,15 +168,19 @@ func TestPeer(t *testing.T) {
...
@@ -152,15 +168,19 @@ func TestPeer(t *testing.T) {
})
})
t
.
Run
(
"ok"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"ok"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/peers"
,
nil
,
http
.
StatusOK
,
debugapi
.
PeersResponse
{
jsonhttptest
.
Request
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/peers"
,
http
.
StatusOK
,
jsonhttptest
.
WithExpectedJSONResponse
(
debugapi
.
PeersResponse
{
Peers
:
[]
p2p
.
Peer
{{
Address
:
overlay
}},
Peers
:
[]
p2p
.
Peer
{{
Address
:
overlay
}},
})
}),
)
})
})
t
.
Run
(
"get method not allowed"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"get method not allowed"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
testServer
.
Client
,
http
.
MethodPost
,
"/peers"
,
nil
,
http
.
StatusMethodNotAllowed
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
testServer
.
Client
,
http
.
MethodPost
,
"/peers"
,
http
.
StatusMethodNotAllowed
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Code
:
http
.
StatusMethodNotAllowed
,
Code
:
http
.
StatusMethodNotAllowed
,
Message
:
http
.
StatusText
(
http
.
StatusMethodNotAllowed
),
Message
:
http
.
StatusText
(
http
.
StatusMethodNotAllowed
),
})
}),
)
})
})
}
}
pkg/debugapi/pin_test.go
View file @
aa15b3a4
...
@@ -45,131 +45,172 @@ func TestPinChunkHandler(t *testing.T) {
...
@@ -45,131 +45,172 @@ func TestPinChunkHandler(t *testing.T) {
// bad chunk address
// bad chunk address
t
.
Run
(
"pin-bad-address"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"pin-bad-address"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
debugTestServer
.
Client
,
http
.
MethodPost
,
"/chunks-pin/abcd1100zz"
,
nil
,
http
.
StatusBadRequest
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
debugTestServer
.
Client
,
http
.
MethodPost
,
"/chunks-pin/abcd1100zz"
,
http
.
StatusBadRequest
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
"bad address"
,
Message
:
"bad address"
,
Code
:
http
.
StatusBadRequest
,
Code
:
http
.
StatusBadRequest
,
})
}),
)
})
})
// list pins without anything pinned
// list pins without anything pinned
t
.
Run
(
"list-pins-zero-pins"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"list-pins-zero-pins"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
debugTestServer
.
Client
,
http
.
MethodGet
,
"/chunks-pin"
,
nil
,
http
.
StatusOK
,
debugapi
.
ListPinnedChunksResponse
{
jsonhttptest
.
Request
(
t
,
debugTestServer
.
Client
,
http
.
MethodGet
,
"/chunks-pin"
,
http
.
StatusOK
,
jsonhttptest
.
WithExpectedJSONResponse
(
debugapi
.
ListPinnedChunksResponse
{
Chunks
:
[]
debugapi
.
PinnedChunk
{},
Chunks
:
[]
debugapi
.
PinnedChunk
{},
})
}),
)
})
})
// pin a chunk which is not existing
// pin a chunk which is not existing
t
.
Run
(
"pin-absent-chunk"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"pin-absent-chunk"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
debugTestServer
.
Client
,
http
.
MethodPost
,
"/chunks-pin/123456"
,
nil
,
http
.
StatusNotFound
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
debugTestServer
.
Client
,
http
.
MethodPost
,
"/chunks-pin/123456"
,
http
.
StatusNotFound
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
http
.
StatusText
(
http
.
StatusNotFound
),
Message
:
http
.
StatusText
(
http
.
StatusNotFound
),
Code
:
http
.
StatusNotFound
,
Code
:
http
.
StatusNotFound
,
})
}),
)
})
})
// unpin on a chunk which is not pinned
// unpin on a chunk which is not pinned
t
.
Run
(
"unpin-while-not-pinned"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"unpin-while-not-pinned"
,
func
(
t
*
testing
.
T
)
{
// Post a chunk
// Post a chunk
jsonhttptest
.
ResponseDirect
(
t
,
bzzTestServer
,
http
.
MethodPost
,
resource
(
hash
),
bytes
.
NewReader
(
data
),
http
.
StatusOK
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
bzzTestServer
,
http
.
MethodPost
,
resource
(
hash
),
http
.
StatusOK
,
jsonhttptest
.
WithRequestBody
(
bytes
.
NewReader
(
data
)),
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Code
:
http
.
StatusOK
,
Code
:
http
.
StatusOK
,
})
}),
)
jsonhttptest
.
ResponseDirect
(
t
,
debugTestServer
.
Client
,
http
.
MethodDelete
,
"/chunks-pin/"
+
hash
.
String
(),
nil
,
http
.
StatusBadRequest
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
debugTestServer
.
Client
,
http
.
MethodDelete
,
"/chunks-pin/"
+
hash
.
String
(),
http
.
StatusBadRequest
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
"chunk is not yet pinned"
,
Message
:
"chunk is not yet pinned"
,
Code
:
http
.
StatusBadRequest
,
Code
:
http
.
StatusBadRequest
,
})
}),
)
})
})
// pin a existing chunk first time
// pin a existing chunk first time
t
.
Run
(
"pin-chunk-1"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"pin-chunk-1"
,
func
(
t
*
testing
.
T
)
{
// Post a chunk
// Post a chunk
jsonhttptest
.
ResponseDirect
(
t
,
bzzTestServer
,
http
.
MethodPost
,
resource
(
hash
),
bytes
.
NewReader
(
data
),
http
.
StatusOK
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
bzzTestServer
,
http
.
MethodPost
,
resource
(
hash
),
http
.
StatusOK
,
jsonhttptest
.
WithRequestBody
(
bytes
.
NewReader
(
data
)),
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Code
:
http
.
StatusOK
,
Code
:
http
.
StatusOK
,
})
}),
)
jsonhttptest
.
ResponseDirect
(
t
,
debugTestServer
.
Client
,
http
.
MethodPost
,
"/chunks-pin/"
+
hash
.
String
(),
nil
,
http
.
StatusOK
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
debugTestServer
.
Client
,
http
.
MethodPost
,
"/chunks-pin/"
+
hash
.
String
(),
http
.
StatusOK
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Code
:
http
.
StatusOK
,
Code
:
http
.
StatusOK
,
})
}),
)
// Check is the chunk is pinned once
// Check is the chunk is pinned once
jsonhttptest
.
ResponseDirect
(
t
,
debugTestServer
.
Client
,
http
.
MethodGet
,
"/chunks-pin/"
+
hash
.
String
(),
nil
,
http
.
StatusOK
,
debugapi
.
PinnedChunk
{
jsonhttptest
.
Request
(
t
,
debugTestServer
.
Client
,
http
.
MethodGet
,
"/chunks-pin/"
+
hash
.
String
(),
http
.
StatusOK
,
jsonhttptest
.
WithExpectedJSONResponse
(
debugapi
.
PinnedChunk
{
Address
:
swarm
.
MustParseHexAddress
(
"aabbcc"
),
Address
:
swarm
.
MustParseHexAddress
(
"aabbcc"
),
PinCounter
:
1
,
PinCounter
:
1
,
})
}),
)
})
})
// pin a existing chunk second time
// pin a existing chunk second time
t
.
Run
(
"pin-chunk-2"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"pin-chunk-2"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
debugTestServer
.
Client
,
http
.
MethodPost
,
"/chunks-pin/"
+
hash
.
String
(),
nil
,
http
.
StatusOK
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
debugTestServer
.
Client
,
http
.
MethodPost
,
"/chunks-pin/"
+
hash
.
String
(),
http
.
StatusOK
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Code
:
http
.
StatusOK
,
Code
:
http
.
StatusOK
,
})
}),
)
// Check is the chunk is pinned twice
// Check is the chunk is pinned twice
jsonhttptest
.
ResponseDirect
(
t
,
debugTestServer
.
Client
,
http
.
MethodGet
,
"/chunks-pin/"
+
hash
.
String
(),
nil
,
http
.
StatusOK
,
debugapi
.
PinnedChunk
{
jsonhttptest
.
Request
(
t
,
debugTestServer
.
Client
,
http
.
MethodGet
,
"/chunks-pin/"
+
hash
.
String
(),
http
.
StatusOK
,
jsonhttptest
.
WithExpectedJSONResponse
(
debugapi
.
PinnedChunk
{
Address
:
swarm
.
MustParseHexAddress
(
"aabbcc"
),
Address
:
swarm
.
MustParseHexAddress
(
"aabbcc"
),
PinCounter
:
2
,
PinCounter
:
2
,
})
}),
)
})
})
// unpin a chunk first time
// unpin a chunk first time
t
.
Run
(
"unpin-chunk-1"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"unpin-chunk-1"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
debugTestServer
.
Client
,
http
.
MethodDelete
,
"/chunks-pin/"
+
hash
.
String
(),
nil
,
http
.
StatusOK
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
debugTestServer
.
Client
,
http
.
MethodDelete
,
"/chunks-pin/"
+
hash
.
String
(),
http
.
StatusOK
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Code
:
http
.
StatusOK
,
Code
:
http
.
StatusOK
,
})
}),
)
// Check is the chunk is pinned once
// Check is the chunk is pinned once
jsonhttptest
.
ResponseDirect
(
t
,
debugTestServer
.
Client
,
http
.
MethodGet
,
"/chunks-pin/"
+
hash
.
String
(),
nil
,
http
.
StatusOK
,
debugapi
.
PinnedChunk
{
jsonhttptest
.
Request
(
t
,
debugTestServer
.
Client
,
http
.
MethodGet
,
"/chunks-pin/"
+
hash
.
String
(),
http
.
StatusOK
,
jsonhttptest
.
WithExpectedJSONResponse
(
debugapi
.
PinnedChunk
{
Address
:
swarm
.
MustParseHexAddress
(
"aabbcc"
),
Address
:
swarm
.
MustParseHexAddress
(
"aabbcc"
),
PinCounter
:
1
,
PinCounter
:
1
,
})
}),
)
})
})
// unpin a chunk second time
// unpin a chunk second time
t
.
Run
(
"unpin-chunk-2"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"unpin-chunk-2"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
debugTestServer
.
Client
,
http
.
MethodDelete
,
"/chunks-pin/"
+
hash
.
String
(),
nil
,
http
.
StatusOK
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
debugTestServer
.
Client
,
http
.
MethodDelete
,
"/chunks-pin/"
+
hash
.
String
(),
http
.
StatusOK
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Code
:
http
.
StatusOK
,
Code
:
http
.
StatusOK
,
})
}),
)
// Check if the chunk is removed from the pinIndex
// Check if the chunk is removed from the pinIndex
jsonhttptest
.
ResponseDirect
(
t
,
debugTestServer
.
Client
,
http
.
MethodGet
,
"/chunks-pin/"
+
hash
.
String
(),
nil
,
http
.
StatusNotFound
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
debugTestServer
.
Client
,
http
.
MethodGet
,
"/chunks-pin/"
+
hash
.
String
(),
http
.
StatusNotFound
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
http
.
StatusText
(
http
.
StatusNotFound
),
Message
:
http
.
StatusText
(
http
.
StatusNotFound
),
Code
:
http
.
StatusNotFound
,
Code
:
http
.
StatusNotFound
,
})
}),
)
})
})
// Add 2 chunks, pin it and check if they show up in the list
// Add 2 chunks, pin it and check if they show up in the list
t
.
Run
(
"list-chunks"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"list-chunks"
,
func
(
t
*
testing
.
T
)
{
// Post a chunk
// Post a chunk
jsonhttptest
.
ResponseDirect
(
t
,
bzzTestServer
,
http
.
MethodPost
,
resource
(
hash
),
bytes
.
NewReader
(
data
),
http
.
StatusOK
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
bzzTestServer
,
http
.
MethodPost
,
resource
(
hash
),
http
.
StatusOK
,
jsonhttptest
.
WithRequestBody
(
bytes
.
NewReader
(
data
)),
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Code
:
http
.
StatusOK
,
Code
:
http
.
StatusOK
,
})
}),
)
jsonhttptest
.
ResponseDirect
(
t
,
debugTestServer
.
Client
,
http
.
MethodPost
,
"/chunks-pin/"
+
hash
.
String
(),
nil
,
http
.
StatusOK
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
debugTestServer
.
Client
,
http
.
MethodPost
,
"/chunks-pin/"
+
hash
.
String
(),
http
.
StatusOK
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Code
:
http
.
StatusOK
,
Code
:
http
.
StatusOK
,
})
}),
)
// post another chunk
// post another chunk
hash2
:=
swarm
.
MustParseHexAddress
(
"ddeeff"
)
hash2
:=
swarm
.
MustParseHexAddress
(
"ddeeff"
)
data2
:=
[]
byte
(
"eagle"
)
data2
:=
[]
byte
(
"eagle"
)
mockValidator
.
AddPair
(
hash2
,
data2
)
mockValidator
.
AddPair
(
hash2
,
data2
)
jsonhttptest
.
ResponseDirect
(
t
,
bzzTestServer
,
http
.
MethodPost
,
resource
(
hash2
),
bytes
.
NewReader
(
data2
),
http
.
StatusOK
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
bzzTestServer
,
http
.
MethodPost
,
resource
(
hash2
),
http
.
StatusOK
,
jsonhttptest
.
WithRequestBody
(
bytes
.
NewReader
(
data2
)),
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Code
:
http
.
StatusOK
,
Code
:
http
.
StatusOK
,
})
}),
jsonhttptest
.
ResponseDirect
(
t
,
debugTestServer
.
Client
,
http
.
MethodPost
,
"/chunks-pin/"
+
hash2
.
String
(),
nil
,
http
.
StatusOK
,
jsonhttp
.
StatusResponse
{
)
jsonhttptest
.
Request
(
t
,
debugTestServer
.
Client
,
http
.
MethodPost
,
"/chunks-pin/"
+
hash2
.
String
(),
http
.
StatusOK
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Message
:
http
.
StatusText
(
http
.
StatusOK
),
Code
:
http
.
StatusOK
,
Code
:
http
.
StatusOK
,
})
}),
)
jsonhttptest
.
ResponseDirect
(
t
,
debugTestServer
.
Client
,
http
.
MethodGet
,
"/chunks-pin"
,
nil
,
http
.
StatusOK
,
debugapi
.
ListPinnedChunksResponse
{
jsonhttptest
.
Request
(
t
,
debugTestServer
.
Client
,
http
.
MethodGet
,
"/chunks-pin"
,
http
.
StatusOK
,
jsonhttptest
.
WithExpectedJSONResponse
(
debugapi
.
ListPinnedChunksResponse
{
Chunks
:
[]
debugapi
.
PinnedChunk
{
Chunks
:
[]
debugapi
.
PinnedChunk
{
{
{
Address
:
swarm
.
MustParseHexAddress
(
"aabbcc"
),
Address
:
swarm
.
MustParseHexAddress
(
"aabbcc"
),
...
@@ -180,6 +221,7 @@ func TestPinChunkHandler(t *testing.T) {
...
@@ -180,6 +221,7 @@ func TestPinChunkHandler(t *testing.T) {
PinCounter
:
1
,
PinCounter
:
1
,
},
},
},
},
})
}),
)
})
})
}
}
pkg/debugapi/pingpong_test.go
View file @
aa15b3a4
...
@@ -41,36 +41,46 @@ func TestPingpong(t *testing.T) {
...
@@ -41,36 +41,46 @@ func TestPingpong(t *testing.T) {
})
})
t
.
Run
(
"ok"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"ok"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
ts
.
Client
,
http
.
MethodPost
,
"/pingpong/"
+
peerID
.
String
(),
nil
,
http
.
StatusOK
,
debugapi
.
PingpongResponse
{
jsonhttptest
.
Request
(
t
,
ts
.
Client
,
http
.
MethodPost
,
"/pingpong/"
+
peerID
.
String
(),
http
.
StatusOK
,
jsonhttptest
.
WithExpectedJSONResponse
(
debugapi
.
PingpongResponse
{
RTT
:
rtt
.
String
(),
RTT
:
rtt
.
String
(),
})
}),
)
})
})
t
.
Run
(
"peer not found"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"peer not found"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
ts
.
Client
,
http
.
MethodPost
,
"/pingpong/"
+
unknownPeerID
.
String
(),
nil
,
http
.
StatusNotFound
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
ts
.
Client
,
http
.
MethodPost
,
"/pingpong/"
+
unknownPeerID
.
String
(),
http
.
StatusNotFound
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Code
:
http
.
StatusNotFound
,
Code
:
http
.
StatusNotFound
,
Message
:
"peer not found"
,
Message
:
"peer not found"
,
})
}),
)
})
})
t
.
Run
(
"invalid peer address"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"invalid peer address"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
ts
.
Client
,
http
.
MethodPost
,
"/pingpong/invalid-address"
,
nil
,
http
.
StatusBadRequest
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
ts
.
Client
,
http
.
MethodPost
,
"/pingpong/invalid-address"
,
http
.
StatusBadRequest
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Code
:
http
.
StatusBadRequest
,
Code
:
http
.
StatusBadRequest
,
Message
:
"invalid peer address"
,
Message
:
"invalid peer address"
,
})
}),
)
})
})
t
.
Run
(
"error"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"error"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
ts
.
Client
,
http
.
MethodPost
,
"/pingpong/"
+
errorPeerID
.
String
(),
nil
,
http
.
StatusInternalServerError
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
ts
.
Client
,
http
.
MethodPost
,
"/pingpong/"
+
errorPeerID
.
String
(),
http
.
StatusInternalServerError
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Code
:
http
.
StatusInternalServerError
,
Code
:
http
.
StatusInternalServerError
,
Message
:
http
.
StatusText
(
http
.
StatusInternalServerError
),
// do not leak internal error
Message
:
http
.
StatusText
(
http
.
StatusInternalServerError
),
// do not leak internal error
})
}),
)
})
})
t
.
Run
(
"get method not allowed"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"get method not allowed"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
ts
.
Client
,
http
.
MethodGet
,
"/pingpong/"
+
peerID
.
String
(),
nil
,
http
.
StatusMethodNotAllowed
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
ts
.
Client
,
http
.
MethodGet
,
"/pingpong/"
+
peerID
.
String
(),
http
.
StatusMethodNotAllowed
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Code
:
http
.
StatusMethodNotAllowed
,
Code
:
http
.
StatusMethodNotAllowed
,
Message
:
http
.
StatusText
(
http
.
StatusMethodNotAllowed
),
Message
:
http
.
StatusText
(
http
.
StatusMethodNotAllowed
),
})
}),
)
})
})
}
}
pkg/debugapi/status_test.go
View file @
aa15b3a4
...
@@ -15,15 +15,19 @@ import (
...
@@ -15,15 +15,19 @@ import (
func
TestHealth
(
t
*
testing
.
T
)
{
func
TestHealth
(
t
*
testing
.
T
)
{
testServer
:=
newTestServer
(
t
,
testServerOptions
{})
testServer
:=
newTestServer
(
t
,
testServerOptions
{})
jsonhttptest
.
ResponseDirect
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/health"
,
nil
,
http
.
StatusOK
,
debugapi
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/health"
,
http
.
StatusOK
,
jsonhttptest
.
WithExpectedJSONResponse
(
debugapi
.
StatusResponse
{
Status
:
"ok"
,
Status
:
"ok"
,
})
}),
)
}
}
func
TestReadiness
(
t
*
testing
.
T
)
{
func
TestReadiness
(
t
*
testing
.
T
)
{
testServer
:=
newTestServer
(
t
,
testServerOptions
{})
testServer
:=
newTestServer
(
t
,
testServerOptions
{})
jsonhttptest
.
ResponseDirect
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/readiness"
,
nil
,
http
.
StatusOK
,
debugapi
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/readiness"
,
http
.
StatusOK
,
jsonhttptest
.
WithExpectedJSONResponse
(
debugapi
.
StatusResponse
{
Status
:
"ok"
,
Status
:
"ok"
,
})
}),
)
}
}
pkg/debugapi/topology_test.go
View file @
aa15b3a4
...
@@ -27,9 +27,11 @@ func TestTopologyOK(t *testing.T) {
...
@@ -27,9 +27,11 @@ func TestTopologyOK(t *testing.T) {
TopologyOpts
:
[]
topmock
.
Option
{
topmock
.
WithMarshalJSONFunc
(
marshalFunc
)},
TopologyOpts
:
[]
topmock
.
Option
{
topmock
.
WithMarshalJSONFunc
(
marshalFunc
)},
})
})
jsonhttptest
.
ResponseDirect
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/topology"
,
nil
,
http
.
StatusOK
,
topologyResponse
{
jsonhttptest
.
Request
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/topology"
,
http
.
StatusOK
,
jsonhttptest
.
WithExpectedJSONResponse
(
topologyResponse
{
Topology
:
"abcd"
,
Topology
:
"abcd"
,
})
}),
)
}
}
func
TestTopologyError
(
t
*
testing
.
T
)
{
func
TestTopologyError
(
t
*
testing
.
T
)
{
...
@@ -40,8 +42,10 @@ func TestTopologyError(t *testing.T) {
...
@@ -40,8 +42,10 @@ func TestTopologyError(t *testing.T) {
TopologyOpts
:
[]
topmock
.
Option
{
topmock
.
WithMarshalJSONFunc
(
marshalFunc
)},
TopologyOpts
:
[]
topmock
.
Option
{
topmock
.
WithMarshalJSONFunc
(
marshalFunc
)},
})
})
jsonhttptest
.
ResponseDirect
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/topology"
,
nil
,
http
.
StatusInternalServerError
,
jsonhttp
.
StatusResponse
{
jsonhttptest
.
Request
(
t
,
testServer
.
Client
,
http
.
MethodGet
,
"/topology"
,
http
.
StatusInternalServerError
,
jsonhttptest
.
WithExpectedJSONResponse
(
jsonhttp
.
StatusResponse
{
Message
:
"error"
,
Message
:
"error"
,
Code
:
http
.
StatusInternalServerError
,
Code
:
http
.
StatusInternalServerError
,
})
}),
)
}
}
pkg/debugapi/welcome_message_test.go
View file @
aa15b3a4
...
@@ -25,9 +25,11 @@ func TestGetWelcomeMessage(t *testing.T) {
...
@@ -25,9 +25,11 @@ func TestGetWelcomeMessage(t *testing.T) {
return
DefaultTestWelcomeMessage
return
DefaultTestWelcomeMessage
}))})
}))})
jsonhttptest
.
ResponseDirect
(
t
,
srv
.
Client
,
http
.
MethodGet
,
"/welcome-message"
,
nil
,
http
.
StatusOK
,
debugapi
.
WelcomeMessageResponse
{
jsonhttptest
.
Request
(
t
,
srv
.
Client
,
http
.
MethodGet
,
"/welcome-message"
,
http
.
StatusOK
,
jsonhttptest
.
WithExpectedJSONResponse
(
debugapi
.
WelcomeMessageResponse
{
WelcomeMesssage
:
DefaultTestWelcomeMessage
,
WelcomeMesssage
:
DefaultTestWelcomeMessage
,
})
}),
)
}
}
func
TestSetWelcomeMessage
(
t
*
testing
.
T
)
{
func
TestSetWelcomeMessage
(
t
*
testing
.
T
)
{
...
@@ -81,7 +83,10 @@ func TestSetWelcomeMessage(t *testing.T) {
...
@@ -81,7 +83,10 @@ func TestSetWelcomeMessage(t *testing.T) {
Message
:
tC
.
wantMessage
,
Message
:
tC
.
wantMessage
,
Code
:
tC
.
wantStatus
,
Code
:
tC
.
wantStatus
,
}
}
jsonhttptest
.
ResponseDirect
(
t
,
srv
.
Client
,
http
.
MethodPost
,
testURL
,
body
,
tC
.
wantStatus
,
wantResponse
)
jsonhttptest
.
Request
(
t
,
srv
.
Client
,
http
.
MethodPost
,
testURL
,
tC
.
wantStatus
,
jsonhttptest
.
WithRequestBody
(
body
),
jsonhttptest
.
WithExpectedJSONResponse
(
wantResponse
),
)
if
!
tC
.
wantFail
{
if
!
tC
.
wantFail
{
got
:=
srv
.
P2PMock
.
GetWelcomeMessage
()
got
:=
srv
.
P2PMock
.
GetWelcomeMessage
()
if
got
!=
tC
.
message
{
if
got
!=
tC
.
message
{
...
@@ -113,7 +118,10 @@ func TestSetWelcomeMessageInternalServerError(t *testing.T) {
...
@@ -113,7 +118,10 @@ func TestSetWelcomeMessageInternalServerError(t *testing.T) {
Message
:
testError
.
Error
(),
Message
:
testError
.
Error
(),
Code
:
wantCode
,
Code
:
wantCode
,
}
}
jsonhttptest
.
ResponseDirect
(
t
,
srv
.
Client
,
http
.
MethodPost
,
testURL
,
body
,
wantCode
,
wantResp
)
jsonhttptest
.
Request
(
t
,
srv
.
Client
,
http
.
MethodPost
,
testURL
,
wantCode
,
jsonhttptest
.
WithRequestBody
(
body
),
jsonhttptest
.
WithExpectedJSONResponse
(
wantResp
),
)
})
})
}
}
pkg/jsonhttp/jsonhttptest/jsonhttptest.go
View file @
aa15b3a4
This diff is collapsed.
Click to expand it.
pkg/jsonhttp/jsonhttptest/jsonhttptest_test.go
View file @
aa15b3a4
This diff is collapsed.
Click to expand it.
pkg/jsonhttp/jsonhttptest/testing_mock_test.go
0 → 100644
View file @
aa15b3a4
// 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
jsonhttptest_test
import
(
"errors"
"fmt"
"testing"
)
// assert is a test helper that validates a functionality of another helper
// function by mocking Errorf, Fatal and Helper methods on testing.TB.
func
assert
(
t
*
testing
.
T
,
wantError
,
wantFatal
string
,
f
func
(
m
*
mock
))
{
t
.
Helper
()
defer
func
()
{
if
v
:=
recover
();
v
!=
nil
{
if
err
,
ok
:=
v
.
(
error
);
ok
&&
errors
.
Is
(
err
,
errFailed
)
{
return
// execution of the goroutine is stopped by a mock Fatal function
}
t
.
Fatalf
(
"panic: %v"
,
v
)
}
}()
m
:=
&
mock
{
wantError
:
wantError
,
wantFatal
:
wantFatal
,
}
f
(
m
)
if
!
m
.
isHelper
{
// Request function is tested and it must be always a helper
t
.
Error
(
"not a helper function"
)
}
if
m
.
gotError
!=
m
.
wantError
{
t
.
Errorf
(
"got error %q, want %q"
,
m
.
gotError
,
m
.
wantError
)
}
if
m
.
gotFatal
!=
m
.
wantFatal
{
t
.
Errorf
(
"got error %v, want %v"
,
m
.
gotFatal
,
m
.
wantFatal
)
}
}
// mock provides the same interface as testing.TB with overridden Errorf, Fatal
// and Heleper methods.
type
mock
struct
{
testing
.
TB
isHelper
bool
gotError
string
wantError
string
gotFatal
string
wantFatal
string
}
func
(
m
*
mock
)
Helper
()
{
m
.
isHelper
=
true
}
func
(
m
*
mock
)
Errorf
(
format
string
,
args
...
interface
{})
{
m
.
gotError
=
fmt
.
Sprintf
(
format
,
args
...
)
}
func
(
m
*
mock
)
Fatal
(
args
...
interface
{})
{
m
.
gotFatal
=
fmt
.
Sprint
(
args
...
)
panic
(
errFailed
)
// terminate the goroutine to detect it in the assert function
}
var
errFailed
=
errors
.
New
(
"failed"
)
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