Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
155d1b6f
Commit
155d1b6f
authored
Mar 01, 2017
by
Kamil Trzcinski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added test for new helper methods
parent
81fc0554
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
55 additions
and
0 deletions
+55
-0
internal/helper/helpers.go
internal/helper/helpers.go
+1
-0
internal/helper/helpers_test.go
internal/helper/helpers_test.go
+54
-0
No files found.
internal/helper/helpers.go
View file @
155d1b6f
...
@@ -186,5 +186,6 @@ func ReadRequestBody(w http.ResponseWriter, r *http.Request, maxBodySize int64)
...
@@ -186,5 +186,6 @@ func ReadRequestBody(w http.ResponseWriter, r *http.Request, maxBodySize int64)
func
CloneRequestWithNewBody
(
r
*
http
.
Request
,
body
[]
byte
)
*
http
.
Request
{
func
CloneRequestWithNewBody
(
r
*
http
.
Request
,
body
[]
byte
)
*
http
.
Request
{
newReq
:=
*
r
newReq
:=
*
r
newReq
.
Body
=
ioutil
.
NopCloser
(
bytes
.
NewReader
(
body
))
newReq
.
Body
=
ioutil
.
NopCloser
(
bytes
.
NewReader
(
body
))
newReq
.
ContentLength
=
int64
(
len
(
body
))
return
&
newReq
return
&
newReq
}
}
internal/helper/helpers_test.go
View file @
155d1b6f
package
helper
package
helper
import
(
import
(
"bytes"
"io"
"net/http"
"net/http"
"net/http/httptest"
"testing"
"testing"
)
)
...
@@ -47,3 +50,54 @@ func TestSetForwardedForGeneratesHeader(t *testing.T) {
...
@@ -47,3 +50,54 @@ func TestSetForwardedForGeneratesHeader(t *testing.T) {
}
}
}
}
}
}
func
TestReadRequestBody
(
t
*
testing
.
T
)
{
data
:=
[]
byte
(
"123456"
)
rw
:=
httptest
.
NewRecorder
()
req
:=
httptest
.
NewRequest
(
"POST"
,
"/test"
,
bytes
.
NewBuffer
(
data
))
result
,
err
:=
ReadRequestBody
(
rw
,
req
,
1000
)
if
!
bytes
.
Equal
(
result
,
data
)
{
t
.
Fatalf
(
"Expected to receive the same result, got %v"
,
result
)
}
if
err
!=
nil
{
t
.
Fatalf
(
"Expected to not receive error from reading"
)
}
}
func
TestReadRequestBodyLimit
(
t
*
testing
.
T
)
{
data
:=
[]
byte
(
"123456"
)
rw
:=
httptest
.
NewRecorder
()
req
:=
httptest
.
NewRequest
(
"POST"
,
"/test"
,
bytes
.
NewBuffer
(
data
))
result
,
err
:=
ReadRequestBody
(
rw
,
req
,
2
)
if
len
(
result
)
!=
0
{
t
.
Fatalf
(
"Expected empty result, got %v"
,
result
)
}
if
err
==
nil
{
t
.
Fatalf
(
"Expected to receive error from reading"
)
}
}
func
TestCloneRequestWithBody
(
t
*
testing
.
T
)
{
input
:=
[]
byte
(
"test"
)
newInput
:=
[]
byte
(
"new body"
)
req
:=
httptest
.
NewRequest
(
"POST"
,
"/test"
,
bytes
.
NewBuffer
(
input
))
newReq
:=
CloneRequestWithNewBody
(
req
,
newInput
)
if
req
==
newReq
{
t
.
Fatalf
(
"Expected a new request to be different from old one"
)
}
if
req
.
Body
==
newReq
.
Body
{
t
.
Fatalf
(
"Expected the body object to be different"
)
}
if
newReq
.
ContentLength
!=
int64
(
len
(
newInput
))
{
t
.
Fatalf
(
"Expected the Content-Length to be updated"
)
}
var
buffer
bytes
.
Buffer
io
.
Copy
(
&
buffer
,
newReq
.
Body
)
if
!
bytes
.
Equal
(
buffer
.
Bytes
(),
newInput
)
{
t
.
Fatal
(
"Expected the readed body to be the same as passed to function"
)
}
}
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