-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresponse_test.go
More file actions
108 lines (91 loc) · 2.84 KB
/
Copy pathresponse_test.go
File metadata and controls
108 lines (91 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package middleware
import (
"bytes"
"io"
"net/http"
"strings"
"testing"
)
// response must satisfy io.ReaderFrom so that io.Copy (used by http.FileServer)
// can reach the underlying sendfile(2) fast path through the wrapper.
var _ io.ReaderFrom = (*response)(nil)
// readerFromWriter is a ResponseWriter that also implements io.ReaderFrom, just
// like the net/http response that supports the sendfile fast path.
type readerFromWriter struct {
buf bytes.Buffer
header http.Header
status int
usedReadFrom bool
}
func (w *readerFromWriter) Header() http.Header {
if w.header == nil {
w.header = http.Header{}
}
return w.header
}
func (w *readerFromWriter) Write(b []byte) (int, error) { return w.buf.Write(b) }
func (w *readerFromWriter) WriteHeader(status int) { w.status = status }
func (w *readerFromWriter) ReadFrom(src io.Reader) (int64, error) {
w.usedReadFrom = true
return w.buf.ReadFrom(src)
}
// plainWriter is a ResponseWriter that does not implement io.ReaderFrom, which
// forces the response wrapper to fall back to a regular copy.
type plainWriter struct {
buf bytes.Buffer
header http.Header
status int
}
func (w *plainWriter) Header() http.Header {
if w.header == nil {
w.header = http.Header{}
}
return w.header
}
func (w *plainWriter) Write(b []byte) (int, error) { return w.buf.Write(b) }
func (w *plainWriter) WriteHeader(status int) { w.status = status }
func TestResponseReadFromDelegates(t *testing.T) {
underlying := &readerFromWriter{}
w := &response{ResponseWriter: underlying}
data := "hello sendfile world"
n, err := w.ReadFrom(strings.NewReader(data))
if err != nil {
t.Fatalf("ReadFrom returned error: %s", err)
}
if !underlying.usedReadFrom {
t.Fatal("expected ReadFrom to delegate to the underlying io.ReaderFrom")
}
if n != int64(len(data)) {
t.Fatalf("expected %d bytes read, got %d", len(data), n)
}
if w.Length != len(data) {
t.Fatalf("expected recorded length %d, got %d", len(data), w.Length)
}
if w.Status != http.StatusOK {
t.Fatalf("expected status %d, got %d", http.StatusOK, w.Status)
}
if underlying.buf.String() != data {
t.Fatalf("expected body %q, got %q", data, underlying.buf.String())
}
}
func TestResponseReadFromFallback(t *testing.T) {
underlying := &plainWriter{}
w := &response{ResponseWriter: underlying}
data := "fallback copy path"
n, err := w.ReadFrom(strings.NewReader(data))
if err != nil {
t.Fatalf("ReadFrom returned error: %s", err)
}
if n != int64(len(data)) {
t.Fatalf("expected %d bytes read, got %d", len(data), n)
}
if w.Length != len(data) {
t.Fatalf("expected recorded length %d, got %d", len(data), w.Length)
}
if w.Status != http.StatusOK {
t.Fatalf("expected status %d, got %d", http.StatusOK, w.Status)
}
if underlying.buf.String() != data {
t.Fatalf("expected body %q, got %q", data, underlying.buf.String())
}
}