Add unit tests for MakeNotifierWrapper

- fixes issue where result was assigned to value rather than
to pointer reference.

Signed-off-by: Alex Ellis (VMware) <alexellis2@gmail.com>
This commit is contained in:
Alex Ellis (VMware)
2019-01-05 16:22:01 +00:00
committed by Alex Ellis
parent f7cf7a6496
commit 67c9a71686
2 changed files with 108 additions and 8 deletions

View File

@ -13,32 +13,43 @@ func MakeNotifierWrapper(next http.HandlerFunc, notifiers []HTTPNotifier) http.H
return func(w http.ResponseWriter, r *http.Request) {
then := time.Now()
writer := newCustomWriter(w)
next(w, r)
writer := newWriteInterceptor(w)
next(&writer, r)
url := r.URL.String()
for _, notifier := range notifiers {
notifier.Notify(r.Method, url, url, writer.CapturedStatusCode, time.Since(then))
notifier.Notify(r.Method, url, url, writer.Status(), time.Since(then))
}
}
}
func newCustomWriter(w http.ResponseWriter) customWriter {
return customWriter{
func newWriteInterceptor(w http.ResponseWriter) writeInterceptor {
return writeInterceptor{
w: w,
}
}
type customWriter struct {
type writeInterceptor struct {
CapturedStatusCode int
w http.ResponseWriter
}
func (c *customWriter) Write(data []byte) (int, error) {
func (c *writeInterceptor) Status() int {
if c.CapturedStatusCode == 0 {
return http.StatusOK
}
return c.CapturedStatusCode
}
func (c *writeInterceptor) Header() http.Header {
return c.w.Header()
}
func (c *writeInterceptor) Write(data []byte) (int, error) {
return c.w.Write(data)
}
func (c *customWriter) WriteHeader(code int) {
func (c *writeInterceptor) WriteHeader(code int) {
c.CapturedStatusCode = code
c.w.WriteHeader(code)
}