mirror of
https://github.com/Eggbertx/gochan.git
synced 2025-09-04 05:56:23 -07:00
36 lines
733 B
Go
36 lines
733 B
Go
package testutil
|
|
|
|
import (
|
|
"bytes"
|
|
"net/http"
|
|
)
|
|
|
|
// MockResponseWriter can be used in place of a http.ResponseWriter interface for tests.
|
|
// It will panic if any of its methods are run in a non-test environment
|
|
type MockResponseWriter struct {
|
|
StatusCode int
|
|
Buffer *bytes.Buffer
|
|
header http.Header
|
|
}
|
|
|
|
func (m *MockResponseWriter) Header() http.Header {
|
|
PanicIfNotTest()
|
|
if m.header == nil {
|
|
m.header = make(http.Header)
|
|
}
|
|
return m.header
|
|
|
|
}
|
|
|
|
func (m *MockResponseWriter) Write(ba []byte) (int, error) {
|
|
PanicIfNotTest()
|
|
if m.Buffer == nil {
|
|
m.Buffer = new(bytes.Buffer)
|
|
}
|
|
return m.Buffer.Write(ba)
|
|
}
|
|
|
|
func (m *MockResponseWriter) WriteHeader(statusCode int) {
|
|
PanicIfNotTest()
|
|
m.StatusCode = statusCode
|
|
}
|