Finihes Unit testing

This commit is contained in:
2015-08-13 10:11:08 +02:00
parent 5ef16a7afc
commit bf5ba9a46f
2 changed files with 152 additions and 21 deletions

View File

@@ -50,7 +50,12 @@ func (l *Logger) Wrap() func(chain.Handler) chain.Handler {
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
hh := WrapResponseWritter(h)

View File

@@ -12,8 +12,14 @@ import (
type LogSuite struct {
*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()})
@@ -29,32 +35,152 @@ func (s *LogSuite) SetUpSuite(c *C) {
func (s *LogSuite) SetUpTest(c *C) {
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) {
rec := httptest.NewRecorder()
req, err := http.NewRequest("GET", "http://"+httptest.DefaultRemoteAddr+"/", nil)
req.RemoteAddr = "foo"
c.Assert(err, IsNil, Commentf("Got error: %s", err))
func (s *LogSuite) TestLogAreOnTwoLine(c *C) {
//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")
})
data := map[string]http.HandlerFunc{
`\[narco\] .* Requested GET on / from foo \(User-Agent: \<unknown\>\)
\[narco\] .* returned 200: OK, size: 1B in .*
`: func(rw http.ResponseWriter, req *http.Request) {
fmt.Fprintf(rw, req.URL.Path)
},
`\[narco\] .* Requested GET on / from foo \(User-Agent: \<unknown\>\)
\[narco\] .* returned 404: Not Found, size: 10B in .*
`: func(rw http.ResponseWriter, req *http.Request) {
http.Error(rw, "foo error", http.StatusNotFound)
},
c.Assert(s.buffer.String(), Matches, `\[narco\] .* Requested .*
\[narco\] .* returned .*
`)
}
func (s *LogSuite) TestReportTheMethod(c *C) {
data := []string{
"GET", "POST", "PUT", "HEAD", "DELETE", "TRACE", "CONNECT",
}
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.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)
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)
}