-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
169 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright (c) 2021, Janoš Guljaš <[email protected]> | ||
// All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package web | ||
|
||
import "net/http" | ||
|
||
// ResponseStatusRecorder implements http.ResponseWriter that keeps tack of HTTP | ||
// response status code and written body size in bytes. | ||
type ResponseStatusRecorder struct { | ||
http.ResponseWriter | ||
status int | ||
size int | ||
} | ||
|
||
// NewResponseStatusRecorder wraps an http.ResponseWriter with | ||
// ResponseStatusRecorder in order to record the status code and written body | ||
// size. | ||
func NewResponseStatusRecorder(w http.ResponseWriter) *ResponseStatusRecorder { | ||
return &ResponseStatusRecorder{ | ||
ResponseWriter: w, | ||
} | ||
} | ||
|
||
// Write implements http.ResponseWriter. | ||
func (r *ResponseStatusRecorder) Write(b []byte) (int, error) { | ||
size, err := r.ResponseWriter.Write(b) | ||
if err != nil { | ||
return 0, err | ||
} | ||
if r.status == 0 { | ||
// The status will be StatusOK if WriteHeader has not been called yet | ||
r.status = http.StatusOK | ||
} | ||
r.size += size | ||
return size, err | ||
} | ||
|
||
// WriteHeader implements http.ResponseWriter. | ||
func (r *ResponseStatusRecorder) WriteHeader(s int) { | ||
r.ResponseWriter.WriteHeader(s) | ||
if r.status == 0 { | ||
r.status = s | ||
} | ||
} | ||
|
||
// Status returns the responded status code. If it is 0, no response data has | ||
// been written. | ||
func (r *ResponseStatusRecorder) Status() int { | ||
return r.status | ||
} | ||
|
||
// ResponseBodySize returns the number of bytes that are written as the response body. | ||
func (r *ResponseStatusRecorder) ResponseBodySize() int { | ||
return r.size | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright (c) 2021, Janoš Guljaš <[email protected]> | ||
// All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package web_test | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"resenje.org/web" | ||
) | ||
|
||
func TestResponseStatusRecorder_noWrite(t *testing.T) { | ||
w := httptest.NewRecorder() | ||
|
||
rec := web.NewResponseStatusRecorder(w) | ||
|
||
if size := rec.ResponseBodySize(); size != 0 { | ||
t.Errorf("got %v bytes that are written as body, want 0", size) | ||
} | ||
if status := rec.Status(); status != 0 { | ||
t.Errorf("git status %v, want %v", status, 0) | ||
} | ||
} | ||
|
||
func TestResponseStatusRecorder_write(t *testing.T) { | ||
w := httptest.NewRecorder() | ||
|
||
rec := web.NewResponseStatusRecorder(w) | ||
|
||
n, err := rec.Write([]byte("hi")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if n != 2 { | ||
t.Errorf("got %v bytes that are written, want 2", n) | ||
} | ||
if size := rec.ResponseBodySize(); size != 2 { | ||
t.Errorf("got %v bytes that are written as body, want 2", size) | ||
} | ||
if status := rec.Status(); status != http.StatusOK { | ||
t.Errorf("git status %v, want %v", status, http.StatusOK) | ||
} | ||
|
||
n, err = rec.Write([]byte("hello")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if n != 5 { | ||
t.Errorf("got %v bytes that are written, want 5", n) | ||
} | ||
if size := rec.ResponseBodySize(); size != 7 { | ||
t.Errorf("got %v bytes that are written as body, want 7", size) | ||
} | ||
} | ||
|
||
func TestResponseStatusRecorder_writeHeader(t *testing.T) { | ||
w := httptest.NewRecorder() | ||
|
||
rec := web.NewResponseStatusRecorder(w) | ||
|
||
rec.WriteHeader(http.StatusTeapot) | ||
|
||
if size := rec.ResponseBodySize(); size != 0 { | ||
t.Errorf("got %v bytes that are written as body, want 0", size) | ||
} | ||
if status := rec.Status(); status != http.StatusTeapot { | ||
t.Errorf("git status %v, want %v", status, http.StatusTeapot) | ||
} | ||
} | ||
|
||
func TestResponseStatusRecorder_writeHeaderAfterWrite(t *testing.T) { | ||
w := httptest.NewRecorder() | ||
|
||
rec := web.NewResponseStatusRecorder(w) | ||
|
||
n, err := rec.Write([]byte("hi")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if n != 2 { | ||
t.Errorf("got %v bytes that are written, want 2", n) | ||
} | ||
if size := rec.ResponseBodySize(); size != 2 { | ||
t.Errorf("got %v bytes that are written as body, want 2", size) | ||
} | ||
if status := rec.Status(); status != http.StatusOK { | ||
t.Errorf("git status %v, want %v", status, http.StatusOK) | ||
} | ||
|
||
rec.WriteHeader(http.StatusTeapot) | ||
|
||
if status := rec.Status(); status != http.StatusOK { | ||
t.Errorf("git status %v, want %v", status, http.StatusOK) | ||
} | ||
} |