github.com/xmidt-org/webpa-common@v1.11.9/health/responseWriter_test.go (about) 1 package health 2 3 import ( 4 "bufio" 5 "bytes" 6 "net" 7 "net/http" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func testResponseWriterWrapping(t *testing.T) { 14 var ( 15 assert = assert.New(t) 16 delegate = new(mockResponseWriter) 17 composite = Wrap(delegate) 18 ) 19 20 delegate.On("WriteHeader", 200).Once() 21 22 assert.Equal(0, composite.StatusCode()) 23 composite.WriteHeader(200) 24 assert.Equal(200, composite.StatusCode()) 25 26 delegate.AssertExpectations(t) 27 } 28 29 func testResponseWriterCloseNotifier(t *testing.T) { 30 assert := assert.New(t) 31 32 { 33 var ( 34 delegate = new(mockResponseWriter) 35 composite http.CloseNotifier = Wrap(delegate) 36 ) 37 38 assert.Panics(func() { composite.CloseNotify() }) 39 delegate.AssertExpectations(t) 40 } 41 42 { 43 var ( 44 delegate = new(mockResponseWriterFull) 45 composite http.CloseNotifier = Wrap(delegate) 46 47 closeChannel = make(chan bool, 1) 48 expectedChannel <-chan bool = closeChannel 49 ) 50 51 delegate.On("CloseNotify").Return(expectedChannel).Once() 52 assert.Equal(expectedChannel, composite.CloseNotify()) 53 delegate.AssertExpectations(t) 54 } 55 } 56 57 func testResponseWriterHijacker(t *testing.T) { 58 var ( 59 assert = assert.New(t) 60 expectedConn net.Conn = &net.IPConn{} 61 62 buffer bytes.Buffer 63 expectedReadWriter = bufio.NewReadWriter( 64 bufio.NewReader(&buffer), 65 bufio.NewWriter(&buffer), 66 ) 67 ) 68 69 { 70 var ( 71 delegate = new(mockResponseWriter) 72 composite http.Hijacker = Wrap(delegate) 73 ) 74 75 conn, rw, err := composite.Hijack() 76 assert.Nil(conn) 77 assert.Nil(rw) 78 assert.Error(err) 79 80 delegate.AssertExpectations(t) 81 } 82 83 { 84 var ( 85 delegate = new(mockResponseWriterFull) 86 composite http.Hijacker = Wrap(delegate) 87 ) 88 89 delegate.On("Hijack").Return(expectedConn, expectedReadWriter, error(nil)).Once() 90 conn, rw, err := composite.Hijack() 91 assert.Equal(expectedConn, conn) 92 assert.Equal(expectedReadWriter, rw) 93 assert.NoError(err) 94 95 delegate.AssertExpectations(t) 96 } 97 } 98 99 func testResponseWriterFlusher(t *testing.T) { 100 { 101 var ( 102 delegate = new(mockResponseWriter) 103 composite http.Flusher = Wrap(delegate) 104 ) 105 106 composite.Flush() 107 delegate.AssertExpectations(t) 108 } 109 110 { 111 var ( 112 delegate = new(mockResponseWriterFull) 113 composite http.Flusher = Wrap(delegate) 114 ) 115 116 delegate.On("Flush").Once() 117 composite.Flush() 118 delegate.AssertExpectations(t) 119 } 120 } 121 122 func testResponseWriterPusher(t *testing.T) { 123 var ( 124 assert = assert.New(t) 125 expectedTarget = "expectedTarget" 126 expectedOptions = &http.PushOptions{Method: "GET"} 127 ) 128 129 { 130 var ( 131 delegate = new(mockResponseWriter) 132 composite http.Pusher = Wrap(delegate) 133 ) 134 135 assert.Error(composite.Push(expectedTarget, expectedOptions)) 136 delegate.AssertExpectations(t) 137 } 138 139 { 140 var ( 141 delegate = new(mockResponseWriterFull) 142 composite http.Pusher = Wrap(delegate) 143 ) 144 145 delegate.On("Push", expectedTarget, expectedOptions).Return(error(nil)).Once() 146 assert.NoError(composite.Push(expectedTarget, expectedOptions)) 147 delegate.AssertExpectations(t) 148 } 149 } 150 151 func TestResponseWriter(t *testing.T) { 152 t.Run("Wrapping", testResponseWriterWrapping) 153 t.Run("CloseNotifier", testResponseWriterCloseNotifier) 154 t.Run("Hijacker", testResponseWriterHijacker) 155 t.Run("Flusher", testResponseWriterFlusher) 156 t.Run("Pusher", testResponseWriterPusher) 157 }