-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite_test.go
47 lines (38 loc) · 1.19 KB
/
write_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
package stompy
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
//implement SocketWriter
type MockStompWriter struct {
writeError error
flushError error
byteWriteError error
}
func (m *MockStompWriter) Write(p []byte) (n int, err error) {
return len(p), m.writeError
}
func (m *MockStompWriter) WriteByte(c byte) error {
return m.byteWriteError
}
func (m *MockStompWriter) Flush() error {
return m.flushError
}
func Test_write_frame_ok(t *testing.T) {
encoder := headerEncoderDecoder{"1.1"}
writer := &MockStompWriter{}
frame := Frame{Command: _COMMAND_CONNECT, Headers: StompHeaders{}, Body: _NULLBUFF}
err := writeFrame(writer, frame, encoder)
assert.NoError(t, err, "did not expect an error writing")
}
func Test_write_frame_err(t *testing.T) {
encoder := headerEncoderDecoder{"1.1"}
mockWriter := &MockStompWriter{flushError: errors.New("unexpected")}
frame := Frame{Command: _COMMAND_CONNECT, Headers: StompHeaders{}, Body: _NULLBUFF}
err := writeFrame(mockWriter, frame, encoder)
assert.Error(t, err, "expected an error to be returned when writing failed")
if _, ok := err.(ConnectionError); !ok {
assert.Fail(t, "error should be a connection Error")
}
}