httpwrap - examples_test.go
1 package httpwrap_test
2
3 import (
4 "fmt"
5 "io"
6 "net/http"
7 "net/http/httptest"
8
9 "vimagination.zapto.org/httpwrap"
10 )
11
12 type writeLogger struct {
13 http.ResponseWriter
14 }
15
16 func (w writeLogger) Write(p []byte) (int, error) {
17 fmt.Println("Writing Bytes:", len(p))
18
19 return w.ResponseWriter.Write(p)
20 }
21
22 func (w writeLogger) WriteString(p string) (int, error) {
23 fmt.Println("Writing String:", len(p))
24
25 return io.WriteString(w.ResponseWriter, p)
26 }
27
28 func Example() {
29 w := httptest.NewRecorder()
30 l := writeLogger{w}
31
32 ww := httpwrap.Wrap(w, httpwrap.OverrideWriter(l), httpwrap.OverrideStringWriter(l))
33
34 ww.Write([]byte("Some Data\n"))
35 io.WriteString(ww, "Some More Data\n")
36 fmt.Println(w.Body)
37
38 // Output:
39 // Writing Bytes: 10
40 // Writing String: 15
41 // Some Data
42 // Some More Data
43 }
44