Finihes Unit testing
This commit is contained in:
@@ -50,7 +50,12 @@ func (l *Logger) Wrap() func(chain.Handler) chain.Handler {
|
|||||||
uAgent = strings.Join(uAgentList, "; ")
|
uAgent = strings.Join(uAgentList, "; ")
|
||||||
}
|
}
|
||||||
|
|
||||||
l.Printf("Requested %s on %s from %s (User-Agent: %s)", r.Method, r.URL.Path, r.RemoteAddr, uAgent)
|
remote := r.RemoteAddr
|
||||||
|
if len(remote) == 0 {
|
||||||
|
remote = "<unknown>"
|
||||||
|
}
|
||||||
|
|
||||||
|
l.Printf("Requested %s on %s from %s (User-Agent: %s)", r.Method, r.URL.Path, remote, uAgent)
|
||||||
|
|
||||||
//wrap the request with our own interface
|
//wrap the request with our own interface
|
||||||
hh := WrapResponseWritter(h)
|
hh := WrapResponseWritter(h)
|
||||||
|
|||||||
166
logger_test.go
166
logger_test.go
@@ -12,8 +12,14 @@ import (
|
|||||||
|
|
||||||
type LogSuite struct {
|
type LogSuite struct {
|
||||||
*NarcoSuite
|
*NarcoSuite
|
||||||
l *Logger
|
|
||||||
buffer bytes.Buffer
|
l *Logger
|
||||||
|
buffer bytes.Buffer
|
||||||
|
rec *httptest.ResponseRecorder
|
||||||
|
req *http.Request
|
||||||
|
URL string
|
||||||
|
RemoteAddress string
|
||||||
|
UserAgent string
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ = Suite(&LogSuite{NarcoSuite: NewNarcoSuite()})
|
var _ = Suite(&LogSuite{NarcoSuite: NewNarcoSuite()})
|
||||||
@@ -29,32 +35,152 @@ func (s *LogSuite) SetUpSuite(c *C) {
|
|||||||
|
|
||||||
func (s *LogSuite) SetUpTest(c *C) {
|
func (s *LogSuite) SetUpTest(c *C) {
|
||||||
s.buffer.Reset()
|
s.buffer.Reset()
|
||||||
|
s.rec = httptest.NewRecorder()
|
||||||
|
var err error
|
||||||
|
s.URL = "http://" + httptest.DefaultRemoteAddr + "/"
|
||||||
|
s.req, err = http.NewRequest("GET", s.URL, nil)
|
||||||
|
c.Assert(err, IsNil, Commentf("Unexpected error :%s", err))
|
||||||
|
s.RemoteAddress = httptest.DefaultRemoteAddr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *LogSuite) TestLogFormat(c *C) {
|
func (s *LogSuite) TestLogAreOnTwoLine(c *C) {
|
||||||
rec := httptest.NewRecorder()
|
//when logging first line is the request, second line is the result
|
||||||
req, err := http.NewRequest("GET", "http://"+httptest.DefaultRemoteAddr+"/", nil)
|
s.ServeHTTP(s.rec, s.req, func(rw http.ResponseWriter, req *http.Request) {
|
||||||
req.RemoteAddr = "foo"
|
fmt.Fprintf(rw, "foo")
|
||||||
c.Assert(err, IsNil, Commentf("Got error: %s", err))
|
})
|
||||||
|
|
||||||
data := map[string]http.HandlerFunc{
|
c.Assert(s.buffer.String(), Matches, `\[narco\] .* Requested .*
|
||||||
`\[narco\] .* Requested GET on / from foo \(User-Agent: \<unknown\>\)
|
\[narco\] .* returned .*
|
||||||
\[narco\] .* returned 200: OK, size: 1B in .*
|
`)
|
||||||
`: func(rw http.ResponseWriter, req *http.Request) {
|
}
|
||||||
fmt.Fprintf(rw, req.URL.Path)
|
|
||||||
},
|
func (s *LogSuite) TestReportTheMethod(c *C) {
|
||||||
`\[narco\] .* Requested GET on / from foo \(User-Agent: \<unknown\>\)
|
data := []string{
|
||||||
\[narco\] .* returned 404: Not Found, size: 10B in .*
|
"GET", "POST", "PUT", "HEAD", "DELETE", "TRACE", "CONNECT",
|
||||||
`: func(rw http.ResponseWriter, req *http.Request) {
|
|
||||||
http.Error(rw, "foo error", http.StatusNotFound)
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for matchString, fn := range data {
|
for _, m := range data {
|
||||||
|
s.req.Method = m
|
||||||
|
//when logging first line is the request, second line is the result
|
||||||
|
s.ServeHTTP(s.rec, s.req, func(rw http.ResponseWriter, req *http.Request) {
|
||||||
|
fmt.Fprintf(rw, "foo")
|
||||||
|
})
|
||||||
|
|
||||||
|
c.Assert(s.buffer.String(), Matches, `\A\[narco\] .* Requested `+m+".*\n.*\n")
|
||||||
s.buffer.Reset()
|
s.buffer.Reset()
|
||||||
s.ServeHTTP(rec, req, fn)
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *LogSuite) TestReportTheURI(c *C) {
|
||||||
|
data := map[string]string{
|
||||||
|
"/": "/",
|
||||||
|
"/foo": "/foo",
|
||||||
|
"/bar/baz": "/bar/baz",
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
for reqURI, resURI := range data {
|
||||||
|
|
||||||
|
s.req, err = http.NewRequest("GET", "http://"+httptest.DefaultRemoteAddr+reqURI, nil)
|
||||||
|
c.Assert(err, IsNil, Commentf("Unexpected error: %s", err))
|
||||||
|
//when logging first line is the request, second line is the result
|
||||||
|
s.ServeHTTP(s.rec, s.req, func(rw http.ResponseWriter, req *http.Request) {
|
||||||
|
fmt.Fprintf(rw, "foo")
|
||||||
|
})
|
||||||
|
|
||||||
|
c.Assert(s.buffer.String(), Matches, `\A\[narco\] .* Requested [A-Z]+ on `+resURI+".*\n.*\n")
|
||||||
|
s.buffer.Reset()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *LogSuite) TestLogReportsRemoteAddress(c *C) {
|
||||||
|
data := map[string]string{
|
||||||
|
"": `\<unknown\>`,
|
||||||
|
httptest.DefaultRemoteAddr: httptest.DefaultRemoteAddr,
|
||||||
|
}
|
||||||
|
for reqAddress, reported := range data {
|
||||||
|
s.req.RemoteAddr = reqAddress
|
||||||
|
s.ServeHTTP(s.rec, s.req, func(rw http.ResponseWriter, req *http.Request) {
|
||||||
|
fmt.Fprintf(rw, "foo")
|
||||||
|
})
|
||||||
|
c.Assert(s.buffer.String(), Matches, `\A\[narco\] .* Requested [A-Z]+ on .* from `+reported+` .*`+"\n.*\n")
|
||||||
|
s.buffer.Reset()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *LogSuite) TestLogReportTheUserAgent(c *C) {
|
||||||
|
|
||||||
|
data := map[string]func(*http.Request){
|
||||||
|
`\<unknown\>`: func(*http.Request) {},
|
||||||
|
"golangtest/1.0.0": func(req *http.Request) { req.Header["User-Agent"] = []string{"golangtest/1.0.0"} },
|
||||||
|
}
|
||||||
|
|
||||||
|
for uAgent, reqModifier := range data {
|
||||||
|
req, err := http.NewRequest("GET", s.URL, nil)
|
||||||
|
c.Assert(err, IsNil, Commentf("Unexpected error: %s", err))
|
||||||
|
reqModifier(req)
|
||||||
|
s.ServeHTTP(s.rec, req, func(rw http.ResponseWriter, req *http.Request) {
|
||||||
|
fmt.Fprintf(rw, "foo")
|
||||||
|
})
|
||||||
|
c.Assert(s.buffer.String(), Matches, `\A\[narco\] .* Requested [A-Z]+ on .* from .* \(User-Agent: `+uAgent+`\)`+"\n.*\n")
|
||||||
|
s.buffer.Reset()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *LogSuite) TestLogReportsTheStatus(c *C) {
|
||||||
|
data := []int{http.StatusOK, http.StatusUnauthorized, http.StatusInternalServerError}
|
||||||
|
|
||||||
|
for _, status := range data {
|
||||||
|
|
||||||
|
s.ServeHTTP(s.rec, s.req, func(rw http.ResponseWriter, req *http.Request) {
|
||||||
|
rw.WriteHeader(status)
|
||||||
|
fmt.Fprintf(rw, "foo")
|
||||||
|
})
|
||||||
|
|
||||||
|
matchString := fmt.Sprintf(`\A\[narco\] .*`+"\n"+`\[narco\] .* returned %d: .*, .*`+"\n", status)
|
||||||
|
|
||||||
c.Assert(s.buffer.String(), Matches, matchString)
|
c.Assert(s.buffer.String(), Matches, matchString)
|
||||||
|
s.buffer.Reset()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *LogSuite) TestLogReportsTheResponseSize(c *C) {
|
||||||
|
|
||||||
|
data := []string{
|
||||||
|
"foo",
|
||||||
|
"longer payload",
|
||||||
|
`<!doctype html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>test</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Hello World!!</h1>
|
||||||
|
</body>
|
||||||
|
</html>`,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, payload := range data {
|
||||||
|
s.ServeHTTP(s.rec, s.req, func(rw http.ResponseWriter, req *http.Request) {
|
||||||
|
fmt.Fprintf(rw, "%s", payload)
|
||||||
|
})
|
||||||
|
|
||||||
|
matchString := fmt.Sprintf(`\A\[narco\] .*`+"\n"+`\[narco\] .* returned [0-9]+: .*, size: %dB .*`+"\n",
|
||||||
|
len(payload))
|
||||||
|
|
||||||
|
c.Assert(s.buffer.String(), Matches, matchString)
|
||||||
|
s.buffer.Reset()
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *LogSuite) TestLogReportsTheResponseTime(c *C) {
|
||||||
|
|
||||||
|
s.ServeHTTP(s.rec, s.req, func(rw http.ResponseWriter, req *http.Request) {
|
||||||
|
fmt.Fprintf(rw, "foo")
|
||||||
|
})
|
||||||
|
|
||||||
|
matchString := `\A\[narco\] .*` + "\n" + `\[narco\] .* returned [0-9]+: .*, size: 3B in [0-9]+\.[0-9]+.*s` + "\n"
|
||||||
|
|
||||||
|
c.Assert(s.buffer.String(), Matches, matchString)
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user