-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
134 lines (111 loc) · 3.71 KB
/
client_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
package espn
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
func TestBasePaths(t *testing.T) {
var tests = []struct {
year int
wantLeaguePath string
}{
{2022, "/ffl/seasons/2022/segments/0/leagues/12345"},
{2017, "/ffl/leagueHistory/12345?seasonId=2017"},
}
expectedEspnURL := "https://fantasy.espn.com/apis/v3/games"
for _, tt := range tests {
t.Run(fmt.Sprintf("%d", tt.year), func(t *testing.T) {
expectedBasePath := fmt.Sprintf("/ffl/seasons/%d", tt.year)
client := newPublicClient(GameTypeNfl, "12345", tt.year)
if client.espnURL != expectedEspnURL {
t.Errorf("expected ESPN URL to be %s, got %s", expectedEspnURL, client.espnURL)
}
if client.basePath != expectedBasePath {
t.Errorf("expected base path to be %s, got %s", expectedBasePath, client.basePath)
}
if client.baseLeaguePath != tt.wantLeaguePath {
t.Errorf("expected league path to be %s, got %s", tt.wantLeaguePath, client.baseLeaguePath)
}
})
}
}
func TestBaseRequest(t *testing.T) {
var tests = []struct {
name string
espnS2 string
swid string
filter string
}{
{"publicNoFilter", "", "", ""},
{"publicFilter", "", "", "{\"foo\": \"bar\"}"},
{"privateNoFilter", "private_espn_s2", "private_swid", ""},
{"privateFilter", "private_espn_s2", "private_swid", "{\"foo\": \"bar\"}"},
}
expectedPath := "/ffl/seasons/2022/segments/0/leagues/12345"
expectedQueryParams := map[string][]string{
"view": {"mView1", "mView2"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println("starting request check")
if r.URL.Path != expectedPath {
t.Errorf("expected request to %s, got %s", expectedPath, r.URL.Path)
}
if fmt.Sprintf("%s", r.URL.Query()) != fmt.Sprintf("%s", expectedQueryParams) {
t.Errorf("expected query params to be %s, got %s", expectedQueryParams, r.URL.Query())
}
if h := r.Header[http.CanonicalHeaderKey("x-fantasy-filter")]; h != nil {
t.Errorf("expected header x-fantasy-filter to be nil, got %s", h)
}
expectedCookies := map[string]string{
"espn_s2": tt.espnS2,
"SWID": tt.swid,
}
for _, c := range r.Cookies() {
fmt.Printf("%s: %s\n", c.Name, c.Value)
}
for k, v := range expectedCookies {
cookieVal, err := r.Cookie(k)
if v == "" && err == nil {
t.Errorf("expected cookie %s to be nil, got %s", k, cookieVal)
} else if v != "" && err != nil {
t.Errorf("expected cookie %s to be %s, got error %s", k, v, err)
} else if v != "" && v != cookieVal.Value {
t.Errorf("expected cookie %s to be %s, got %s", k, v, cookieVal.Value)
}
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{}`))
}))
defer server.Close()
var client *espnClient
if tt.espnS2 == "" && tt.swid == "" {
client = newPublicClient(GameTypeNfl, "12345", 2022)
} else {
client = newPrivateClient(GameTypeNfl, "12345", 2022, tt.espnS2, tt.swid)
}
// Point client at the test server and copy over any cookies set for the ESPN URL
client.espnURL = server.URL
serverURL, _ := url.Parse(server.URL)
espnURL, _ := url.Parse(espnBaseURL)
if client.HTTPClient.Jar != nil {
cookies := make([]*http.Cookie, 0)
for _, c := range client.HTTPClient.Jar.Cookies(espnURL) {
cookies = append(cookies, &http.Cookie{
Name: c.Name,
Value: c.Value,
})
}
client.HTTPClient.Jar.SetCookies(serverURL, cookies)
}
res := LeagueInfoResponseJSON{}
err := client.getLeagueInternal([]string{"mView1", "mView2"}, "", "", &res)
if err != nil {
t.Errorf("Error in getLeagueInternal: %s", err)
}
})
}
}