forked from szatmary/sonos
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmock_client_test.go
More file actions
93 lines (83 loc) · 3.18 KB
/
mock_client_test.go
File metadata and controls
93 lines (83 loc) · 3.18 KB
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
package sonos
import (
"bytes"
"io"
"net/http"
"net/url"
"strings"
"testing"
)
// MockRoundTripper implements http.RoundTripper to intercept requests
type MockRoundTripper struct {
Handlers map[string]func(*http.Request) (*http.Response, error)
}
func (m *MockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
// 1. Check for SOAPAction header
soapAction := req.Header.Get("SOAPAction")
// Clean quotes if present
soapAction = strings.Trim(soapAction, "\"")
if handler, ok := m.Handlers[soapAction]; ok {
return handler(req)
}
// 2. Check for Device Description request (used in NewZonePlayer)
if strings.Contains(req.URL.Path, "device_description.xml") {
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(mockDeviceDescription)),
Header: make(http.Header),
}, nil
}
return &http.Response{
StatusCode: http.StatusNotFound,
Body: io.NopCloser(bytes.NewBufferString("Not found")),
Header: make(http.Header),
}, nil
}
// Helper to create a mocked ZonePlayer
func NewMockZonePlayer(t *testing.T, handlers map[string]func(*http.Request) (*http.Response, error)) *ZonePlayer {
mockClient := &http.Client{
Transport: &MockRoundTripper{Handlers: handlers},
}
// The location doesn't matter much as we intercept requests, but it needs to be valid
loc, _ := url.Parse("http://192.168.1.100:1400/xml/device_description.xml")
zp, err := NewZonePlayer(WithClient(mockClient), WithLocation(loc))
if err != nil {
t.Fatalf("Failed to create mock ZonePlayer: %v", err)
}
return zp
}
const mockDeviceDescription = `<?xml version="1.0" encoding="utf-8" ?>
<root xmlns="urn:schemas-upnp-org:device-1-0">
<specVersion>
<major>1</major>
<minor>0</minor>
</specVersion>
<device>
<deviceType>urn:schemas-upnp-org:device:ZonePlayer:1</deviceType>
<friendlyName>Kitchen</friendlyName>
<manufacturer>Sonos, Inc.</manufacturer>
<modelName>Sonos One</modelName>
<modelDescription>Sonos One Audio Player</modelDescription>
<UDN>uuid:RINCON_000E58CDCA4001400</UDN>
<serialNum>00-11-22-33-44-55:0</serialNum>
<softwareVersion>57.2-54070</softwareVersion>
<hardwareVersion>1.20.1.6-1.2</hardwareVersion>
<roomName>Kitchen</roomName>
<serviceList>
<service>
<serviceType>urn:schemas-upnp-org:service:RenderingControl:1</serviceType>
<serviceId>urn:upnp-org:serviceId:RenderingControl</serviceId>
<controlURL>/MediaRenderer/RenderingControl/Control</controlURL>
<eventSubURL>/MediaRenderer/RenderingControl/Event</eventSubURL>
<SCPDURL>/xml/RenderingControl1.xml</SCPDURL>
</service>
<service>
<serviceType>urn:schemas-upnp-org:service:AVTransport:1</serviceType>
<serviceId>urn:upnp-org:serviceId:AVTransport</serviceId>
<controlURL>/MediaRenderer/AVTransport/Control</controlURL>
<eventSubURL>/MediaRenderer/AVTransport/Event</eventSubURL>
<SCPDURL>/xml/AVTransport1.xml</SCPDURL>
</service>
</serviceList>
</device>
</root>`