1
// Copyright 2012 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
13
func TestRecorder(t *testing.T) {
14
type checkFunc func(*ResponseRecorder) error
15
check := func(fns ...checkFunc) []checkFunc { return fns }
17
hasStatus := func(wantCode int) checkFunc {
18
return func(rec *ResponseRecorder) error {
19
if rec.Code != wantCode {
20
return fmt.Errorf("Status = %d; want %d", rec.Code, wantCode)
25
hasContents := func(want string) checkFunc {
26
return func(rec *ResponseRecorder) error {
27
if rec.Body.String() != want {
28
return fmt.Errorf("wrote = %q; want %q", rec.Body.String(), want)
33
hasFlush := func(want bool) checkFunc {
34
return func(rec *ResponseRecorder) error {
35
if rec.Flushed != want {
36
return fmt.Errorf("Flushed = %v; want %v", rec.Flushed, want)
44
h func(w http.ResponseWriter, r *http.Request)
49
func(w http.ResponseWriter, r *http.Request) {},
50
check(hasStatus(200), hasContents("")),
54
func(w http.ResponseWriter, r *http.Request) {
59
check(hasStatus(201), hasContents("hi")),
63
func(w http.ResponseWriter, r *http.Request) {
64
w.Write([]byte("hi first"))
68
check(hasStatus(200), hasContents("hi first"), hasFlush(false)),
72
func(w http.ResponseWriter, r *http.Request) {
73
w.(http.Flusher).Flush() // also sends a 200
76
check(hasStatus(200), hasFlush(true)),
79
r, _ := http.NewRequest("GET", "http://foo.com/", nil)
80
for _, tt := range tests {
81
h := http.HandlerFunc(tt.h)
84
for _, check := range tt.checks {
85
if err := check(rec); err != nil {
86
t.Errorf("%s: %v", tt.name, err)