-
Notifications
You must be signed in to change notification settings - Fork 14
/
mercury_test.go
78 lines (63 loc) · 2.02 KB
/
mercury_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
package spotcontrol
import (
"encoding/binary"
Spotify "github.com/badfortrains/spotcontrol/proto"
"github.com/golang/protobuf/proto"
"testing"
)
func setupTestController(stream packetStream) *SpircController {
s := &session{
stream: stream,
deviceId: "testDevice",
}
s.mercury = setupMercury(s)
return setupController(s, "fakeUser", []byte{})
}
func TestMultiPart(t *testing.T) {
stream := &fakeStream{
recvPackets: make(chan shanPacket, 5),
sendPackets: make(chan shanPacket, 5),
}
controller := setupTestController(stream)
subHeader := &Spotify.Header{
Uri: proto.String("hm://searchview/km/v2/search/Future"),
}
subHeaderData, _ := proto.Marshal(subHeader)
header := &Spotify.Header{
Uri: proto.String("hm://searchview/km/v2/search/Future"),
ContentType: proto.String("application/json; charset=UTF-8"),
StatusCode: proto.Int32(200),
}
body := []byte("{searchResults: {tracks: [], albums: [], tracks: []}}")
headerData, _ := proto.Marshal(header)
seq := []byte{0, 0, 0, 1}
p0, _ := encodeMercuryHead([]byte{0, 0, 0, 0}, 1, 1)
binary.Write(p0, binary.BigEndian, uint16(len(subHeaderData)))
p0.Write(subHeaderData)
p1, _ := encodeMercuryHead(seq, 1, 0)
binary.Write(p1, binary.BigEndian, uint16(len(headerData)))
p1.Write(headerData)
p2, _ := encodeMercuryHead(seq, 1, 1)
binary.Write(p2, binary.BigEndian, uint16(len(body)))
p2.Write(body)
didRecieveCallback := false
controller.session.mercurySendRequest(mercuryRequest{
method: "SEND",
uri: "hm://searchview/km/v2/search/Future",
payload: [][]byte{},
}, func(res mercuryResponse) {
didRecieveCallback = true
if string(res.payload[0]) != string(body) {
t.Errorf("bad body received")
}
})
stream.recvPackets <- shanPacket{cmd: 0xb2, buf: p0.Bytes()}
stream.recvPackets <- shanPacket{cmd: 0xb2, buf: p1.Bytes()}
stream.recvPackets <- shanPacket{cmd: 0xb2, buf: p2.Bytes()}
controller.session.poll()
controller.session.poll()
controller.session.poll()
if !didRecieveCallback {
t.Errorf("never received callback")
}
}