|
4 | 4 | "bytes" |
5 | 5 | "crypto/aes" |
6 | 6 | "crypto/cipher" |
| 7 | + "fmt" |
7 | 8 | "io" |
8 | 9 | "net/http" |
9 | 10 | "os" |
@@ -108,6 +109,155 @@ func TestExtractClientID(t *testing.T) { |
108 | 109 | } |
109 | 110 | } |
110 | 111 |
|
| 112 | +func TestNewClient(t *testing.T) { |
| 113 | + html := `<html><body><script src="https://a-v2.sndcdn.com/assets/app-123.js"></script></body></html>` |
| 114 | + js := `(function(){ bla bla client_id:"my-client-id-123" bla bla })` |
| 115 | + |
| 116 | + transport := &mockTransport{ |
| 117 | + RoundTripFunc: func(req *http.Request) (*http.Response, error) { |
| 118 | + if req.URL.String() == "https://mock.com" { |
| 119 | + return &http.Response{ |
| 120 | + StatusCode: 200, |
| 121 | + Body: io.NopCloser(strings.NewReader(html)), |
| 122 | + }, nil |
| 123 | + } |
| 124 | + if req.URL.String() == "https://a-v2.sndcdn.com/assets/app-123.js" { |
| 125 | + return &http.Response{ |
| 126 | + StatusCode: 200, |
| 127 | + Body: io.NopCloser(strings.NewReader(js)), |
| 128 | + }, nil |
| 129 | + } |
| 130 | + return &http.Response{StatusCode: 404, Body: io.NopCloser(strings.NewReader("Not Found"))}, nil |
| 131 | + }, |
| 132 | + } |
| 133 | + |
| 134 | + httpClient := &http.Client{Transport: transport} |
| 135 | + client, err := newClient("https://mock.com", httpClient) |
| 136 | + if err != nil { |
| 137 | + t.Fatalf("newClient() error = %v", err) |
| 138 | + } |
| 139 | + |
| 140 | + if client.clientID != "my-client-id-123" { |
| 141 | + t.Errorf("got clientID %q, want %q", client.clientID, "my-client-id-123") |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +func TestNewClient_Direct(t *testing.T) { |
| 146 | + // We just want coverage for the wrapper function. |
| 147 | + // It's expected to fail in most environments without network. |
| 148 | + _, _ = NewClient() |
| 149 | +} |
| 150 | + |
| 151 | +func TestNewClient_Fail(t *testing.T) { |
| 152 | + transport := &mockTransport{ |
| 153 | + RoundTripFunc: func(req *http.Request) (*http.Response, error) { |
| 154 | + return nil, fmt.Errorf("fail") |
| 155 | + }, |
| 156 | + } |
| 157 | + httpClient := &http.Client{Transport: transport} |
| 158 | + _, err := newClient("http://mock", httpClient) |
| 159 | + if err == nil { |
| 160 | + t.Error("expected error") |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +func TestGetError(t *testing.T) { |
| 165 | + client := &Client{ |
| 166 | + httpClient: &http.Client{ |
| 167 | + Transport: &mockTransport{ |
| 168 | + RoundTripFunc: func(req *http.Request) (*http.Response, error) { |
| 169 | + return nil, fmt.Errorf("network error") |
| 170 | + }, |
| 171 | + }, |
| 172 | + }, |
| 173 | + } |
| 174 | + |
| 175 | + _, err := client.get("http://fail") |
| 176 | + if err == nil { |
| 177 | + t.Error("expected error for network failure") |
| 178 | + } |
| 179 | + |
| 180 | + client.httpClient.Transport = &mockTransport{ |
| 181 | + RoundTripFunc: func(req *http.Request) (*http.Response, error) { |
| 182 | + return &http.Response{ |
| 183 | + StatusCode: 500, |
| 184 | + Body: io.NopCloser(strings.NewReader("internal error")), |
| 185 | + }, nil |
| 186 | + }, |
| 187 | + } |
| 188 | + |
| 189 | + _, err = client.get("http://500") |
| 190 | + if err == nil { |
| 191 | + t.Error("expected error for HTTP 500") |
| 192 | + } |
| 193 | + |
| 194 | + t.Run("NewRequestError", func(t *testing.T) { |
| 195 | + _, err := client.get(":") |
| 196 | + if err == nil { |
| 197 | + t.Error("expected error") |
| 198 | + } |
| 199 | + }) |
| 200 | +} |
| 201 | + |
| 202 | +func TestExtractClientIDErrors(t *testing.T) { |
| 203 | + t.Run("FetchMainFailed", func(t *testing.T) { |
| 204 | + client := &Client{ |
| 205 | + httpClient: &http.Client{ |
| 206 | + Transport: &mockTransport{ |
| 207 | + RoundTripFunc: func(req *http.Request) (*http.Response, error) { |
| 208 | + return nil, fmt.Errorf("fail") |
| 209 | + }, |
| 210 | + }, |
| 211 | + }, |
| 212 | + } |
| 213 | + _, err := client.extractClientIDFrom("http://mock") |
| 214 | + if err == nil { |
| 215 | + t.Error("expected error") |
| 216 | + } |
| 217 | + }) |
| 218 | + |
| 219 | + t.Run("NoAssetsFound", func(t *testing.T) { |
| 220 | + client := &Client{ |
| 221 | + httpClient: &http.Client{ |
| 222 | + Transport: &mockTransport{ |
| 223 | + RoundTripFunc: func(req *http.Request) (*http.Response, error) { |
| 224 | + return &http.Response{ |
| 225 | + StatusCode: 200, |
| 226 | + Body: io.NopCloser(strings.NewReader("no assets here")), |
| 227 | + }, nil |
| 228 | + }, |
| 229 | + }, |
| 230 | + }, |
| 231 | + } |
| 232 | + _, err := client.extractClientIDFrom("http://mock") |
| 233 | + if err == nil || !strings.Contains(err.Error(), "no asset URLs found") { |
| 234 | + t.Errorf("unexpected error: %v", err) |
| 235 | + } |
| 236 | + }) |
| 237 | + |
| 238 | + t.Run("AssetFetchFails", func(t *testing.T) { |
| 239 | + html := `<html><body><script src="https://a-v2.sndcdn.com/assets/fail.js"></script></body></html>` |
| 240 | + client := &Client{ |
| 241 | + httpClient: &http.Client{ |
| 242 | + Transport: &mockTransport{ |
| 243 | + RoundTripFunc: func(req *http.Request) (*http.Response, error) { |
| 244 | + if req.URL.String() == "http://mock" { |
| 245 | + return &http.Response{ |
| 246 | + StatusCode: 200, |
| 247 | + Body: io.NopCloser(strings.NewReader(html)), |
| 248 | + }, nil |
| 249 | + } |
| 250 | + return nil, fmt.Errorf("asset fail") |
| 251 | + }, |
| 252 | + }, |
| 253 | + }, |
| 254 | + } |
| 255 | + _, err := client.extractClientIDFrom("http://mock") |
| 256 | + if err == nil || !strings.Contains(err.Error(), "not found in any asset bundle") { |
| 257 | + t.Errorf("unexpected error: %v", err) |
| 258 | + } |
| 259 | + }) |
| 260 | +} |
111 | 261 | func TestDownload(t *testing.T) { |
112 | 262 | // Setup encryption for mock segment |
113 | 263 | key := []byte("1234567890123456") |
|
0 commit comments