-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.go
More file actions
44 lines (36 loc) · 1008 Bytes
/
Copy pathclient_test.go
File metadata and controls
44 lines (36 loc) · 1008 Bytes
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
package main
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestFetchLocations_UsesCache(t *testing.T) {
mockJSON := `{
"count": 1,
"results": [
{ "name": "canalave-city-area", "url": "https://pokeapi.co/api/v2/location-area/1/" }
]
}`
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(mockJSON))
}))
defer server.Close()
client := NewPokeDexClient()
client.baseURL = server.URL
httpClient := &http.Client{}
data1, err := client.fetchLocations(httpClient, 0, 20)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(data1.Locations) != 1 {
t.Fatalf("expected 1 location, got %d", len(data1.Locations))
}
data2, err := client.fetchLocations(httpClient, 0, 20)
if err != nil {
t.Fatalf("unexpected error on cache hit: %v", err)
}
if data2.Locations[0].Name != "canalave-city-area" {
t.Fatalf("unexpected location name: %s", data2.Locations[0].Name)
}
}