From bf5ba9a46fa7877714c5345872636cc9023606cd Mon Sep 17 00:00:00 2001 From: Alexandre Tuleu Date: Thu, 13 Aug 2015 10:11:08 +0200 Subject: [PATCH] Finihes Unit testing --- logger.go | 7 ++- logger_test.go | 166 +++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 152 insertions(+), 21 deletions(-) diff --git a/logger.go b/logger.go index 3c7ca04..8d2877c 100644 --- a/logger.go +++ b/logger.go @@ -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 = "" + } + + 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) diff --git a/logger_test.go b/logger_test.go index 1d4b8db..453a432 100644 --- a/logger_test.go +++ b/logger_test.go @@ -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: \\) -\[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: \\) -\[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{ + "": `\`, + 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){ + `\`: 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", + ` + + + test + + +

Hello World!!

+ +`, + } + + 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) + +}