-
-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathskip_response_writer_test.go
49 lines (44 loc) · 1.09 KB
/
skip_response_writer_test.go
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
package goa
import (
"bytes"
"io"
"strings"
"testing"
)
func TestSkipResponseWriter(t *testing.T) {
const input = "Hello, World!"
var responseWriter io.ReadCloser
responseWriter = SkipResponseWriter(strings.NewReader(input))
defer func() {
err := responseWriter.Close()
if err != nil {
t.Error(err)
}
}()
_, ok := responseWriter.(io.WriterTo)
if !ok {
t.Errorf("SkipResponseWriter's result must implement io.WriterTo")
}
var writerToBuffer bytes.Buffer
_, err := io.Copy(&writerToBuffer, responseWriter) // io.Copy uses WriterTo if implemented
if err != nil {
t.Fatal(err)
}
if writerToBuffer.String() != input {
t.Errorf("WriteTo: expected=%q actual=%q", input, writerToBuffer.String())
}
responseWriter = SkipResponseWriter(strings.NewReader(input))
defer func() {
err := responseWriter.Close()
if err != nil {
t.Error(err)
}
}()
readBytes, err := io.ReadAll(responseWriter) // io.ReadAll ignores WriterTo and calls Read
if err != nil {
t.Fatal(err)
}
if string(readBytes) != input {
t.Errorf("Read: expected=%q actual=%q", input, string(readBytes))
}
}