1package http
2
3import (
4	"net/http"
5)
6
7// TestResponseWriter is a http.ResponseWriter object that keeps track of all activity
8// allowing you to make assertions about how it was used.
9//
10// DEPRECATED: We recommend you use http://golang.org/pkg/net/http/httptest instead.
11type TestResponseWriter struct {
12
13	// StatusCode is the last int written by the call to WriteHeader(int)
14	StatusCode int
15
16	// Output is a string containing the written bytes using the Write([]byte) func.
17	Output string
18
19	// header is the internal storage of the http.Header object
20	header http.Header
21}
22
23// Header gets the http.Header describing the headers that were set in this response.
24func (rw *TestResponseWriter) Header() http.Header {
25
26	if rw.header == nil {
27		rw.header = make(http.Header)
28	}
29
30	return rw.header
31}
32
33// Write writes the specified bytes to Output.
34func (rw *TestResponseWriter) Write(bytes []byte) (int, error) {
35
36	// assume 200 success if no header has been set
37	if rw.StatusCode == 0 {
38		rw.WriteHeader(200)
39	}
40
41	// add these bytes to the output string
42	rw.Output = rw.Output + string(bytes)
43
44	// return normal values
45	return 0, nil
46
47}
48
49// WriteHeader stores the HTTP status code in the StatusCode.
50func (rw *TestResponseWriter) WriteHeader(i int) {
51	rw.StatusCode = i
52}
53