github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/httplib/context/respwriter.go (about)

     1  package context
     2  
     3  import (
     4  	"bufio"
     5  	"bytes"
     6  	"net"
     7  	"net/http"
     8  )
     9  
    10  type TestResponseWriter struct {
    11  	http.ResponseWriter
    12  	http.Hijacker
    13  	http.Flusher
    14  	http.CloseNotifier
    15  	Body            bytes.Buffer
    16  	WHeader         http.Header
    17  	HttpStatus      int
    18  	written         bool
    19  	CloseNotifyChan <-chan bool
    20  }
    21  
    22  func (w *TestResponseWriter) Header() http.Header {
    23  	if w.WHeader == nil {
    24  		w.WHeader = make(map[string][]string)
    25  	}
    26  	return w.WHeader
    27  }
    28  
    29  func (w *TestResponseWriter) WriteString(data string) (int, error) {
    30  	return w.Write([]byte(data))
    31  }
    32  
    33  func (w *TestResponseWriter) Write(data []byte) (int, error) {
    34  	w.written = true
    35  	return w.Body.Write(data)
    36  }
    37  
    38  func (w *TestResponseWriter) WriteHeader(status int) {
    39  	w.HttpStatus = status
    40  }
    41  
    42  func (w *TestResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
    43  	return nil, nil, nil
    44  }
    45  
    46  func (w *TestResponseWriter) Flush() {
    47  }
    48  
    49  func (w *TestResponseWriter) CloseNotify() <-chan bool {
    50  	return w.CloseNotifyChan
    51  }
    52  
    53  func (w *TestResponseWriter) Status() int {
    54  	return w.HttpStatus
    55  }
    56  
    57  func (w *TestResponseWriter) Size() int {
    58  	return w.Body.Len()
    59  }
    60  
    61  func (w *TestResponseWriter) Written() bool {
    62  	return w.written
    63  }
    64  
    65  func (w *TestResponseWriter) WriteHeaderNow() {
    66  	if !w.Written() {
    67  		w.WriteHeader(w.HttpStatus)
    68  	}
    69  }