-
Notifications
You must be signed in to change notification settings - Fork 4
/
request_test.go
82 lines (72 loc) · 2.04 KB
/
request_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
package jellyfin
import (
"net/url"
"reflect"
"testing"
)
func TestClient_encodeGETUrl(t *testing.T) {
tests := []struct {
name string
c *Client
endpoint string
params params
want string
wantErr bool
}{
{
name: "POSITIVE - encodes the url",
c: &Client{
baseURL: &url.URL{
Scheme: "https",
Host: "jellyfin.example.com",
},
},
endpoint: "/audio/1234/stream",
params: params{
"UserId": "userID",
"DeviceId": "deviceID",
"playSessionId": "deviceID",
"static": "true",
"api_key": "5678",
},
want: "https://jellyfin.example.com/audio/1234/stream?UserId=userID&DeviceId=deviceID&playSessionId=deviceID&static=true&api_key=5678",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.c.encodeGETUrl(tt.endpoint, tt.params)
if (err != nil) != tt.wantErr {
t.Errorf("Client.encodeGETUrl() error = %v, wantErr %v", err, tt.wantErr)
return
}
// since params was a map, we can't do a direct string comparison. The map values can get added in any order.
// gotta parse out the 'want' url and the 'got' url and determine if the query values are the same.
wantParsed, err := url.Parse(tt.want)
if err != nil {
t.Errorf("unable to parse want url: %v", err)
}
gotParsed, err := url.Parse(got)
if err != nil {
t.Errorf("unable to parse got url: %v", err)
}
if wantParsed.Scheme != gotParsed.Scheme {
t.Errorf("schemes did not match, got %v, want %v", got, tt.want)
}
if wantParsed.Host != gotParsed.Host {
t.Errorf("hosts did not match, got %v, want %v", got, tt.want)
}
// compare the queries
for k, v := range wantParsed.Query() {
gotValue, contained := gotParsed.Query()[k]
if !contained {
t.Errorf("param [%s] not contained in got", k)
}
// contained but now check the value
if !reflect.DeepEqual(v, gotValue) {
t.Errorf("value of param [%s] %v was not equal to got %v", k, v, gotValue)
}
}
})
}
}