-
-
Notifications
You must be signed in to change notification settings - Fork 239
/
printer_test.go
83 lines (63 loc) · 1.71 KB
/
printer_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
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
package httpexpect
import (
"bytes"
"errors"
"io"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
type failingReader struct{}
func (failingReader) Read(_ []byte) (n int, err error) {
return 0, errors.New("error")
}
func (failingReader) Close() error {
return errors.New("error")
}
func TestPrinter_Compact(t *testing.T) {
printer := NewCompactPrinter(t)
body1 := bytes.NewBufferString("body1")
body2 := bytes.NewBufferString("body2")
req1, _ := http.NewRequest("GET", "http://example.com", body1)
req2, _ := http.NewRequest("GET", "http://example.com", nil)
printer.Request(req1)
printer.Request(req2)
printer.Request(nil)
printer.Response(&http.Response{Body: io.NopCloser(body2)}, 0)
printer.Response(&http.Response{}, 0)
printer.Response(nil, 0)
}
func TestPrinter_Debug(t *testing.T) {
printer := NewDebugPrinter(t, true)
body1 := bytes.NewBufferString("body1")
body2 := bytes.NewBufferString("body2")
req1, _ := http.NewRequest("GET", "http://example.com", body1)
req2, _ := http.NewRequest("GET", "http://example.com", nil)
printer.Request(req1)
printer.Request(req2)
printer.Request(nil)
printer.Response(&http.Response{Body: io.NopCloser(body2)}, 0)
printer.Response(&http.Response{}, 0)
printer.Response(nil, 0)
}
func TestPrinter_Panics(t *testing.T) {
t.Run("CurlPrinter", func(t *testing.T) {
curl := NewCurlPrinter(t)
assert.Panics(t, func() {
curl.Request(&http.Request{})
})
})
t.Run("DebugPrinter", func(t *testing.T) {
curl := NewDebugPrinter(t, true)
assert.Panics(t, func() {
curl.Request(&http.Request{
Body: failingReader{},
})
})
assert.Panics(t, func() {
curl.Response(&http.Response{
Body: failingReader{},
}, 0)
})
})
}