Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
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
0
Merge Requests
0
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
Łukasz Nowak
caddy
Commits
bba1059e
Commit
bba1059e
authored
Aug 18, 2016
by
Tw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
log: add log request body test
Signed-off-by:
Tw
<
tw19881113@gmail.com
>
parent
1d3212a5
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
59 additions
and
1 deletion
+59
-1
caddyhttp/httpserver/replacer.go
caddyhttp/httpserver/replacer.go
+2
-1
caddyhttp/log/log_test.go
caddyhttp/log/log_test.go
+57
-0
No files found.
caddyhttp/httpserver/replacer.go
View file @
bba1059e
...
...
@@ -276,5 +276,6 @@ const (
headerContentType
=
"Content-Type"
contentTypeJSON
=
"application/json"
contentTypeXML
=
"application/xml"
maxLogBodySize
=
100
*
1024
// MaxLogBodySize limits the size of logged request's body
MaxLogBodySize
=
100
*
1024
)
caddyhttp/log/log_test.go
View file @
bba1059e
...
...
@@ -2,6 +2,7 @@ package log
import
(
"bytes"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
...
...
@@ -60,3 +61,59 @@ func TestLoggedStatus(t *testing.T) {
t
.
Errorf
(
"Expected the log entry to contain 'foobar' (custom placeholder), but it didn't: %s"
,
logged
)
}
}
func
TestLogRequestBody
(
t
*
testing
.
T
)
{
var
got
bytes
.
Buffer
logger
:=
Logger
{
Rules
:
[]
Rule
{{
PathScope
:
"/"
,
Format
:
"{request_body}"
,
Log
:
log
.
New
(
&
got
,
""
,
0
),
}},
Next
:
httpserver
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
// drain up body
ioutil
.
ReadAll
(
r
.
Body
)
return
0
,
nil
}),
}
for
i
,
c
:=
range
[]
struct
{
body
string
expect
string
}{
{
""
,
"
\n
"
},
{
"{hello} world!"
,
"{hello} world!
\n
"
},
{
func
()
string
{
length
:=
httpserver
.
MaxLogBodySize
+
100
b
:=
make
([]
byte
,
length
)
for
i
:=
0
;
i
<
length
;
i
++
{
b
[
i
]
=
0xab
}
return
string
(
b
)
}(),
func
()
string
{
b
:=
make
([]
byte
,
httpserver
.
MaxLogBodySize
)
for
i
:=
0
;
i
<
httpserver
.
MaxLogBodySize
;
i
++
{
b
[
i
]
=
0xab
}
return
string
(
b
)
+
"
\n
"
}(),
},
}
{
got
.
Reset
()
r
,
err
:=
http
.
NewRequest
(
"POST"
,
"/"
,
bytes
.
NewBufferString
(
c
.
body
))
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
r
.
Header
.
Set
(
"Content-Type"
,
"application/json"
)
status
,
err
:=
logger
.
ServeHTTP
(
httptest
.
NewRecorder
(),
r
)
if
status
!=
0
{
t
.
Errorf
(
"case %d: Expected status to be 0, but was %d"
,
i
,
status
)
}
if
err
!=
nil
{
t
.
Errorf
(
"case %d: Expected error to be nil, instead got: %v"
,
i
,
err
)
}
if
got
.
String
()
!=
c
.
expect
{
t
.
Errorf
(
"case %d: Expected body %q, but got %q"
,
i
,
c
.
expect
,
got
.
String
())
}
}
}
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