MCPcopy Index your code
hub / github.com/felixge/httpsnoop / TestWrap_integration

Function TestWrap_integration

wrap_test.go:71–181  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

69}
70
71func TestWrap_integration(t *testing.T) {
72 tests := []struct {
73 Name string
74 Handler http.Handler
75 Hooks Hooks
76 WantCode int
77 WantBody []byte
78 }{
79 {
80 Name: "WriteHeader (no hook)",
81 Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
82 w.WriteHeader(http.StatusNotFound)
83 }),
84 WantCode: http.StatusNotFound,
85 },
86 {
87 Name: "WriteHeader",
88 Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
89 w.WriteHeader(http.StatusNotFound)
90 }),
91 Hooks: Hooks{
92 WriteHeader: func(next WriteHeaderFunc) WriteHeaderFunc {
93 return func(code int) {
94 if code != http.StatusNotFound {
95 t.Errorf("got=%d want=%d", code, http.StatusNotFound)
96 }
97 next(http.StatusForbidden)
98 }
99 },
100 },
101 WantCode: http.StatusForbidden,
102 },
103
104 {
105 Name: "Write (no hook)",
106 Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
107 w.Write([]byte("foo"))
108 }),
109 WantCode: http.StatusOK,
110 WantBody: []byte("foo"),
111 },
112 {
113 Name: "Write (rewrite hook)",
114 Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
115 if n, err := w.Write([]byte("foo")); err != nil {
116 t.Errorf("got=%s", err)
117 } else if got, want := n, len("foobar"); got != want {
118 t.Errorf("got=%d want=%d", got, want)
119 }
120 }),
121 Hooks: Hooks{
122 Write: func(next WriteFunc) WriteFunc {
123 return func(p []byte) (int, error) {
124 if string(p) != "foo" {
125 t.Errorf("%s", p)
126 }
127 return next([]byte("foobar"))
128 }

Callers

nothing calls this directly

Calls 3

WrapFunction · 0.85
WriteHeaderMethod · 0.45
WriteMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…