-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpenname.go
82 lines (69 loc) · 2.05 KB
/
penname.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
// Package penname provides a mock that implements the Closer & Writer
// interfaces for testing
package penname
import (
"bytes"
"fmt"
"net/http"
)
// PenName contains the state of the mock for performing its designated actions
// and capturing details for assertions
type PenName struct {
Closed bool
written bytes.Buffer
writtenHeaders bytes.Buffer
returnError error
}
// New returns an initialized PenName for use in tests
func New() *PenName {
return &PenName{}
}
// Close implements the closer interface, returning an error if returnError is
// set. Whether or not Close is called is stored in Closed for inspection
// later.
func (p *PenName) Close() error {
if p.returnError != nil {
return p.returnError
}
p.Closed = true
return nil
}
// Header implements the ResponseWriter interface, returning an empty set of
// headers to meet the interface requirements
func (p *PenName) Header() http.Header {
return http.Header{}
}
// Reset is a convencinece method for reseting the state of the mock
func (p *PenName) Reset() {
p.Closed = false
p.written.Reset()
p.writtenHeaders.Reset()
}
// ReturnError sets the error that will be returned when actions are attempted
func (p *PenName) ReturnError(err error) {
p.returnError = err
}
// Write implements the Writer interface, returning an error if returnError is
// set. The contents of what is written is stored in Written for inspection
// later.
func (p *PenName) Write(b []byte) (n int, err error) {
if p.returnError != nil {
return 0, p.returnError
}
p.written.Write(b)
return len(b), nil
}
// Written returns the bytes that have been written to the writer
func (p *PenName) Written() []byte {
return p.written.Bytes()
}
// WriteHeader implements the ResponseWriter interface, capturing headers to a
// separate
func (p *PenName) WriteHeader(i int) {
p.writtenHeaders.WriteString(fmt.Sprintf("Header: %v", i))
}
// WrittenHeaders returns the bytes of the headers that have been written to
// the writer
func (p *PenName) WrittenHeaders() []byte {
return p.writtenHeaders.Bytes()
}