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
fdad616d
Commit
fdad616d
authored
Aug 22, 2016
by
Matt Holt
Committed by
GitHub
Aug 22, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1049 from tw4452852/log_body
capture request body normally
parents
1d3212a5
590862a9
Changes
3
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
239 additions
and
197 deletions
+239
-197
caddyhttp/httpserver/replacer.go
caddyhttp/httpserver/replacer.go
+172
-146
caddyhttp/httpserver/replacer_test.go
caddyhttp/httpserver/replacer_test.go
+10
-51
caddyhttp/log/log_test.go
caddyhttp/log/log_test.go
+57
-0
No files found.
caddyhttp/httpserver/replacer.go
View file @
fdad616d
This diff is collapsed.
Click to expand it.
caddyhttp/httpserver/replacer_test.go
View file @
fdad616d
package
httpserver
package
httpserver
import
(
import
(
"bytes"
"io/ioutil"
"net/http"
"net/http"
"net/http/httptest"
"net/http/httptest"
"os"
"os"
...
@@ -24,28 +22,12 @@ func TestNewReplacer(t *testing.T) {
...
@@ -24,28 +22,12 @@ func TestNewReplacer(t *testing.T) {
switch
v
:=
rep
.
(
type
)
{
switch
v
:=
rep
.
(
type
)
{
case
*
replacer
:
case
*
replacer
:
if
v
.
replacements
[
"{host}"
](
)
!=
"localhost"
{
if
v
.
getSubstitution
(
"{host}"
)
!=
"localhost"
{
t
.
Error
(
"Expected host to be localhost"
)
t
.
Error
(
"Expected host to be localhost"
)
}
}
if
v
.
replacements
[
"{method}"
](
)
!=
"POST"
{
if
v
.
getSubstitution
(
"{method}"
)
!=
"POST"
{
t
.
Error
(
"Expected request method to be POST"
)
t
.
Error
(
"Expected request method to be POST"
)
}
}
// Response placeholders should only be set after call to Replace()
got
,
want
:=
""
,
""
if
getReplacement
,
ok
:=
v
.
replacements
[
"{status}"
];
ok
{
got
=
getReplacement
()
}
if
want
:=
""
;
got
!=
want
{
t
.
Errorf
(
"Expected status to NOT be set before Replace() is called; was: %s"
,
got
)
}
rep
.
Replace
(
"{foobar}"
)
if
getReplacement
,
ok
:=
v
.
replacements
[
"{status}"
];
ok
{
got
=
getReplacement
()
}
if
want
=
"200"
;
got
!=
want
{
t
.
Errorf
(
"Expected status to be %s, was: %s"
,
want
,
got
)
}
default
:
default
:
t
.
Fatalf
(
"Expected *replacer underlying Replacer type, got: %#v"
,
rep
)
t
.
Fatalf
(
"Expected *replacer underlying Replacer type, got: %#v"
,
rep
)
}
}
...
@@ -94,19 +76,21 @@ func TestReplace(t *testing.T) {
...
@@ -94,19 +76,21 @@ func TestReplace(t *testing.T) {
complexCases
:=
[]
struct
{
complexCases
:=
[]
struct
{
template
string
template
string
replacements
map
[
string
]
func
()
string
replacements
map
[
string
]
string
expect
string
expect
string
}{
}{
{
"/a{1}/{2}"
,
{
map
[
string
]
func
()
string
{
"/a{1}/{2}"
,
"{1}"
:
func
()
string
{
return
"12"
},
map
[
string
]
string
{
"{2}"
:
func
()
string
{
return
""
}},
"{1}"
:
"12"
,
"{2}"
:
""
,
},
"/a12/"
},
"/a12/"
},
}
}
for
_
,
c
:=
range
complexCases
{
for
_
,
c
:=
range
complexCases
{
repl
:=
&
replacer
{
repl
:=
&
replacer
{
r
eplacements
:
c
.
replacements
,
customR
eplacements
:
c
.
replacements
,
}
}
if
expected
,
actual
:=
c
.
expect
,
repl
.
Replace
(
c
.
template
);
expected
!=
actual
{
if
expected
,
actual
:=
c
.
expect
,
repl
.
Replace
(
c
.
template
);
expected
!=
actual
{
t
.
Errorf
(
"for template '%s', expected '%s', got '%s'"
,
c
.
template
,
expected
,
actual
)
t
.
Errorf
(
"for template '%s', expected '%s', got '%s'"
,
c
.
template
,
expected
,
actual
)
...
@@ -163,28 +147,3 @@ func TestRound(t *testing.T) {
...
@@ -163,28 +147,3 @@ func TestRound(t *testing.T) {
}
}
}
}
}
}
func
TestReadRequestBody
(
t
*
testing
.
T
)
{
payload
:=
[]
byte
(
`{ "foo": "bar" }`
)
var
readSize
int64
=
5
r
,
err
:=
http
.
NewRequest
(
"POST"
,
"/"
,
bytes
.
NewReader
(
payload
))
if
err
!=
nil
{
t
.
Error
(
err
)
}
defer
r
.
Body
.
Close
()
logBody
,
err
:=
readRequestBody
(
r
,
readSize
)
if
err
!=
nil
{
t
.
Error
(
"readRequestBody failed"
,
err
)
}
else
if
!
bytes
.
EqualFold
(
payload
[
0
:
readSize
],
logBody
)
{
t
.
Error
(
"Expected log comparison failed"
)
}
// Ensure the Request body is the same as the original.
reqBody
,
err
:=
ioutil
.
ReadAll
(
r
.
Body
)
if
err
!=
nil
{
t
.
Error
(
"Unable to read request body"
,
err
)
}
else
if
!
bytes
.
EqualFold
(
payload
,
reqBody
)
{
t
.
Error
(
"Expected request body comparison failed"
)
}
}
caddyhttp/log/log_test.go
View file @
fdad616d
...
@@ -2,6 +2,7 @@ package log
...
@@ -2,6 +2,7 @@ package log
import
(
import
(
"bytes"
"bytes"
"io/ioutil"
"log"
"log"
"net/http"
"net/http"
"net/http/httptest"
"net/http/httptest"
...
@@ -60,3 +61,59 @@ func TestLoggedStatus(t *testing.T) {
...
@@ -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
)
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