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
5ec4f25e
Commit
5ec4f25e
authored
Jan 27, 2020
by
Janos Guljas
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
improve jsonhttptest
parent
84040016
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
28 deletions
+21
-28
jsonhttptest.go
pkg/jsonhttp/jsonhttptest/jsonhttptest.go
+21
-28
No files found.
pkg/jsonhttp/jsonhttptest/jsonhttptest.go
View file @
5ec4f25e
...
@@ -16,56 +16,49 @@ import (
...
@@ -16,56 +16,49 @@ import (
func
ResponseDirect
(
t
*
testing
.
T
,
client
*
http
.
Client
,
method
,
url
string
,
body
io
.
Reader
,
responseCode
int
,
response
interface
{})
{
func
ResponseDirect
(
t
*
testing
.
T
,
client
*
http
.
Client
,
method
,
url
string
,
body
io
.
Reader
,
responseCode
int
,
response
interface
{})
{
t
.
Helper
()
t
.
Helper
()
resp
,
err
:=
request
(
client
,
method
,
url
,
body
,
responseCode
)
resp
:=
request
(
t
,
client
,
method
,
url
,
body
,
responseCode
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
defer
resp
.
Body
.
Close
()
defer
resp
.
Body
.
Close
()
if
resp
.
StatusCode
!=
responseCode
{
got
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
t
.
Errorf
(
"got response status %s, want %v %s"
,
resp
.
Status
,
responseCode
,
http
.
StatusText
(
responseCode
))
}
gotBytes
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatal
(
err
)
t
.
Fatal
(
err
)
}
}
wantBytes
,
err
:=
json
.
Marshal
(
response
)
got
=
bytes
.
TrimSpace
(
got
)
want
,
err
:=
json
.
Marshal
(
response
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Error
(
err
)
t
.
Error
(
err
)
}
}
got
:=
string
(
bytes
.
TrimSpace
(
gotBytes
))
if
!
bytes
.
Equal
(
got
,
want
)
{
want
:=
string
(
wantBytes
)
t
.
Errorf
(
"got response %s, want %s"
,
string
(
got
),
string
(
want
))
if
got
!=
want
{
t
.
Errorf
(
"got response %s, want %s"
,
got
,
want
)
}
}
}
}
func
ResponseUnmarshal
(
t
*
testing
.
T
,
client
*
http
.
Client
,
method
,
url
string
,
body
io
.
Reader
,
responseCode
int
,
response
interface
{})
{
func
ResponseUnmarshal
(
t
*
testing
.
T
,
client
*
http
.
Client
,
method
,
url
string
,
body
io
.
Reader
,
responseCode
int
,
response
interface
{})
{
t
.
Helper
()
t
.
Helper
()
resp
,
err
:=
request
(
client
,
method
,
url
,
body
,
responseCode
)
resp
:=
request
(
t
,
client
,
method
,
url
,
body
,
responseCode
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
defer
resp
.
Body
.
Close
()
defer
resp
.
Body
.
Close
()
if
resp
.
StatusCode
!=
responseCode
{
if
err
:=
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
&
response
);
err
!=
nil
{
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
)
t
.
Fatal
(
err
)
}
}
}
}
func
request
(
client
*
http
.
Client
,
method
,
url
string
,
body
io
.
Reader
,
responseCode
int
)
(
resp
*
http
.
Response
,
err
error
)
{
func
request
(
t
*
testing
.
T
,
client
*
http
.
Client
,
method
,
url
string
,
body
io
.
Reader
,
responseCode
int
)
*
http
.
Response
{
t
.
Helper
()
req
,
err
:=
http
.
NewRequest
(
method
,
url
,
body
)
req
,
err
:=
http
.
NewRequest
(
method
,
url
,
body
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
t
.
Fatal
(
err
)
}
resp
,
err
:=
client
.
Do
(
req
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
resp
.
StatusCode
!=
responseCode
{
t
.
Errorf
(
"got response status %s, want %v %s"
,
resp
.
Status
,
responseCode
,
http
.
StatusText
(
responseCode
))
}
}
return
client
.
Do
(
req
)
return
resp
}
}
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