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
473fb58f
Commit
473fb58f
authored
Jan 30, 2020
by
Janos Guljas
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add jsonhttptest tests
parent
3ddbd836
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
76 additions
and
0 deletions
+76
-0
jsonhttptest_test.go
pkg/jsonhttp/jsonhttptest/jsonhttptest_test.go
+76
-0
No files found.
pkg/jsonhttp/jsonhttptest/jsonhttptest_test.go
0 → 100644
View file @
473fb58f
// 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
(
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/ethersphere/bee/pkg/jsonhttp"
"github.com/ethersphere/bee/pkg/jsonhttp/jsonhttptest"
)
func
TestResponse
(
t
*
testing
.
T
)
{
type
response
struct
{
Message
string
`json:"message"`
}
message
:=
"text"
wantMethod
,
wantPath
,
wantBody
:=
http
.
MethodPatch
,
"/testing"
,
"request body"
var
gotMethod
,
gotPath
,
gotBody
string
s
:=
httptest
.
NewServer
(
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
gotMethod
=
r
.
Method
gotPath
=
r
.
URL
.
Path
b
,
err
:=
ioutil
.
ReadAll
(
r
.
Body
)
if
err
!=
nil
{
jsonhttp
.
InternalServerError
(
w
,
err
)
return
}
gotBody
=
string
(
b
)
jsonhttp
.
Created
(
w
,
response
{
Message
:
message
,
})
}))
defer
s
.
Close
()
c
:=
s
.
Client
()
t
.
Run
(
"direct"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
c
,
wantMethod
,
s
.
URL
+
wantPath
,
strings
.
NewReader
(
wantBody
),
http
.
StatusCreated
,
response
{
Message
:
message
,
})
if
gotMethod
!=
wantMethod
{
t
.
Errorf
(
"got method %s, want %s"
,
gotMethod
,
wantMethod
)
}
if
gotPath
!=
wantPath
{
t
.
Errorf
(
"got path %s, want %s"
,
gotPath
,
wantPath
)
}
if
gotBody
!=
wantBody
{
t
.
Errorf
(
"got body %s, want %s"
,
gotBody
,
wantBody
)
}
})
t
.
Run
(
"unmarshal"
,
func
(
t
*
testing
.
T
)
{
var
r
response
jsonhttptest
.
ResponseUnmarshal
(
t
,
c
,
wantMethod
,
s
.
URL
+
wantPath
,
strings
.
NewReader
(
wantBody
),
http
.
StatusCreated
,
&
r
)
if
gotMethod
!=
wantMethod
{
t
.
Errorf
(
"got method %s, want %s"
,
gotMethod
,
wantMethod
)
}
if
gotPath
!=
wantPath
{
t
.
Errorf
(
"got path %s, want %s"
,
gotPath
,
wantPath
)
}
if
gotBody
!=
wantBody
{
t
.
Errorf
(
"got body %s, want %s"
,
gotBody
,
wantBody
)
}
if
r
.
Message
!=
message
{
t
.
Errorf
(
"got messag %s, want %s"
,
r
.
Message
,
message
)
}
})
}
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