Source File
recorder.go
Belonging Package
net/http/httptest
package httptest
import (
)
Flushed bool
result *http.Response // cache of Result's return value
snapHeader http.Header // snapshot of HeaderMap at first Write
wroteHeader bool
}
const DefaultRemoteAddr = "1.2.3.4"
func ( *ResponseRecorder) ( []byte, string) {
if .wroteHeader {
return
}
if len() > 512 {
= [:512]
}
:= .Header()
, := ["Content-Type"]
:= .Get("Transfer-Encoding") != ""
if ! && ! {
if == nil {
= []byte()
}
.Set("Content-Type", http.DetectContentType())
}
.WriteHeader(200)
}
func ( *ResponseRecorder) ( string) (int, error) {
.writeHeader(nil, )
if .Body != nil {
.Body.WriteString()
}
return len(), nil
}
func ( *ResponseRecorder) ( int) {
if .wroteHeader {
return
}
.Code =
.wroteHeader = true
if .HeaderMap == nil {
.HeaderMap = make(http.Header)
}
.snapHeader = .HeaderMap.Clone()
}
func ( *ResponseRecorder) () {
if !.wroteHeader {
.WriteHeader(200)
}
.Flushed = true
}
func ( *ResponseRecorder) () *http.Response {
if .result != nil {
return .result
}
if .snapHeader == nil {
.snapHeader = .HeaderMap.Clone()
}
:= &http.Response{
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
StatusCode: .Code,
Header: .snapHeader,
}
.result =
if .StatusCode == 0 {
.StatusCode = 200
}
.Status = fmt.Sprintf("%03d %s", .StatusCode, http.StatusText(.StatusCode))
if .Body != nil {
.Body = io.NopCloser(bytes.NewReader(.Body.Bytes()))
} else {
.Body = http.NoBody
}
.ContentLength = parseContentLength(.Header.Get("Content-Length"))
if , := .snapHeader["Trailer"]; {
.Trailer = make(http.Header, len())
for , := range {
= http.CanonicalHeaderKey()
continue
}
, := .HeaderMap[]
if ! {
continue
}
:= make([]string, len())
copy(, )
.Trailer[] =
}
}
for , := range .HeaderMap {
if !strings.HasPrefix(, http.TrailerPrefix) {
continue
}
if .Trailer == nil {
.Trailer = make(http.Header)
}
for , := range {
.Trailer.Add(strings.TrimPrefix(, http.TrailerPrefix), )
}
}
return
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |