github.com/d4l3k/go@v0.0.0-20151015000803-65fc379daeda/src/net/http/httptest/recorder_test.go (about)

     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.
     4  
     5  package httptest
     6  
     7  import (
     8  	"fmt"
     9  	"io"
    10  	"net/http"
    11  	"testing"
    12  )
    13  
    14  func TestRecorder(t *testing.T) {
    15  	type checkFunc func(*ResponseRecorder) error
    16  	check := func(fns ...checkFunc) []checkFunc { return fns }
    17  
    18  	hasStatus := func(wantCode int) checkFunc {
    19  		return func(rec *ResponseRecorder) error {
    20  			if rec.Code != wantCode {
    21  				return fmt.Errorf("Status = %d; want %d", rec.Code, wantCode)
    22  			}
    23  			return nil
    24  		}
    25  	}
    26  	hasContents := func(want string) checkFunc {
    27  		return func(rec *ResponseRecorder) error {
    28  			if rec.Body.String() != want {
    29  				return fmt.Errorf("wrote = %q; want %q", rec.Body.String(), want)
    30  			}
    31  			return nil
    32  		}
    33  	}
    34  	hasFlush := func(want bool) checkFunc {
    35  		return func(rec *ResponseRecorder) error {
    36  			if rec.Flushed != want {
    37  				return fmt.Errorf("Flushed = %v; want %v", rec.Flushed, want)
    38  			}
    39  			return nil
    40  		}
    41  	}
    42  
    43  	tests := []struct {
    44  		name   string
    45  		h      func(w http.ResponseWriter, r *http.Request)
    46  		checks []checkFunc
    47  	}{
    48  		{
    49  			"200 default",
    50  			func(w http.ResponseWriter, r *http.Request) {},
    51  			check(hasStatus(200), hasContents("")),
    52  		},
    53  		{
    54  			"first code only",
    55  			func(w http.ResponseWriter, r *http.Request) {
    56  				w.WriteHeader(201)
    57  				w.WriteHeader(202)
    58  				w.Write([]byte("hi"))
    59  			},
    60  			check(hasStatus(201), hasContents("hi")),
    61  		},
    62  		{
    63  			"write sends 200",
    64  			func(w http.ResponseWriter, r *http.Request) {
    65  				w.Write([]byte("hi first"))
    66  				w.WriteHeader(201)
    67  				w.WriteHeader(202)
    68  			},
    69  			check(hasStatus(200), hasContents("hi first"), hasFlush(false)),
    70  		},
    71  		{
    72  			"write string",
    73  			func(w http.ResponseWriter, r *http.Request) {
    74  				io.WriteString(w, "hi first")
    75  			},
    76  			check(hasStatus(200), hasContents("hi first"), hasFlush(false)),
    77  		},
    78  		{
    79  			"flush",
    80  			func(w http.ResponseWriter, r *http.Request) {
    81  				w.(http.Flusher).Flush() // also sends a 200
    82  				w.WriteHeader(201)
    83  			},
    84  			check(hasStatus(200), hasFlush(true)),
    85  		},
    86  	}
    87  	r, _ := http.NewRequest("GET", "http://foo.com/", nil)
    88  	for _, tt := range tests {
    89  		h := http.HandlerFunc(tt.h)
    90  		rec := NewRecorder()
    91  		h.ServeHTTP(rec, r)
    92  		for _, check := range tt.checks {
    93  			if err := check(rec); err != nil {
    94  				t.Errorf("%s: %v", tt.name, err)
    95  			}
    96  		}
    97  	}
    98  }