From bec130a5638c72298b2d0104a47bdff64920736f Mon Sep 17 00:00:00 2001 From: Benoit Benedetti Date: Sat, 20 Feb 2016 22:52:42 +0100 Subject: [PATCH] Recorder: Exporting ResponseRecorder #614 --- middleware/recorder.go | 22 ++++++++++++++++------ middleware/replacer.go | 2 +- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/middleware/recorder.go b/middleware/recorder.go index 0477cce88..481b60dff 100644 --- a/middleware/recorder.go +++ b/middleware/recorder.go @@ -14,7 +14,7 @@ import ( // to be written, however, in which case 200 must be assumed. // It is best to have the constructor initialize this type // with that default status code. -type responseRecorder struct { +type ResponseRecorder struct { http.ResponseWriter status int size int @@ -27,8 +27,8 @@ type responseRecorder struct { // Because a status is not set unless WriteHeader is called // explicitly, this constructor initializes with a status code // of 200 to cover the default case. -func NewResponseRecorder(w http.ResponseWriter) *responseRecorder { - return &responseRecorder{ +func NewResponseRecorder(w http.ResponseWriter) *ResponseRecorder { + return &ResponseRecorder{ ResponseWriter: w, status: http.StatusOK, start: time.Now(), @@ -37,14 +37,14 @@ func NewResponseRecorder(w http.ResponseWriter) *responseRecorder { // WriteHeader records the status code and calls the // underlying ResponseWriter's WriteHeader method. -func (r *responseRecorder) WriteHeader(status int) { +func (r *ResponseRecorder) WriteHeader(status int) { r.status = status r.ResponseWriter.WriteHeader(status) } // Write is a wrapper that records the size of the body // that gets written. -func (r *responseRecorder) Write(buf []byte) (int, error) { +func (r *ResponseRecorder) Write(buf []byte) (int, error) { n, err := r.ResponseWriter.Write(buf) if err == nil { r.size += n @@ -52,9 +52,19 @@ func (r *responseRecorder) Write(buf []byte) (int, error) { return n, err } +// Size is a Getter to size property +func (r *ResponseRecorder) Size() int { + return r.size +} + +// Status is a Getter to status property +func (r *ResponseRecorder) Status() int { + return r.status +} + // Hijacker is a wrapper of http.Hijacker underearth if any, // otherwise it just returns an error. -func (r *responseRecorder) Hijack() (net.Conn, *bufio.ReadWriter, error) { +func (r *ResponseRecorder) Hijack() (net.Conn, *bufio.ReadWriter, error) { if hj, ok := r.ResponseWriter.(http.Hijacker); ok { return hj.Hijack() } diff --git a/middleware/replacer.go b/middleware/replacer.go index 8a3d202a2..d53fdc660 100644 --- a/middleware/replacer.go +++ b/middleware/replacer.go @@ -30,7 +30,7 @@ type replacer struct { // values into the replacer. rr may be nil if it is not // available. emptyValue should be the string that is used // in place of empty string (can still be empty string). -func NewReplacer(r *http.Request, rr *responseRecorder, emptyValue string) Replacer { +func NewReplacer(r *http.Request, rr *ResponseRecorder, emptyValue string) Replacer { rep := replacer{ replacements: map[string]string{ "{method}": r.Method,