github.com/mdempsky/go@v0.0.0-20151201204031-5dd372bd1e70/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  	hasHeader := func(key, want string) checkFunc {
    43  		return func(rec *ResponseRecorder) error {
    44  			if got := rec.HeaderMap.Get(key); got != want {
    45  				return fmt.Errorf("header %s = %q; want %q", key, got, want)
    46  			}
    47  			return nil
    48  		}
    49  	}
    50  
    51  	tests := []struct {
    52  		name   string
    53  		h      func(w http.ResponseWriter, r *http.Request)
    54  		checks []checkFunc
    55  	}{
    56  		{
    57  			"200 default",
    58  			func(w http.ResponseWriter, r *http.Request) {},
    59  			check(hasStatus(200), hasContents("")),
    60  		},
    61  		{
    62  			"first code only",
    63  			func(w http.ResponseWriter, r *http.Request) {
    64  				w.WriteHeader(201)
    65  				w.WriteHeader(202)
    66  				w.Write([]byte("hi"))
    67  			},
    68  			check(hasStatus(201), hasContents("hi")),
    69  		},
    70  		{
    71  			"write sends 200",
    72  			func(w http.ResponseWriter, r *http.Request) {
    73  				w.Write([]byte("hi first"))
    74  				w.WriteHeader(201)
    75  				w.WriteHeader(202)
    76  			},
    77  			check(hasStatus(200), hasContents("hi first"), hasFlush(false)),
    78  		},
    79  		{
    80  			"write string",
    81  			func(w http.ResponseWriter, r *http.Request) {
    82  				io.WriteString(w, "hi first")
    83  			},
    84  			check(
    85  				hasStatus(200),
    86  				hasContents("hi first"),
    87  				hasFlush(false),
    88  				hasHeader("Content-Type", "text/plain; charset=utf-8"),
    89  			),
    90  		},
    91  		{
    92  			"flush",
    93  			func(w http.ResponseWriter, r *http.Request) {
    94  				w.(http.Flusher).Flush() // also sends a 200
    95  				w.WriteHeader(201)
    96  			},
    97  			check(hasStatus(200), hasFlush(true)),
    98  		},
    99  		{
   100  			"Content-Type detection",
   101  			func(w http.ResponseWriter, r *http.Request) {
   102  				io.WriteString(w, "<html>")
   103  			},
   104  			check(hasHeader("Content-Type", "text/html; charset=utf-8")),
   105  		},
   106  		{
   107  			"no Content-Type detection with Transfer-Encoding",
   108  			func(w http.ResponseWriter, r *http.Request) {
   109  				w.Header().Set("Transfer-Encoding", "some encoding")
   110  				io.WriteString(w, "<html>")
   111  			},
   112  			check(hasHeader("Content-Type", "")), // no header
   113  		},
   114  		{
   115  			"no Content-Type detection if set explicitly",
   116  			func(w http.ResponseWriter, r *http.Request) {
   117  				w.Header().Set("Content-Type", "some/type")
   118  				io.WriteString(w, "<html>")
   119  			},
   120  			check(hasHeader("Content-Type", "some/type")),
   121  		},
   122  	}
   123  	r, _ := http.NewRequest("GET", "http://foo.com/", nil)
   124  	for _, tt := range tests {
   125  		h := http.HandlerFunc(tt.h)
   126  		rec := NewRecorder()
   127  		h.ServeHTTP(rec, r)
   128  		for _, check := range tt.checks {
   129  			if err := check(rec); err != nil {
   130  				t.Errorf("%s: %v", tt.name, err)
   131  			}
   132  		}
   133  	}
   134  }