|
| 1 | +package captured |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "net" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +// fake |
| 13 | +type hijacker struct { |
| 14 | + http.ResponseWriter |
| 15 | +} |
| 16 | + |
| 17 | +func (h hijacker) Hijack() (net.Conn, *bufio.ReadWriter, error) { |
| 18 | + panic("not to call") |
| 19 | +} |
| 20 | + |
| 21 | +var _ http.Hijacker = (*hijacker)(nil) |
| 22 | + |
| 23 | +func ExampleResponseWriter_additionalMethods() { |
| 24 | + var rw http.ResponseWriter = New(hijacker{}) |
| 25 | + if _, ok := rw.(http.Hijacker); !ok { |
| 26 | + fmt.Println("the non-ResponseWriter methods are not available directly") |
| 27 | + } |
| 28 | + if _, ok := rw.(interface{ Unwrap() http.ResponseWriter }).Unwrap().(http.Hijacker); ok { |
| 29 | + fmt.Println("become so, after unwrapping") |
| 30 | + } |
| 31 | + // Output: |
| 32 | + // the non-ResponseWriter methods are not available directly |
| 33 | + // become so, after unwrapping |
| 34 | +} |
| 35 | + |
| 36 | +func TestBytes(t *testing.T) { |
| 37 | + tcs := map[uint]string{ |
| 38 | + 0: "0B", |
| 39 | + 10: "10B", |
| 40 | + 20: "20B", |
| 41 | + 1000: "1000B", |
| 42 | + 1024: "1KB", |
| 43 | + 1024 + 102: "1.1KB", |
| 44 | + 1000 * 1024: "1000KB", |
| 45 | + 1024 * 1024: "1MB", |
| 46 | + 1.5 * 1024 * 1024: "1.5MB", |
| 47 | + 1<<63 - 1: "8EB", |
| 48 | + } |
| 49 | + for input, expected := range tcs { |
| 50 | + t.Run(fmt.Sprintf("%d", input), func(t *testing.T) { |
| 51 | + got := bytes(input) |
| 52 | + if got != expected { |
| 53 | + t.Errorf("expected %q got %q", expected, got) |
| 54 | + } |
| 55 | + }) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func TestResponseWriter_SizeRepresentation(t *testing.T) { |
| 60 | + crw := New(httptest.NewRecorder()) |
| 61 | + crw.Write([]byte("lorem ipsum dolor sit amet consectetur adipscing elit")) |
| 62 | + expected, got := "53B", crw.SizeRepresentation() |
| 63 | + if expected != got { |
| 64 | + t.Errorf("expected %q got %q", expected, got) |
| 65 | + } |
| 66 | +} |
0 commit comments