github.com/searKing/golang/go@v1.2.117/net/http/response_writer_test.go (about) 1 // Copyright 2023 The searKing Author. 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 http 6 7 import ( 8 "net/http" 9 "net/http/httptest" 10 "testing" 11 ) 12 13 func TestResponseWriterUnwrap(t *testing.T) { 14 testWriter := httptest.NewRecorder() 15 writer := &recordResponseWriter{ResponseWriter: testWriter} 16 if testWriter != writer.Unwrap() { 17 t.Errorf("response writer not the same") 18 } 19 } 20 21 func TestResponseWriterReset(t *testing.T) { 22 testWriter := httptest.NewRecorder() 23 w := &recordResponseWriter{} 24 25 w.reset(testWriter) 26 if w.size != 0 { 27 t.Errorf("writer.size got (%v), want (%v)", w.size, 0) 28 } 29 if w.status != 0 { 30 t.Errorf("writer.status got (%v), want (%v)", w.status, 0) 31 } 32 if w.ResponseWriter != testWriter { 33 t.Errorf("response writer not the same") 34 } 35 } 36 37 func TestResponseWriterWriteHeader(t *testing.T) { 38 testWriter := httptest.NewRecorder() 39 w := &recordResponseWriter{} 40 w.reset(testWriter) 41 42 w.WriteHeader(http.StatusMultipleChoices) 43 if !w.Written() { 44 t.Errorf("w.Written() got (%v), want (%t)", w.Written(), true) 45 } 46 if w.Status() != http.StatusMultipleChoices { 47 t.Errorf("w.Status() got (%v), want (%v)", w.Status(), http.StatusMultipleChoices) 48 } 49 if testWriter.Code != http.StatusMultipleChoices { 50 t.Errorf("testWriter.Code got (%v), want (%v)", testWriter.Code, http.StatusMultipleChoices) 51 } 52 w.WriteHeader(-1) 53 if w.Status() != -1 { 54 t.Errorf("w.Status() got (%v), want (%v)", w.Status(), -1) 55 } 56 } 57 58 func TestResponseWriterWriteHeadersNow(t *testing.T) { 59 testWriter := httptest.NewRecorder() 60 w := &recordResponseWriter{} 61 w.reset(testWriter) 62 63 w.WriteHeader(http.StatusMultipleChoices) 64 65 if !w.Written() { 66 t.Errorf("w.Written() got (%v), want (%t)", w.Written(), true) 67 } 68 if w.Size() != 0 { 69 t.Errorf("w.Size() got (%v), want (%v)", w.Size(), 0) 70 } 71 if testWriter.Code != http.StatusMultipleChoices { 72 t.Errorf("testWriter.Code got (%v), want (%v)", testWriter.Code, http.StatusMultipleChoices) 73 } 74 75 w.size = 10 76 if w.Size() != 10 { 77 t.Errorf("w.Size() got (%v), want (%v)", w.Size(), 10) 78 } 79 } 80 81 func TestResponseWriterWrite(t *testing.T) { 82 testWriter := httptest.NewRecorder() 83 w := &recordResponseWriter{} 84 w.reset(testWriter) 85 86 n, err := w.Write([]byte("hola")) 87 if n != 4 { 88 t.Errorf("n got (%v), want (%v)", n, 4) 89 } 90 if w.Size() != 4 { 91 t.Errorf("w.Size() got (%v), want (%v)", w.Size(), 4) 92 } 93 if w.Status() != http.StatusOK { 94 t.Errorf("w.Status() got (%v), want (%v)", w.Status(), http.StatusOK) 95 } 96 if testWriter.Code != http.StatusOK { 97 t.Errorf("testWriter.Code got (%v), want (%v)", testWriter.Code, http.StatusOK) 98 } 99 if testWriter.Body.String() != "hola" { 100 t.Errorf("testWriter.Body.String() got (%v), want (%v)", testWriter.Body.String(), "hola") 101 } 102 if err != nil { 103 t.Errorf("w.Write() got an error: %s", err.Error()) 104 } 105 106 n, err = w.Write([]byte(" adios")) 107 if n != 6 { 108 t.Errorf("n got (%v), want (%v)", n, 6) 109 } 110 if w.Size() != 10 { 111 t.Errorf("w.Size() got (%v), want (%v)", w.Size(), 10) 112 } 113 if testWriter.Body.String() != "hola adios" { 114 t.Errorf("testWriter.Body.String() got (%v), want (%v)", testWriter.Body.String(), "hola adios") 115 } 116 if err != nil { 117 t.Errorf("w.Write() got an error: %s", err.Error()) 118 } 119 } 120 121 func TestResponseWriterHijack(t *testing.T) { 122 testWriter := httptest.NewRecorder() 123 w := &recordResponseWriter{} 124 w.reset(testWriter) 125 _, _, err := w.Hijack() 126 if err == nil { 127 t.Error("w.Hijack() should got an error") 128 } 129 if w.Written() { 130 t.Errorf("w.Written() got (%v), want (%t)", w.Written(), false) 131 } 132 w.CloseNotify() 133 134 w.Flush() 135 } 136 137 func TestResponseWriterFlush(t *testing.T) { 138 testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 139 writer := &recordResponseWriter{} 140 writer.reset(w) 141 142 writer.WriteHeader(http.StatusInternalServerError) 143 writer.Flush() 144 })) 145 defer testServer.Close() 146 147 // should return 500 148 resp, err := http.Get(testServer.URL) 149 if err != nil { 150 t.Errorf("http.Get() got an error: %s", err.Error()) 151 } 152 if resp.StatusCode != http.StatusInternalServerError { 153 t.Errorf("resp.StatusCode got (%v), want (%v)", resp.StatusCode, http.StatusInternalServerError) 154 } 155 } 156 157 func TestResponseWriterStatusCode(t *testing.T) { 158 testWriter := httptest.NewRecorder() 159 w := &recordResponseWriter{} 160 w.reset(testWriter) 161 162 w.WriteHeader(http.StatusOK) 163 164 if w.Status() != http.StatusOK { 165 t.Errorf("w.Status() got (%v), want (%v)", w.Status(), http.StatusOK) 166 } 167 if !w.Written() { 168 t.Errorf("w.Written() got (%v), want (%t)", w.Written(), true) 169 } 170 171 w.WriteHeader(http.StatusUnauthorized) 172 173 // status must be 200 although we tried to change it 174 if w.Status() != http.StatusUnauthorized { 175 t.Errorf("w.Status() got (%v), want (%v)", w.Status(), http.StatusUnauthorized) 176 } 177 }