71 lines
1.7 KiB
Go
71 lines
1.7 KiB
Go
/*
|
|
Copyright 2024 The Kubernetes Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package responsewriter
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
// inMemoryResponseWriter is a http.Writer that keep the response in memory.
|
|
type inMemoryResponseWriter struct {
|
|
writeHeaderCalled bool
|
|
header http.Header
|
|
respCode int
|
|
data []byte
|
|
}
|
|
|
|
func NewInMemoryResponseWriter() *inMemoryResponseWriter {
|
|
return &inMemoryResponseWriter{header: http.Header{}}
|
|
}
|
|
|
|
func (r *inMemoryResponseWriter) Header() http.Header {
|
|
return r.header
|
|
}
|
|
|
|
func (r *inMemoryResponseWriter) RespCode() int {
|
|
return r.respCode
|
|
}
|
|
|
|
func (r *inMemoryResponseWriter) Data() []byte {
|
|
return r.data
|
|
}
|
|
|
|
func (r *inMemoryResponseWriter) WriteHeader(code int) {
|
|
r.writeHeaderCalled = true
|
|
r.respCode = code
|
|
}
|
|
|
|
func (r *inMemoryResponseWriter) Write(in []byte) (int, error) {
|
|
if !r.writeHeaderCalled {
|
|
r.WriteHeader(http.StatusOK)
|
|
}
|
|
r.data = append(r.data, in...)
|
|
return len(in), nil
|
|
}
|
|
|
|
func (r *inMemoryResponseWriter) String() string {
|
|
s := fmt.Sprintf("ResponseCode: %d", r.respCode)
|
|
if r.data != nil {
|
|
s += fmt.Sprintf(", Body: %s", string(r.data))
|
|
}
|
|
if r.header != nil {
|
|
s += fmt.Sprintf(", Header: %s", r.header)
|
|
}
|
|
return s
|
|
}
|