Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(703)

Delta Between Two Patch Sets: src/pkg/http/httptest/recorder.go

Issue 4239076: code review 4239076: http: change ResponseWriter.SetHeader(k,v) to Header() ... (Closed)
Left Patch Set: Created 13 years ago
Right Patch Set: diff -r a5f4ae5ff5fa https://go.googlecode.com/hg/ Created 13 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Right: Side by side diff | Download
« no previous file with change/comment | « src/pkg/http/fs.go ('k') | src/pkg/http/pprof/pprof.go » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(no file at all)
1 // Copyright 2011 The Go Authors. All rights reserved. 1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 // The httptest package provides utilities for HTTP testing. 5 // The httptest package provides utilities for HTTP testing.
6 package httptest 6 package httptest
7 7
8 import ( 8 import (
9 "bytes" 9 "bytes"
10 "http" 10 "http"
11 "os" 11 "os"
12 ) 12 )
13 13
14 // ResponseRecorder is an implementation of http.ResponseWriter that 14 // ResponseRecorder is an implementation of http.ResponseWriter that
15 // records its mutations for later inspection in tests. 15 // records its mutations for later inspection in tests.
16 type ResponseRecorder struct { 16 type ResponseRecorder struct {
17 » Code int // the HTTP response code from WriteHeader 17 » Code int // the HTTP response code from WriteHeader
18 » Header http.Header // if non-nil, the headers to populate 18 » HeaderMap http.Header // the HTTP response headers
19 » Body *bytes.Buffer // if non-nil, the bytes.Buffer to append written data to 19 » Body *bytes.Buffer // if non-nil, the bytes.Buffer to append w ritten data to
20 » Flushed bool 20 » Flushed bool
21
22 FakeRemoteAddr string // the fake RemoteAddr to return, or "" for Defaul tRemoteAddr 21 FakeRemoteAddr string // the fake RemoteAddr to return, or "" for Defaul tRemoteAddr
23 FakeUsingTLS bool // whether to return true from the UsingTLS method 22 FakeUsingTLS bool // whether to return true from the UsingTLS method
24 } 23 }
25 24
26 // NewRecorder returns an initialized ResponseRecorder. 25 // NewRecorder returns an initialized ResponseRecorder.
27 func NewRecorder() *ResponseRecorder { 26 func NewRecorder() *ResponseRecorder {
28 return &ResponseRecorder{ 27 return &ResponseRecorder{
29 » » Header: http.Header(make(map[string][]string)), 28 » » HeaderMap: make(http.Header),
30 » » Body: new(bytes.Buffer), 29 » » Body: new(bytes.Buffer),
31 } 30 }
32 } 31 }
33 32
34 // DefaultRemoteAddr is the default remote address to return in RemoteAddr if 33 // DefaultRemoteAddr is the default remote address to return in RemoteAddr if
35 // an explicit DefaultRemoteAddr isn't set on ResponseRecorder. 34 // an explicit DefaultRemoteAddr isn't set on ResponseRecorder.
36 const DefaultRemoteAddr = "1.2.3.4" 35 const DefaultRemoteAddr = "1.2.3.4"
37 36
38 // RemoteAddr returns the value of rw.FakeRemoteAddr, if set, else 37 // RemoteAddr returns the value of rw.FakeRemoteAddr, if set, else
39 // returns DefaultRemoteAddr. 38 // returns DefaultRemoteAddr.
40 func (rw *ResponseRecorder) RemoteAddr() string { 39 func (rw *ResponseRecorder) RemoteAddr() string {
41 if rw.FakeRemoteAddr != "" { 40 if rw.FakeRemoteAddr != "" {
42 return rw.FakeRemoteAddr 41 return rw.FakeRemoteAddr
43 } 42 }
44 return DefaultRemoteAddr 43 return DefaultRemoteAddr
45 } 44 }
46 45
47 // UsingTLS returns the fake value in rw.FakeUsingTLS 46 // UsingTLS returns the fake value in rw.FakeUsingTLS
48 func (rw *ResponseRecorder) UsingTLS() bool { 47 func (rw *ResponseRecorder) UsingTLS() bool {
49 return rw.FakeUsingTLS 48 return rw.FakeUsingTLS
50 } 49 }
51 50
52 // SetHeader populates rw.Header, if non-nil. 51 // Header returns the response headers.
53 func (rw *ResponseRecorder) SetHeader(k, v string) { 52 func (rw *ResponseRecorder) Header() http.Header {
54 » if rw.Header != nil { 53 » return rw.HeaderMap
55 » » if v == "" {
56 » » » rw.Header.Del(k)
57 » » } else {
58 » » » rw.Header.Set(k, v)
59 » » }
60 » }
61 } 54 }
62 55
63 // Write always succeeds and writes to rw.Body, if not nil. 56 // Write always succeeds and writes to rw.Body, if not nil.
64 func (rw *ResponseRecorder) Write(buf []byte) (int, os.Error) { 57 func (rw *ResponseRecorder) Write(buf []byte) (int, os.Error) {
65 if rw.Body != nil { 58 if rw.Body != nil {
66 rw.Body.Write(buf) 59 rw.Body.Write(buf)
67 } 60 }
68 return len(buf), nil 61 return len(buf), nil
69 } 62 }
70 63
71 // WriteHeader sets rw.Code. 64 // WriteHeader sets rw.Code.
72 func (rw *ResponseRecorder) WriteHeader(code int) { 65 func (rw *ResponseRecorder) WriteHeader(code int) {
73 rw.Code = code 66 rw.Code = code
74 } 67 }
75 68
76 // Flush sets rw.Flushed to true. 69 // Flush sets rw.Flushed to true.
77 func (rw *ResponseRecorder) Flush() { 70 func (rw *ResponseRecorder) Flush() {
78 rw.Flushed = true 71 rw.Flushed = true
79 } 72 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b