forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage_test.go
160 lines (121 loc) · 4.93 KB
/
message_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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package quickfix
import (
"bytes"
"github.com/quickfixgo/quickfix/enum"
"testing"
)
var msgResult Message
func BenchmarkParseMessage(b *testing.B) {
rawMsg := []byte("8=FIX.4.29=10435=D34=249=TW52=20140515-19:49:56.65956=ISLD11=10021=140=154=155=TSLA60=00010101-00:00:00.00010=039")
var msg Message
for i := 0; i < b.N; i++ {
msg, _ = ParseMessage(rawMsg)
}
msgResult = msg
}
func TestMessage_ParseMessage(t *testing.T) {
rawMsg := []byte("8=FIX.4.29=10435=D34=249=TW52=20140515-19:49:56.65956=ISLD11=10021=140=154=155=TSLA60=00010101-00:00:00.00010=039")
msg, err := ParseMessage(rawMsg)
if err != nil {
t.Error("Unexpected error, ", err)
}
if !bytes.Equal(rawMsg, msg.rawMessage) {
t.Error("Expected msg bytes to equal raw bytes")
}
expectedBodyBytes := []byte("11=10021=140=154=155=TSLA60=00010101-00:00:00.000")
if !bytes.Equal(msg.bodyBytes, expectedBodyBytes) {
t.Error("Incorrect body bytes, got ", string(msg.bodyBytes))
}
expectedLenFields := 14
if len(msg.fields) != expectedLenFields {
t.Errorf("Expected %v fields, got %v", expectedLenFields, len(msg.fields))
}
}
func TestMessage_parseOutOfOrder(t *testing.T) {
//allow fields out of order, save for validation
rawMsg := []byte("8=FIX.4.09=8135=D11=id21=338=10040=154=155=MSFT34=249=TW52=20140521-22:07:0956=ISLD10=250")
_, err := ParseMessage(rawMsg)
if err != nil {
t.Error("Should not have gotten error, got ", err)
}
}
func TestMessage_Build(t *testing.T) {
builder := NewMessage()
builder.Header.SetField(tagBeginString, FIXString(enum.BeginStringFIX44))
builder.Header.SetField(tagMsgType, FIXString("A"))
builder.Header.SetField(tagSendingTime, FIXString("20140615-19:49:56"))
builder.Body.SetField(Tag(553), FIXString("my_user"))
builder.Body.SetField(Tag(554), FIXString("secret"))
expectedBytes := []byte("8=FIX.4.49=4935=A52=20140615-19:49:56553=my_user554=secret10=072")
result, err := builder.Build()
if err != nil {
t.Error("Unexpected error", err)
}
if !bytes.Equal(expectedBytes, result) {
t.Error("Unexpected bytes, got ", string(result))
}
}
func TestMessage_ReBuild(t *testing.T) {
rawMsg := []byte("8=FIX.4.29=10435=D34=249=TW52=20140515-19:49:56.65956=ISLD11=10021=140=154=155=TSLA60=00010101-00:00:00.00010=039")
msg, _ := ParseMessage(rawMsg)
msg.Header.SetField(tagOrigSendingTime, FIXString("20140515-19:49:56.659"))
msg.Header.SetField(tagSendingTime, FIXString("20140615-19:49:56"))
msg.Build()
expectedBytes := []byte("8=FIX.4.29=12635=D34=249=TW52=20140615-19:49:5656=ISLD122=20140515-19:49:56.65911=10021=140=154=155=TSLA60=00010101-00:00:00.00010=128")
if !bytes.Equal(expectedBytes, msg.rawMessage) {
t.Errorf("Unexpected bytes,\n +%s\n-%s", msg.rawMessage, expectedBytes)
}
expectedBodyBytes := []byte("11=10021=140=154=155=TSLA60=00010101-00:00:00.000")
if !bytes.Equal(msg.bodyBytes, expectedBodyBytes) {
t.Error("Incorrect body bytes, got ", string(msg.bodyBytes))
}
}
func TestMessage_reverseRoute(t *testing.T) {
msg, _ := ParseMessage([]byte("8=FIX.4.29=17135=D34=249=TW50=KK52=20060102-15:04:0556=ISLD57=AP144=BB115=JCD116=CS128=MG129=CB142=JV143=RY145=BH11=ID21=338=10040=w54=155=INTC60=20060102-15:04:0510=123"))
builder := msg.reverseRoute()
var testCases = []struct {
tag Tag
expectedValue string
}{
{tagTargetCompID, "TW"},
{tagTargetSubID, "KK"},
{tagTargetLocationID, "JV"},
{tagSenderCompID, "ISLD"},
{tagSenderSubID, "AP"},
{tagSenderLocationID, "RY"},
{tagDeliverToCompID, "JCD"},
{tagDeliverToSubID, "CS"},
{tagDeliverToLocationID, "BB"},
{tagOnBehalfOfCompID, "MG"},
{tagOnBehalfOfSubID, "CB"},
{tagOnBehalfOfLocationID, "BH"},
}
for _, tc := range testCases {
var field FIXString
err := builder.Header.GetField(tc.tag, &field)
if err != nil {
t.Error("Unexpected error, ", err)
}
if string(field) != tc.expectedValue {
t.Errorf("Expected %v got %v", tc.expectedValue, field)
}
}
}
func TestMessage_reverseRouteIgnoreEmpty(t *testing.T) {
msg, _ := ParseMessage([]byte("8=FIX.4.09=12835=D34=249=TW52=20060102-15:04:0556=ISLD115=116=CS128=MG129=CB11=ID21=338=10040=w54=155=INTC60=20060102-15:04:0510=123"))
builder := msg.reverseRoute()
if builder.Header.Has(tagDeliverToCompID) {
t.Error("Should not reverse if empty")
}
}
func TestMessage_reverseRouteFIX40(t *testing.T) {
//onbehalfof/deliverto location id not supported in fix 4.0
msg, _ := ParseMessage([]byte("8=FIX.4.09=17135=D34=249=TW50=KK52=20060102-15:04:0556=ISLD57=AP144=BB115=JCD116=CS128=MG129=CB142=JV143=RY145=BH11=ID21=338=10040=w54=155=INTC60=20060102-15:04:0510=123"))
builder := msg.reverseRoute()
if builder.Header.Has(tagDeliverToLocationID) {
t.Error("delivertolocation id not supported in fix40")
}
if builder.Header.Has(tagOnBehalfOfLocationID) {
t.Error("onbehalfof location id not supported in fix40")
}
}