-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
110 lines (94 loc) · 2.63 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
package chesscompubapi_test
import (
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"
"time"
"github.com/agoblet/chesscompubapi"
)
type testServerRoute struct {
pattern, responseBody string
statusCode int
requestDuration time.Duration
}
func newTestServer(routes []testServerRoute) *httptest.Server {
mux := http.NewServeMux()
for _, route := range routes {
mux.HandleFunc(route.pattern, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(route.statusCode)
if _, err := w.Write([]byte(route.responseBody)); err != nil {
w.WriteHeader(500)
return
}
time.Sleep(route.requestDuration)
})
}
server := httptest.NewServer(mux)
return server
}
func runErrorTestWithTestServer(routes []testServerRoute, f func(c *chesscompubapi.Client) error, t *testing.T) {
server := newTestServer(routes)
defer server.Close()
c := chesscompubapi.NewClient(chesscompubapi.WithBaseURL(server.URL))
err := f(c)
if err == nil {
t.Error("expected err")
}
}
func runOutputTestWithTestServer[T any](routes []testServerRoute, f func(c *chesscompubapi.Client) (T, error), want T, t *testing.T) {
server := newTestServer(routes)
defer server.Close()
c := chesscompubapi.NewClient(chesscompubapi.WithBaseURL(server.URL))
got, err := f(c)
if err != nil {
t.Errorf("expected err to be nil got %v", err)
return
}
if !reflect.DeepEqual(want, got) {
t.Errorf("got %v, want %v", got, want)
}
}
func TestWithTimeout_ShouldTimeout(t *testing.T) {
server := newTestServer([]testServerRoute{
{
pattern: "/pub/player/henk/games/archives",
requestDuration: time.Minute,
},
})
defer server.Close()
client := chesscompubapi.NewClient(chesscompubapi.WithBaseURL(server.URL), chesscompubapi.WithTimeout(time.Nanosecond))
_, err := client.ListArchives("henk")
urlErr, ok := err.(*url.Error)
if !ok {
t.Error("expected *url.Error")
return
}
if !urlErr.Timeout() {
t.Error("expected timeout")
}
}
func TestClient_ShouldFailOnStatusCode(t *testing.T) {
server := newTestServer([]testServerRoute{
{
pattern: "/pub/player/piet/games/2021/12",
responseBody: "not found",
statusCode: 404,
},
})
defer server.Close()
client := chesscompubapi.NewClient(chesscompubapi.WithBaseURL(server.URL))
const wantErrMessage = "404 not found"
_, err := client.ListGames(chesscompubapi.Archive{
Username: "piet", Year: 2021, Month: 12,
})
httpErr, ok := err.(*chesscompubapi.HTTPError)
if !ok {
t.Error("expected *chesscompubapi.HTTPError")
return
}
if httpErr.Error() != wantErrMessage {
t.Errorf("wrong error message, want '%s', got '%s'", wantErrMessage, httpErr.Error())
}
}